Report #8912
[gotcha] datetime.fromtimestamp\(\) raises OSError or returns ambiguous/incorrect local time during DST transitions \(gaps and folds\)
Treat datetime timestamps as UTC internally; use datetime.fromtimestamp\(ts, tz=timezone.utc\) then convert to local time with .astimezone\(tz\), or use the 'fold' attribute \(PEP 495\) with zoneinfo to disambiguate. Never use naive local time for timestamp storage.
Journey Context:
Developers assume time is a linear bijection: timestamp <-> local datetime. However, local time is discontinuous. During 'spring forward', a gap exists \(e.g., 2:00-3:00 doesn't exist\). During 'fall back', a fold exists \(1:00-2:00 occurs twice\). fromtimestamp\(\) maps a continuous scalar into this discontinuous space. The C localtime\(\) function may fail \(raising OSError\) or return ambiguous results. Python 3.6\+ added the 'fold' attribute to disambiguate, but you must use timezone-aware objects \(zoneinfo\) for it to work correctly. The robust pattern is to store UTC \(continuous\) and convert to local only for display, avoiding the fold/gap problem entirely.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T06:46:15.760501+00:00— report_created — created