Your laptop and phone never agree on the time. Here is how computers guess it, fix it, and why servers avoid trusting clocks.
Your phone shows 9:00. Your laptop shows 9:01. A third device shows something else entirely, and none of them has ever looked at a wall clock.
So how does a computer know what time it is? Short answer: it doesn't. It guesses, and then it asks someone else to check its guess.
A tiny quartz crystal. It vibrates at a steady rate when you give it power, and the chip counts the vibrations. Enough vibrations make one second.
The catch is that "steady" isn't "perfect". Every crystal runs a tiny bit fast or slow, and the error piles up. That's called drift. Like a watch that gains a few seconds every day.
So the counter drifts. But what does it count from?
Seconds since a start date. Most systems store time as one number: the seconds since 1 January 1970, 00:00 UTC. That date is called the Epoch. UTC is the world's standard time, the same everywhere.
POSIX, the standard that Unix-like systems follow, defines "seconds since the Epoch" as a value that approximates the seconds elapsed since then. It also states that every day counts as exactly 86,400 seconds.
Remember that 86,400. It causes trouble later. First, how do we fix drift?
NTP (Network Time Protocol) is how your computer asks a better clock what time it is. It's a short exchange of messages with a server, where four timestamps get written down.
Click Next and follow the request. Notice where each timestamp is taken.
Laptop sends a request and notes T1
Messages take time to travel, so the laptop can't just copy the server's clock. RFC 5905 gives a formula: offset is half of ((T2 - T1) + (T3 - T4)), and the round-trip delay is (T4 - T1) - (T3 - T2). It assumes the trip there and back took equally long, which is a decent guess.
Same exchange, drawn as a sequence. So how good is the result?
The RFC states that servers and clients are typically precise within a few tens of milliseconds. On a fast local network with modern machines, it states that the potential accuracy goes down to tens of microseconds. A millisecond is a thousandth of a second.
NTP also builds a chain of trust called strata. Stratum 1 servers are wired to a real reference clock (like an atomic clock). Stratum 2 servers get time from stratum 1, and so on down.
Good, but "within milliseconds" isn't "the same". That gap is a problem.
Even with NTP, two servers ask at different moments over different paths, so they get slightly different answers. That difference is clock skew.
Now imagine two servers both write "changed at 10:00:00.003" for the same record. Which write won? If one clock is a few milliseconds behind, the real winner can look like the loser. Like two people timing a race with two watches that disagree.
So can we order events without trusting clocks at all?
In 1978 Leslie Lamport wrote Time, Clocks, and the Ordering of Events in a Distributed System. His point: we care about the order of events, not the time on the wall. He defined "happened before": if event A can have caused event B (same process, or a message from A reached B), then A comes before B.
His logical clock is just a counter. Each machine bumps it on every event and stamps it on messages. When a machine receives a message, it moves its own counter past the number it saw.
Let's play it out. Click Next and watch B's counter jump when the message arrives.
A has an event, counter becomes 1
Notice the send (1) always has a smaller number than the receive (2). The paper's Clock Condition states exactly that: if a happened before b, then a's number is smaller than b's.
That's why distributed systems often avoid wall-clock ordering. A counter can't drift, because it isn't measuring time at all.
The Earth's spin isn't perfectly steady, so UTC sometimes gets an extra second. That's a leap second. RFC 5905 even has two bits in the NTP packet that warn about one: a minute with 61 seconds, or one with 59.
But POSIX defines a day as always 86,400 seconds, so there's no slot for second number 61. Systems have to handle that somehow.
Leap seconds may be on their way out. In 2022 the CGPM (the body that sets the international measurement standards) passed a resolution about the future of UTC that talks about widening the allowed gap between astronomical time and UTC in or before 2035. I'm only summarising here, so read the resolution linked below for the exact wording.
Unix time stored in a signed 32-bit number runs out. The largest value it can hold is 2,147,483,647 seconds after the Epoch.
Counting from 1970, that works out to January 2038 (my own arithmetic, I haven't checked it against a source). After the last value, a signed 32-bit number wraps around to a large negative one. What date that shows up as depends on the software, so I won't promise a specific year.
The danger is old code and devices that still use 32 bits. NTP has its own version: per RFC 5905, its timestamp wraps in 2036 and starts a new "era".
I used to treat now() as the truth. These days I treat it as a rumour. Here are rules of thumb that follow from everything above. They're my own, not from the sources.
Logs, timeouts, showing humans a time.
Deciding which write wins, or whether event A came before event B on another machine.
In Python, the monotonic clock looks like this.
import time
start = time.monotonic()
do_work()
elapsed = time.monotonic() - start # safe even if the wall clock is corrected
created_at = time.time() # wall clock: fine for a log line or display
The rule is simple: wall clock for "what time is it", monotonic clock for "how long did it take", counters for "what came first".