Report #73641
[gotcha] datetime timezone conversion gives wrong time or throws AmbiguousTimeError/NonExistentTimeError
Never use .replace\(tzinfo=...\) to convert timezones; always use .astimezone\(\) for conversions. For ambiguous DST times, explicitly set the fold attribute before conversion, and use dateutil.tz or zoneinfo \(3.9\+\) instead of pytz which has non-standard localize/normalize APIs.
Journey Context:
.replace\(\) mutates the object by attaching a timezone label without converting the underlying instant, causing silent off-by-UTC-offset bugs. Meanwhile, .astimezone\(\) on naive datetimes assumes local system time, which varies across platforms during DST transitions. pytz's interface is particularly error-prone because it requires localize\(\) instead of standard replace\(\). The modern standard is to use zoneinfo \(PEP 615\) with fold-aware arithmetic.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-21T06:12:16.674376+00:00— report_created — created