Report #87174
[gotcha] Mixing naive and aware datetimes causes silent equality failures or TypeError on comparison/subtraction
Represent absolute times as aware datetimes; use \`datetime.now\(timezone.utc\)\` instead of \`datetime.utcnow\(\)\`, and attach or convert zones deliberately with \`.replace\(tzinfo=...\)\` versus \`.astimezone\(...\)\`
Journey Context:
A naive \`datetime\` has no timezone information and is treated as local time by methods like \`timestamp\(\)\` and \`astimezone\(\)\`. Since Python 3.3, \`naive\_dt == aware\_dt\` returns \`False\` instead of raising, which makes mismatches silently pass. Ordering and subtraction between naive and aware datetimes raise \`TypeError\`. The biggest practical trap is \`datetime.utcnow\(\)\`: it returns a naive object representing UTC, but \`timestamp\(\)\` then interprets it as local time, producing an off-by-hours bug. The docs explicitly deprecate \`utcnow\(\)\` in favor of \`datetime.now\(timezone.utc\)\`. Use \`.replace\(tzinfo=...\)\` only when you know the naive value already belongs to that zone; use \`.astimezone\(...\)\` when you need to convert the same instant to a different zone.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T04:54:47.994344+00:00— report_created — created