get the DST boundaries of a given timezone in python

半世苍凉 提交于 2019-12-17 12:21:08

问题


Is it possible to get the DST boundaries of a given timezone with pytz?


回答1:


It doesn't seem to be officially supported. However, you can poke at the internals of a DstTzInfo object and get it from there:

>>> from pytz import timezone
>>> tz = timezone('Europe/London')
>>> tz._utc_transition_times
[datetime.datetime(1, 1, 1, 0, 0), datetime.datetime(1916, 5, 21, 2, 0),
...
datetime.datetime(2036, 3, 30, 1, 0), datetime.datetime(2036, 10, 26, 1, 0),
datetime.datetime(2037, 3, 29, 1, 0), datetime.datetime(2037, 10, 25, 1, 0)]

Each entry corresponds to an entry in _transition_info:

>>> tz._transition_info
[(datetime.timedelta(0), datetime.timedelta(0), 'GMT'),
(datetime.timedelta(0, 3600), datetime.timedelta(0, 3600), 'BST'),
...
(datetime.timedelta(0, 3600), datetime.timedelta(0, 3600), 'BST'),
(datetime.timedelta(0), datetime.timedelta(0), 'GMT'),
(datetime.timedelta(0, 3600), datetime.timedelta(0, 3600), 'BST'),
(datetime.timedelta(0), datetime.timedelta(0), 'GMT')]

And the source tells us what these mean:

_utc_transition_times = None # Sorted list of DST transition times in UTC
_transition_info = None # [(utcoffset, dstoffset, tzname)] corresponding
                        # to _utc_transition_times entries

Of course, this is implementation-dependent and you'd probably want to depend on particular versions of pytz that are known to work.



来源:https://stackoverflow.com/questions/7373389/get-the-dst-boundaries-of-a-given-timezone-in-python

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