Report #16718
[gotcha] Adding timedelta to ambiguous DST time produces wrong wall-clock time
When performing arithmetic on timezone-aware datetimes around DST transitions, convert to UTC \(or another fixed-offset timezone like 'Etc/UTC'\), perform the calculation, then convert back. Never add timedelta directly to wall-clock times in zones with DST.
Journey Context:
PEP 495 introduced the \`fold\` attribute \(0 or 1\) to disambiguate wall-clock times during 'fall back' transitions \(e.g., 1:30 AM occurs twice\). However, Python's datetime arithmetic explicitly ignores \`fold\`. Adding \`timedelta\(hours=1\)\` to \`1:30 AM \(fold=0\)\` and \`1:30 AM \(fold=1\)\` yields the exact same result, regardless of which occurrence you meant. This causes scheduling errors \(alarms firing twice, cron jobs missing\). The fix acknowledges that wall-clock arithmetic is non-linear across DST; UTC is the only safe arithmetic domain.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T03:21:57.562985+00:00— report_created — created