Closed spacemanspiff2007 closed 4 months ago
Happy to explain. This probably warrants a note in the docs.
The reasons for this behavior:
The figure in the Python docs here also shows how this ‘extrapolation’ makes sense graphically.
edit: PS: When first developing the library, I was surprised by this behavior as well. However, I soon discovered the reasoning behind it. There's an interesting thread here where the Temporal designers discuss the matter.
Thank you for the insight. What do you think is the best way to find the time after the DST switch? I've come up with something like this:
def find_time_after_dst_switch(dt: SystemDateTime, time: Time) -> Instant:
# DST changes typically occur on the full minute
time = time.replace(second=0, nanosecond=0)
hour = time.hour
minute = time.minute
while True:
minute += 1
if minute >= 60:
minute = 0
hour += 1
if hour >= 24:
hour = 0
time = time.replace(hour=hour, minute=minute)
try:
return dt.replace_time(time.replace(hour=hour, minute=minute), disambiguate='raise').instant()
except SkippedTime:
continue
@spacemanspiff2007 there's no clean way to do this, I'm afraid. This information would need to be exposed by ZoneInfo
classes. I'll probably submit a feature request for this in the stdlib.
Some potential improvements to your code:
bisect
to perform a more efficient search.minute >= 60
checks, use LocalDateTime
to do arithmetic in local time:Here is a naive algorithm which can be improved by bisect, and probably has some bugs in it still...
skipped_time = SystemDateTime(...)
guess = skipped_time.local().subtract(hours=48, ignore_dst=True) # assume there are no two transitions so closeby
while True:
try:
guess.add(minutes=1, ignore_dst=True).assume_system_tz(disambiguate="raise")
except SkippedTime:
break
@spacemanspiff2007 I've added an explicit note to the docs in the section about ambiguity. Let me know if you have any other ideas/suggestions.
When the dst change means that the clock moves forward disambiguate behaves rather unexpected. I would have expected that
earlier
returns the time before the switch forward andlater
returns the time when the switch is complete. Howeverwhenever
is guessing that I might want to add/subtract an hour and does that for me resulting in the following behavior (Which is in brief described here)What I would have expected
Could you explain the reasoning behind this behavior?