Skip to content Skip to sidebar Skip to footer

Python Datetime.astimezone Behavior Incorrect?

Here is the code which first parses time from string in IST and then converts that to UTC. So when it 4:00 pm in India the time in GMT / UTC is 10:30 am. While the following code p

Solution 1:

astimezone() is correct. parse() is incorrect or the input is ambiguous. parse() interprets GMT+0530 as being the deprecated POSIX-style GMT+h timezone format where the utc offset sign is reversed. See Timezone offset sign reversed by Python dateutil?

To fix it, use the opposite sign:

>>> from dateutil.parser import parse
>>> d = parse('Tue Sep 01 2015 16:00:00 GMT+0530')
>>> utc = d.replace(tzinfo=None) + d.utcoffset() #NOTE: the opposite sign
>>> utc
datetime.datetime(2015, 9, 1, 10, 30)

If the input may be unambiguous (when parse() returns correct results) then you should not change the utc offset sign by hand. You could strip the timezone and reapply it again instead:

>>> import pytz
>>> tz = pytz.timezone('Asia/Kolkata')
>>> tz.localize(parse('Tue Sep 01 2015 16:00:00 GMT+0530').replace(tzinfo=None), is_dst=None)
datetime.datetime(2015, 9, 1, 16, 0, tzinfo=<DstTzInfo 'Asia/Kolkata' IST+5:30:00 STD>)
>>> _.astimezone(pytz.utc)
datetime.datetime(2015, 9, 1, 10, 30, tzinfo=<UTC>)

It may fail for ambiguous or non-existent times (during DST transitions). If you know the input is in GMT+h format then use the 1st code example and convert to UTC manually instead.


Solution 2:

Following snippet works fine.

>>> d = parse('Tue Sep 01 2014 16:00:00 GMT+0530')
>>> ind = timezone('Asia/Kolkata')
>>> d=d.replace(tzinfo=ind)
>>> d
datetime.datetime(2015, 9, 1, 16, 0, tzinfo=<DstTzInfo 'Asia/Kolkata' LMT+5:53:00 STD>)
>>> utc = timezone('UTC')
>>> d.astimezone(utc)
datetime.datetime(2015, 9, 1, 10, 7, tzinfo=<UTC>)

Here I have changed the tzinfo to be used in pytz format. Ensuring that all timezones are in pytz format.

Though, I am not sure why IST is LMT+5:53:00. Found out that offset changes over the years and +5:53 was used long back but now we use +5:30. But frankly I do not have much idea on this.


Post a Comment for "Python Datetime.astimezone Behavior Incorrect?"