Report #71367
[gotcha] datetime subtraction gives wrong duration across daylight savings transitions
Always convert timezone-aware datetimes to UTC before performing arithmetic, then convert back to local time for display. When parsing ambiguous times during 'fall back' transitions, explicitly set the \`fold\` attribute \(0=first occurrence, 1=second\) on the datetime object before converting to UTC or another timezone.
Journey Context:
Naive datetime arithmetic assumes 24 hours per day. During DST transitions, a day can be 23 or 25 hours long. If you subtract two timezone-aware datetimes that straddle a transition, Python's timedelta includes the fold offset, but comparisons and 'local time' arithmetic often surprise developers who expect wall-clock duration to match elapsed seconds. Worse, ambiguous times \(when clocks 'fall back'\) default to the first occurrence \(fold=0\), meaning \`2022-11-06 01:30\` could be interpreted as 1:30 EDT or 1:30 EST depending on fold. Without explicit fold handling, converting to UTC gives wrong absolute times. The robust pattern is: store/transmit only UTC or explicit ISO strings with offsets; convert to local time only for display; never do arithmetic in local time zones.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T02:22:16.662983+00:00— report_created — created