timezone.localize
has is_dst
parameter that defaults to False
. It is used for ambiguous or non-existing input dates. You can set it to None
to raise an exception instead in such cases.
Why do you expect to get a different result only because an index inside your list is different for datetime(2013,4,7,2)
?
>>> from datetime import datetime
>>> import pytz
>>> timezone = pytz.timezone('Australia/Sydney')
>>> timezone.localize(datetime(2013,4,7,2))
datetime.datetime(2013, 4, 7, 2, 0,
tzinfo=<DstTzInfo 'Australia/Sydney' EST+10:00:00 STD>)
>>> timezone.localize(datetime(2013,4,7,2), is_dst=False)
datetime.datetime(2013, 4, 7, 2, 0,
tzinfo=<DstTzInfo 'Australia/Sydney' EST+10:00:00 STD>)
>>> timezone.localize(datetime(2013,4,7,2), is_dst=True)
datetime.datetime(2013, 4, 7, 2, 0,
tzinfo=<DstTzInfo 'Australia/Sydney' EST+11:00:00 DST>)
>>> timezone.localize(datetime(2013,4,7,2), is_dst=None)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.7/dist-packages/pytz/tzinfo.py", line 344, in localize
raise AmbiguousTimeError(dt)
pytz.exceptions.AmbiguousTimeError: 2013-04-07 02:00:00
Unless you provide is_dst
parameter explicitely there is no way for pytz to find out what time is datetime(2013,4,7,2)
.