Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
706 views
in Technique[技术] by (71.8m points)

pytz - get the DST boundaries of a given timezone in python

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...