Report #83423
[gotcha] Wrong local time conversion when using datetime.replace\(\) for timezone aware datetimes during DST transitions \(ambiguous/folded times\)
Never use datetime.replace\(tzinfo=...\) to attach a timezone to a naive datetime; always use datetime.astimezone\(tz\) or tz.localize\(\) \(pytz\) / datetime.fromtimestamp\(\) with tz. For ambiguous times \(fold\), astimezone\(\) correctly sets the fold attribute \(PEP 495\) while replace\(\) gives the wrong UTC offset.
Journey Context:
When crossing DST boundaries, clocks 'fall back' creating duplicate local times \(e.g., 1:30 AM occurs twice\). datetime.replace\(tzinfo=zoneinfo.ZoneInfo\('US/Eastern'\)\) attaches the timezone naively, picking the first occurrence \(fold=0\) and potentially the wrong UTC offset. astimezone\(\) correctly interprets the local time in the context of the transition, setting fold=1 for the second occurrence. This is critical for scheduling systems, logging timestamps, and financial transactions where 1:30 AM must be disambiguated as first or second occurrence.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T22:36:41.041817+00:00— report_created — created