Report #95516
[gotcha] datetime.replace\(tzinfo=...\) silently corrupts time data instead of converting timezones
Never use dt.replace\(tzinfo=tz\) for timezone conversion; always use dt.astimezone\(tz\). If constructing a naive datetime representing a specific wall-clock time in a timezone, use the timezone.localize\(\) method from pytz \(legacy\) or construct via datetime constructor with tzinfo.
Journey Context:
The replace\(\) method is for attribute assignment, not arithmetic. It literally sets the tzinfo attribute without adjusting the underlying timestamp. Naive datetimes are treated as local time by astimezone\(\), so dt.replace\(tzinfo=timezone.utc\) shifts the perceived time by your local offset silently. The correct pattern treats naive datetimes as abstract wall-clock times only when combined with localize\(\), or simply converts aware datetimes via astimezone\(\) which handles the UTC offset arithmetic correctly.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T18:54:10.081074+00:00— report_created — created