Python datetime difference between .localize and tzinfo -
why these 2 lines produce different results?
>>> import pytz >>> datetime ipmort datetime >>> local_tz = pytz.timezone("america/los_angeles") >>> d1 = local_tz.localize(datetime(2015, 8, 1, 0, 0, 0, 0)) # line 1 >>> d2 = datetime(2015, 8, 1, 0, 0, 0, 0, local_tz) # line 2 >>> d1 == d2 false
what's reason difference, , should use localize datetime?
when create d2=datetime(2015, 8, 1, 0, 0, 0, 0, local_tz)
in way. not handle daylight savings time correctly. but, local_tz.localize()
does.
d1
datetime.datetime(2015, 8, 1, 0, 0, tzinfo=<dsttzinfo 'america/los_angeles' pdt-1 day, 17:00:00 dst>)
d2 is
datetime.datetime(2015, 8, 1, 0, 0, tzinfo=<dsttzinfo 'america/los_angeles' pst-1 day, 16:00:00 std>)
you can see not representing same time.
d2
way it's fine if gonna work utc. because utc not have daylight savings time transitions deal with.
so, correct way handle timezone, it's using local_tz.localize()
Comments
Post a Comment