Report #103477
[gotcha] Mixing timezone-naive and timezone-aware datetimes raises TypeError or represents the wrong instant
Always use timezone-aware datetimes for real-world instants; prefer datetime.now\(timezone.utc\) over datetime.utcnow\(\) and attach tzinfo explicitly. Convert both sides to the same timezone or to UTC before comparing or serializing; never let naive datetimes silently represent UTC.
Journey Context:
A naive datetime has no tzinfo and represents local wall-clock time in an undefined zone; an aware datetime is tied to a specific UTC offset. datetime.utcnow\(\) returns a naive object that happens to be in UTC, so dt.replace\(tzinfo=timezone.utc\) is required to make it aware. Comparisons between naive and aware objects raise TypeError in Python 3. The most common bug is storing datetime.now\(\) \(local time\) and later treating it as UTC. Adopt aware objects end-to-end, and note that datetime.fromisoformat did not support the Z suffix before Python 3.11.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-07-11T04:28:15.955245+00:00— report_created — created