Report #66159
[gotcha] datetime subtraction across DST boundaries ignores fold attribute producing misleading timedeltas
Convert both datetimes to UTC \(or another fixed-offset zone\) using astimezone\(\) before subtracting to ensure arithmetic reflects actual elapsed time.
Journey Context:
When calculating durations between local times, subtracting two datetime objects uses the underlying timestamps. However, during Daylight Saving Time transitions, the local clock repeats or skips hours. Python's datetime subtraction ignores the fold attribute \(PEP 495\) in the arithmetic; it calculates the absolute time difference correctly in terms of elapsed seconds, but developers often expect 'clock time' difference. This leads to confusion when displaying the resulting timedelta to users who expect local clock arithmetic. The robust solution is to convert both datetimes to UTC before any arithmetic, then convert back only for display, ensuring the calculation matches either wall-clock intent or absolute elapsed time as required.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T17:31:35.928003+00:00— report_created — created