Skip to content

.tip

· one of the small things

datetime.now(UTC), not utcnow()

datetime.utcnow() gives you the correct UTC time in an object with no tzinfo, so nothing downstream knows its offset. Compare it with an aware datetime and you get a TypeError. Call .timestamp() on it and Python treats it as local time and shifts it by your offset.

from datetime import datetime, UTC
created = datetime.now(UTC)

It's been deprecated since 3.12. Make every datetime aware where you create it, not where you print it, and comparisons stop lying to you.

python