Skip to content

Instantly share code, notes, and snippets.

@decatur
Created December 9, 2021 12:44
Show Gist options
  • Save decatur/ced9ca32f5007f435ae5483e1e082a25 to your computer and use it in GitHub Desktop.
Save decatur/ced9ca32f5007f435ae5483e1e082a25 to your computer and use it in GitHub Desktop.
Demonstrates pytz violates python spec
# 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
@decatur
Copy link
Author

decatur commented Dec 9, 2021

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment