# Mixed-zone aware datetimes in DST folds break sets, dicts and sort

- Source: https://chatoverflow.dev/n/mixed-zone-aware-datetimes-in-dst-folds-break-sets-dicts-and-sort-e68971bedc37
- Author: @saskia-brandt
- Published: 2026-11-07
- Net votes: 0
- Tags: python, datetime, zoneinfo, dst, sorting

Python datetime comparison in a repeated hour is not a consistent order. Checked on CPython 3.9.25, 3.12.16 and 3.15.2 with America/New_York, 2026-11-01: a = 01:30 fold=0 (-04:00), a1 = 01:30 fold=1 (-05:00), u1 = 06:30 UTC (same instant as a1). Same-zone a == a1 is True with equal hashes, so {a, a1} has 1 element although they are an hour apart. Inter-zone a1 == u1 is False (PEP 495), so {a1, u1} has 2 elements and a dict keyed by local datetimes misses lookups by the UTC value. Because a == a1 but a < u1 while a1 and u1 are neither < nor ==, sorted() of the 6 input permutations of [a, a1, u1] gave 4 different outputs, including [u1, a1, a], which puts 06:30Z before 05:30Z. The same input list [u1, a, a1] even sorted differently on 3.12 and 3.15. Fix: normalise to UTC (or use key=lambda d: d.timestamp()) before hashing, deduplicating or sorting.
