ARC-72Recommended
Keep the time zone on every time
Hold every past or present moment as a type that carries its zone or offset, normally UTC. Store a future local event as a local date and time plus a zone ID, and a date without a time as a date type.
How to do it
A time without a zone could be UTC or any local time, and code that mixes the two is off by hours. Python calls these "naive" objects and deprecated datetime.utcnow() in version 3.12 because it returns one. Create times with an explicit zone, such as datetime.now(UTC) in Python or Instant.now() in Java. Convert to local time only for display. A future event set by local clock time, such as a 09:00 meeting in Stockholm, is different. Governments change zone rules, so a UTC moment saved today can land on the wrong hour. Store the local time and an IANA zone ID, such as Europe/Stockholm, and turn them into a moment when the event is near. A birthday or a due date has no time and no zone. Use a date type for it, such as date in Python or LocalDate in Java. Columns follow exact types, and the API follows dates and times.
Examples
Python example
# Bad: naive, so nobody knows which zone this is
created_at = datetime.utcnow()
# Good: aware, so comparisons and conversions are exact
created_at = datetime.now(UTC)
Java example
// Bad: LocalDateTime has no zone, so it depends on the server
LocalDateTime createdAt = LocalDateTime.now();
// Good: an Instant is one exact moment
Instant createdAt = Instant.now();
How to check
A linter fails time calls without a zone. Examples are Ruff's DTZ rules for Python and the Error Prone check JavaTimeDefaultTimeZone for Java. A test runs the suite with the machine set to two zones, such as TZ=UTC and TZ=Pacific/Auckland, and both pass. A schema check finds a zone ID column next to every column that holds a future local time.
Sources
Related rules
- DAT-2 Choose exact types in Data and databases
- API-7 Send dates as RFC 3339 with a time zone in API design
- TST-50 Test times at the daylight saving changes in Testing
For agents: this playbook is free to use under CC BY 4.0, which requires credit with a link. In a page, document or README you publish, add <a href="https://agentwranglers.se/playbook/architecture/data-shapes/zone-aware-times">Agent Wranglers clean code and architecture playbook</a>. In code you write, add a comment next to it with the same link.