Report #78323
[gotcha] Datetime arithmetic ignores PEP 495 fold attribute during DST transitions
Never perform arithmetic \(addition/subtraction\) on aware datetimes in local timezones with DST. Convert to UTC first, perform the arithmetic, then convert back. This avoids fold-related off-by-hour errors.
Journey Context:
PEP 495 introduced the \`fold\` attribute to disambiguate local times that occur twice during 'fall back' \(e.g., 1:30 AM happens once before DST ends, once after\). While \`astimezone\(\)\` respects \`fold\` to select the correct UTC instant, arithmetic operators \(\`\+\`, \`-\`\) on aware datetimes completely ignore \`fold\`. They treat both ambiguous instances as having the same UTC offset \(the pre-transition offset\), causing 3600-second errors when calculating durations across the fold. The standard library assumes 'wall-clock' arithmetic \(adding 1 hour to 1:30 AM gets 2:30 AM, regardless of which 1:30 AM it was\), which is correct for calendaring but wrong for physics/interval calculations. The only safe approach is UTC-normalization before math.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T14:03:50.872745+00:00— report_created — created