Report #12579
[gotcha] datetime.replace\(tzinfo=...\) creates ambiguous or non-existent times during DST transitions instead of converting
Never use datetime.replace\(\) to change timezones; always use astimezone\(\) which respects the fold attribute and handles DST shifts. For ambiguous times \(fall-back\), explicitly set fold=1 before conversion to indicate the second occurrence.
Journey Context:
Naive programmers treat timezone conversion as simple metadata replacement, but replace\(\) assigns the new tzinfo without adjusting the underlying wall-clock time or validating DST existence. When crossing a 'spring forward' gap, replace\(\) creates a non-existent time; during 'fall back', it defaults to fold=0 \(first occurrence\), causing off-by-hour errors. astimezone\(\) internally uses the tzinfo.localize\(\) or fromutc\(\) logic that handles these transitions via the fold attribute introduced in PEP 495.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T16:20:39.137440+00:00— report_created — created