3 ms·
Interesting. I got to thinking why a negative fold (e.g. -1) is not used to indicate local times that do not exist due to standard time to DST transition (movin
by vesinisa 3y ago
Interesting. I got to thinking why a negative fold (e.g. -1) is not used to indicate local times that do not exist due to standard time to DST transition (moving clocks forward in spring). It is in fact discussed by the PEP: https://peps.python.org/pep-0495/#strict-invalid-time-checking https://peps.python.org/pep-0495/#strict-invalid-time-checki...
The PEP provides instructions for detecting invalid times (that work as long as you use the standard library zoneinfo database.) In short - for invalid times, the UTC offset will be different for different fold versions (1/0) but with converse order than for times that exist doubly:
# Non-existent wall time (std -> DST):
>>> print(datetime(2023, 3, 26, 1, 30, tzinfo=ZoneInfo('Europe/London'), fold=0).utcoffset())
0:00:00
>>> print(datetime(2023, 3, 26, 1, 30, tzinfo=ZoneInfo('Europe/London'), fold=1).utcoffset())
1:00:00
# Ambigious wall time (DST -> std):
>>> print(datetime(2023, 10, 29, 1, 30, tzinfo=ZoneInfo('Europe/London'), fold=0).utcoffset())
1:00:00
>>> print(datetime(2023, 10, 29, 1, 30, tzinfo=ZoneInfo('Europe/London'), fold=1).utcoffset())
0:00:00