Report #12356
[gotcha] Using datetime.utcnow\(\) creates a naive datetime claiming to be UTC but lacking tzinfo, causing silent arithmetic errors when mixed with aware datetimes
Replace \`datetime.utcnow\(\)\` with \`datetime.now\(timezone.utc\)\` \(or \`datetime.now\(UTC\)\` in Python 3.11\+\). Always use timezone-aware datetimes in UTC for internal storage and convert to local time only at presentation layer.
Journey Context:
datetime.utcnow\(\) returns a naive datetime object \(tzinfo=None\) that represents UTC wall-clock time. When you subtract an aware datetime from this naive one, Python raises a TypeError, but the dangerous case is passing this naive UTC time to a database driver or API that assumes naive times are local time, or mixing it with \`datetime.now\(\)\` \(local naive\) for duration calculations. The \`utcnow\(\)\` method was deprecated in Python 3.12 exactly because of this footgun. The correct pattern is to use \`timezone.utc\` \(available in the standard library since 3.2\) to create aware objects, ensuring \`dt.tzinfo\` is not None and arithmetic is unambiguous.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T15:46:57.058924+00:00— report_created — created