You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
surprising behaviour of ZoneInfo and datetime and invalid points in time #116038
The standard libary's ZoneInfo and datetime implementations allow you to create invalid objects, such as this one, which lands in the "non-existent" DST jump forward in the UK, early 2024:
Yes - as always, operations within a time zone use naïve time. As always, if you want time zone to matter, you need to convert between time zones (typically to UTC, do computation there, and then convert back).
For astimezone() specifically, the docs warn:
If self.tzinfo is tz, self.astimezone(tz) is equal to self: no adjustment of date or time data is performed.
In any case of ambiguous or missing times around a transition, fold determines the outcome of conversion. For fold=0 (the default), the rules in effect before the transition are used, and if fold=1 then the rules in effect after the transition are used.
>>>fold0=datetime(2024, 3, 31, 1, 30, tzinfo=tz)
>>>fold1=fold0.replace(fold=1)
>>>print(fold0) # note UTC offset 0: before DST started2024-03-3101:30:00+00:00>>>fold0.utcoffset()
datetime.timedelta(0)
>>>print(fold0.astimezone(utc))
2024-03-3101:30:00+00:00>>>print(fold1) # note UTC offset of an hour: after DST started2024-03-3101:30:00+01:00>>>fold1.utcoffset()
datetime.timedelta(seconds=3600)
>>>print(fold1.astimezone(utc))
2024-03-3100:30:00+00:00# So the UTC times differ by an hour, as expected.# Converting back gives legit wall-clock times:>>>print(fold0.astimezone(utc).astimezone(tz))
2024-03-3102:30:00+01:00>>>print(fold1.astimezone(utc).astimezone(tz))
2024-03-3100:30:00+00:00# Note that DST may remain surprising: despite that DST shifted# the local clock by an hour, the two resulting local times# differ by _two_ hours on the local clock. A consequence of# that there are no times of the form 1:MM:SS on the local clock.
This feels like it should raise an exception, given that it is not a valid point in time.
See PEP 495 for why nothing ever complains about missing or ambiguous fold/gap times. It gives simple code you can use to do whatever you want in such cases. But, to date, I have yet to hear of anyone who cared enough to bother 😉.
All that said, I'm going to close this as "not planned". Since everything here is working as intended, and any change would be a backward-compatibility nightmare, it would require a new PEP to argue the case at exhausting length.
Activity
tim-one commentedon Mar 9, 2024
Yes - as always, operations within a time zone use naïve time. As always, if you want time zone to matter, you need to convert between time zones (typically to UTC, do computation there, and then convert back).
For
astimezone()specifically, the docs warn:In any case of ambiguous or missing times around a transition,
folddetermines the outcome of conversion. Forfold=0(the default), the rules in effect before the transition are used, and iffold=1then the rules in effect after the transition are used.See PEP 495 for why nothing ever complains about missing or ambiguous fold/gap times. It gives simple code you can use to do whatever you want in such cases. But, to date, I have yet to hear of anyone who cared enough to bother 😉.
All that said, I'm going to close this as "not planned". Since everything here is working as intended, and any change would be a backward-compatibility nightmare, it would require a new PEP to argue the case at exhausting length.
tim-one commentedon Mar 9, 2024
Oops - clicked a wrong button.