Report #93484
[gotcha] Subtracting timezone-aware datetimes across DST transition gives incorrect timedelta without fold
When creating aware datetime objects for times in the 'fold' \(ambiguous hour when DST ends\), explicitly set fold=1 on the later occurrence \(the one in standard time\) before arithmetic. Use dateutil or zoneinfo to handle ambiguous times correctly during conversion.
Journey Context:
During a 'fall back' DST transition, the local time 1:30 AM occurs twice: once at 1:30 AM DST \(UTC-4\), and again at 1:30 AM Standard Time \(UTC-5\). Python's datetime comparison and subtraction rely on the underlying UTC offset. Without the \`fold\` attribute \(introduced in PEP 495\), both instances would compare as equal and subtraction would give 0, or worse, the wrong sign. Setting \`fold=1\` marks the second occurrence as the later one, ensuring arithmetic correctly reflects the one-hour difference. This is critical for scheduling, billing, or logging systems that cannot tolerate off-by-one-hour errors twice a year.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T15:30:04.555469+00:00— report_created — created