Report #6944
[gotcha] Using \`datetime.replace\(tzinfo=...\)\` shifts timezone label without adjusting time data, causing incorrect wall-clock times
Never use \`replace\(tzinfo=...\)\` to convert between timezones or to localize a naive datetime representing local wall-clock time. Use \`dt.astimezone\(tz\)\` to convert between zones, or for attaching tzinfo to a naive datetime that is already in UTC, use \`.replace\(tzinfo=timezone.utc\)\` only if the naive time is actually UTC.
Journey Context:
\`replace\(tzinfo=...\)\` is a low-level operation that only swaps the tzinfo attribute without any time conversion or adjustment. When developers have a naive datetime representing local wall-clock time \(e.g., from \`datetime.now\(\)\`\) and want to make it timezone-aware, they often use \`.replace\(tzinfo=local\_tz\)\`. This attaches the timezone but treats the existing time values as if they were already in that timezone's offset, effectively shifting the actual UTC instant by the offset amount \(e.g., turning 10:00 local into 10:00 UTC if the offset is \+0, but the user meant 10:00\+02:00\). The correct approach requires understanding that \`astimezone\(\)\` interprets naive datetimes as system local time and converts them, while \`replace\` merely labels the existing numbers.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T01:22:09.022518+00:00— report_created — created