Python Time
- Use
datetime.datetimewith an explicit timezone, never a naive datetime for an instant in time - Create “now” with
datetime.now(UTC)fromdatetime, notdatetime.utcnow()and notdatetime.now() - Store and compare instants in UTC; convert to a local zone only when formatting for a human
- Use
datefor a calendar day andtimedeltafor a duration, do not encode those as strings or ints of seconds unless an external protocol requires it - Parse incoming timestamps with an explicit format or
fromisoformat, and reject values with no offset - Freeze time in tests with a clock injected at the boundary, not by patching every call site
Good:
from datetime import UTC, datetime
def recorded_at() -> datetime: return datetime.now(UTC)Bad:
from datetime import datetime
def recorded_at() -> datetime: return datetime.now()