Lesson 2 of 9 · 24m
Epochs, Precision, Monotonic Clocks, and Leap Seconds
Distinguish epoch serialization, clock precision, elapsed-time measurement, and the small print that makes naive arithmetic wrong.
Engineers often say "just use epoch time" as if that settles the matter. It settles representation, not semantics. Epoch timestamps still need units, precision, timezone discipline, and an answer to an ugly question: did you mean the wall-clock moment, or the elapsed time between two events? Those are different clocks.
Unix time is an agreement
Unix epoch time counts seconds since 1970-01-01T00:00:00Z, with leap seconds traditionally excluded from the count. In practice, most systems smear or ignore leap-second complexity, but you still need integer unit clarity:
1700000000 seconds since epoch
1700000000000 milliseconds since epoch
1700000000000000 microseconds since epochA millisecond value stored in a seconds column looks like a date fifty thousand years in the future or the distant past depending on conversion direction. Name fields explicitly: created_at_epoch_ms beats timestamp when integration code crosses services.
Wall clock versus monotonic clock
Use the wall clock to record when something happened. Use a monotonic clock to measure how long an operation took:
from time import monotonic, time
started_instant = time() # wall clock, may jump
started_elapsed = monotonic() # elapsed clock, never intended as a date
# ... operation ...
duration = monotonic() - started_elapsedmonotonic() values are meaningless after restart and cannot be compared across machines. They are ideal for timeouts, deadlines, rate limiter internals, and performance measurement because NTP corrections do not move them backward.
Precision is not accuracy
A nanosecond timestamp from a distributed system does not mean two events are ordered to nanosecond truth. Clocks skew. Virtual machines drift. Containers share host clocks. Different operating systems expose different precision. For event ordering across machines, use causality — sequence numbers, vector clocks, or database ordering — in addition to timestamps.
timedatectl
chronyc trackingThose commands tell you whether the machine thinks it is synchronized and how far it currently estimates its offset. They do not prove every packet you received carried an accurate timestamp.
Leap seconds and the 2038 problem
Leap seconds are rare but real. Most application logic should avoid pretending it can arithmetic its way through them exactly. Use operating-system and library behavior consistently, and avoid hand-rolled date math. Separately, signed 32-bit epoch seconds overflow in January 2038. Old binaries, file formats, firmware, and embedded systems still carry this constraint.
Field notes
- Document epoch units in schemas and protobuf comments; milliseconds are common in JavaScript while seconds are common in Unix tooling.
- Do not compare monotonic values from different processes or hosts.
- NTP stepping can move wall time backward; monotonic clocks exist because this really happens.
- For audit trails, capture both event time and ingestion time. They answer different questions.
Was this lesson helpful?