Report #92229
[gotcha] Comparing or subtracting timezone-naive and aware datetimes raises TypeError, and DST transitions create ambiguous or non-existent times
Always use timezone-aware datetimes with \`datetime.now\(timezone.utc\)\` or \`datetime.now\(ZoneInfo\('Europe/London'\)\)\`; use \`datetime.fromtimestamp\(ts, tz=timezone.utc\)\` instead of \`utcfromtimestamp\(\)\`; for ambiguous times during DST fallback, use \`fold=1\` attribute \(PEP 495\) to disambiguate later vs earlier occurrence
Journey Context:
Python's datetime has two distinct types: naive \(no tz info\) and aware. Mixing them in operations raises TypeError in Python 3. The \`utcnow\(\)\` and \`utcfromtimestamp\(\)\` methods return naive datetimes pretending to be UTC, which causes bugs when compared to actual aware UTC datetimes. DST transitions create 'fold' times \(1:30 AM happens twice\) or gaps \(2:30 AM doesn't exist\). PEP 495 added the \`fold\` attribute \(0 or 1\) to disambiguate. Libraries like \`pendulum\` or \`dateutil\` help, but stdlib requires explicit timezone objects from \`datetime.timezone\` or \`zoneinfo\` \(3.9\+\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T13:23:50.111767+00:00— report_created — created