Weird timezone issue with pytz

喜你入骨 提交于 2019-11-26 03:58:53

问题


>>> import pytz
>>> pytz.timezone(\'Asia/Hong_Kong\')
<DstTzInfo \'Asia/Hong_Kong\' LMT+7:37:00 STD>

A seven hour and 37 minute offset? This is a little strange, does anyone experience the same issue?

In fact I\'m getting different behavior between

import pytz
from datetime import datetime
hk = pytz.timezone(\'Asia/Hong_Kong\')

dt1 = datetime(2012,1,1,tzinfo=hk)
dt2 = hk.localize(datetime(2012,1,1))
if dt1 > dt2:
   print \"Why?\"

回答1:


Time zones and offsets change over the years. The default zone name and offset delivered when pytz creates a timezone object are the earliest ones available for that zone, and sometimes they can seem kind of strange. When you use localize to attach the zone to a date, the proper zone name and offset are substituted. Simply using the datetime constructor to attach the zone to the date doesn't allow it to adjust properly.



来源:https://stackoverflow.com/questions/11473721/weird-timezone-issue-with-pytz

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