Is it possible to get the DST boundaries of a given timezone with pytz?
Asked
Active
Viewed 4,150 times
1 Answers
21
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.

Thomas
- 174,939
- 50
- 355
- 478
-
Not sure what you mean by that last sentence. Pytz has revisions like anything else, but are/were there actually different implementations of it? – Matt Johnson-Pint Apr 22 '17 at 17:58
-
3Who knows? You're accessing a field that starts with an underscore, which is not part of the documented API. The pytz authors may change it at any moment. – Thomas Apr 28 '17 at 18:47
-
Is there an implementation in core Python by now? – Hauke Apr 02 '19 at 15:17
-
Doesn't look like it. – Thomas Apr 02 '19 at 15:39
-
python 3.9 has [zoneinfo](https://docs.python.org/3/library/zoneinfo.html), which is roughly equivalent to pytz -- but afaict this trick doesn't work with it :) – offby1 Nov 03 '21 at 01:07
-
The [tzdata](https://github.com/python/tzdata) package most probably has a up-to-date list of all the DST changes. Did not check how to parse the data files. – Niko Föhr Nov 15 '22 at 13:37