python datetime.astimezone behavior incorrect?

爷,独闯天下 提交于 2019-12-23 16:32:08

问题


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 prints it as 9:30 pm. So instead of subtracting the offset it is adding the offset. From the python documentation https://docs.python.org/2/library/datetime.html#datetime.datetime.astimezone the sample implementation of astimezone, it does appear that it would add the offset if it is negative but it seems contrary to what it should do. The documentation says that it adjusts the time such that UTC time remains same but in passed timezone's local time which is contrary to the sample implementation.

from dateutil.parser import parse
from pytz import timezone

d = parse('Tue Sep 01 2015 16:00:00 GMT+0530')

# Prints datetime.datetime(2015, 9, 1, 16, 0, tzinfo=tzoffset(None, -19800))
print d

utc = timezone('UTC')

# Prints datetime.datetime(2015, 9, 1, 21, 30, tzinfo=<UTC>)
print d.astimezone(utc)

I am not sure what is wrong. Is it the implementation of the astimezone or the documentation or the offset itself has its sign reversed?


回答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.




回答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.



来源:https://stackoverflow.com/questions/32374060/python-datetime-astimezone-behavior-incorrect

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!