PEP 615: Support for the IANA Time Zone Database in the Standard Library

This is because there’s an STD->DST transition between 2020-03-08 and 2020-03-09, so the difference in wall time is 24 hours, but the absolute elapsed time is 23 hours. I wrote a blog post about datetime arithmetic semantics that goes into more detail about this, but basically, the way datetime’s arithmetic and comparisons work is something like this:

def subtract(a, b):
    if a.tzinfo is b.tzinfo:
        return a.replace(tzinfo=None) - b.replace(tzinfo=None)
    else:
        UTC = timezone.utc
        return a.astimezone(UTC) - b.astimezone(UTC)

So dt2 - dt0 is treated as two different zones and the math is done in UTC, whereas dt1 - dt0 is treated as the same zone, and the math is done in local time.

dt1 will necessarily be the same zone as dt0, because it’s the result of an arithmetical operation on dt0. dt2 is a different zone because I bypassed the cache, but if it hit the cache, the two would be the same.

2 Likes