Cobalt Field Notes

Store UTC, convert at the edges

· 2 min read

Most time zone bugs come from converting in the wrong place. The rule that has worked for me: store and compute in UTC, and convert to a local zone only where a person reads or enters a time.

Storage

Store instants in UTC, either as integers (Unix seconds or milliseconds) or as ISO 8601 strings with an explicit offset such as 2025-03-30T01:30:00Z. A timestamp without a zone becomes ambiguous the moment a second server, or a second developer, reads it.

Calendar questions need a zone

“Did this happen today?” and “reset the quota on the first of the month” are not questions about instants; they depend on whose calendar is meant. Keep the zone as configuration, as an IANA name such as Europe/Berlin rather than a fixed offset, which breaks at the next daylight saving change, and apply it in exactly one place.

Daylight saving time

Twice a year local time misbehaves. In spring a range of local times does not exist: in Europe/Berlin on 30 March 2025 the clocks jumped from 02:00 to 03:00, so 02:30 never happened. In autumn a range happens twice: on 26 October 2025 the clocks went back from 03:00 to 02:00, so 02:30 occurred once in summer time and once in winter time.

Schedulers differ in how they treat jobs inside those windows; some run them once, some skip them, some run them twice. systemd timers evaluate OnCalendar= in the local zone unless the expression names one, as in OnCalendar=*-*-* 02:30:00 UTC. Jobs that must run exactly once a day are safer outside 01:00 to 03:00 local time, or scheduled in UTC.

Testing

Give the code under test a clock and a zone instead of letting it read the system ones, and put the two transition days into the test data. Most of these bugs show up at once.