Created
December 9, 2021 12:44
-
-
Save decatur/ced9ca32f5007f435ae5483e1e082a25 to your computer and use it in GitHub Desktop.
Demonstrates pytz violates python spec
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Please read https://blog.ganssle.io/tag/pytz.html | |
import datetime | |
# The Good | |
try: | |
import zoneinfo | |
except ImportError: | |
from backports import zoneinfo | |
berlin = zoneinfo.ZoneInfo("Europe/Berlin") | |
dt1 = datetime.datetime(2021, 10, 31, 2, tzinfo=berlin, fold=0) | |
dt2 = datetime.datetime(2021, 10, 31, 2, tzinfo=berlin, fold=1) | |
print(dt1.isoformat()) | |
print(dt2.isoformat()) | |
print(dt2 - dt1) # -> 0:00:00 according python specs | |
# The Ugly | |
from pytz import timezone | |
berlin = timezone('Europe/Berlin') | |
dt1 = berlin.localize(datetime.datetime(2021, 10, 31, 2), is_dst=True) | |
dt2 = berlin.localize(datetime.datetime(2021, 10, 31, 2), is_dst=False) | |
print(dt1.isoformat()) | |
print(dt2.isoformat()) | |
print(dt2 - dt1) # -> 1:00:00 in violation of python specs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
See also https://pytz-deprecation-shim.readthedocs.io/en/latest/