Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/Distributed Systems

How does your computer know the time? (And why it can be wrong)

Your laptop and phone never agree on the time. Here is how computers guess it, fix it, and why servers avoid trusting clocks.

Derpx0617 Nov · 6 min read
Contents
What is the clock inside a computer?What is Unix time?How does NTP fix the drift?How accurate is NTP?Why do two servers never fully agree?What is a logical clock?Why does UTC sometimes add a second?What is the 2038 problem?What should I do in my own code?Sources
0% read · 6 min leftBack to top ↑
On this page0%
Contents
What is the clock inside a computer?What is Unix time?How does NTP fix the drift?How accurate is NTP?Why do two servers never fully agree?What is a logical clock?Why does UTC sometimes add a second?What is the 2038 problem?What should I do in my own code?Sources

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.

What is the clock inside a computer?

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.

1Crystal vibrates
2Chip counts
3Counter becomes seconds
4Clock on screen
Every step here is only as good as the crystal.

So the counter drifts. But what does it count from?

What is Unix time?

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?

How does NTP fix the 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.

Your laptop
Time server

Laptop sends a request and notes T1

Step 1 of 4
The four NTP timestamps, T1 to T4.

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?

How accurate is NTP?

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.

Stratum 1: next to an atomic clock
Stratum 2: asks stratum 1
Stratum 3: asks stratum 2
Your laptop
Each hop adds a bit of doubt.

Good, but "within milliseconds" isn't "the same". That gap is a problem.

Why do two servers never fully agree?

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.

Wall clockOrder the events by each machine's clock. Skew can flip the order.
→
Logical clockOrder the events by who heard from whom. No clock needed.

So can we order events without trusting clocks at all?

What is a logical clock?

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.

  1. Every machine keeps a counter and adds one for each event.
  2. Every message carries the sender's counter.
  3. The receiver sets its counter above both its own and the one it just got.

Let's play it out. Click Next and watch B's counter jump when the message arrives.

Machine A
Machine B

A has an event, counter becomes 1

Step 1 of 4
A tiny Lamport clock run: no wall clock anywhere.

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.

Why does UTC sometimes add a second?

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.

What is the 2038 problem?

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.

Signed 32-bit seconds68 years
Unsigned 32-bit seconds (NTP)136 years
Roughly how long each counter lasts. The 68 is my own arithmetic; the NTP RFC gives 136.

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".

What should I do in my own code?

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.

Fine for

Logs, timeouts, showing humans a time.

Risky for

Deciding which write wins, or whether event A came before event B on another machine.

  1. Measuring how long something took? Use a monotonic clock, which only goes forward and ignores clock corrections. Don't subtract two wall-clock times.
  2. Showing a time to a human, or writing a log line? The wall clock is fine. Store it in UTC and convert only when you display it.
  3. Need to know which event came first across machines? Don't trust two wall clocks. Use a counter, a version number, or a logical clock.
  4. Storing timestamps? Use 64-bit integers, so the 32-bit wrap in 2038 can't bite you.
  5. Never write code that assumes every minute has 60 seconds or that time only moves forward.

In Python, the monotonic clock looks like this.

Python
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".

Key takeaways
  • A computer's clock is a counter driven by a crystal, and crystals drift.
  • NTP corrects drift by guessing the travel time, and gets within milliseconds, not perfect.
  • Different machines disagree a little, so ordering by wall clock can be wrong.
  • Logical clocks order events by cause. 32-bit Unix time runs out around 2038.

Sources

  • RFC 5905: Network Time Protocol Version 4
  • POSIX.1-2017, section 4.16 Seconds Since the Epoch
  • Lamport, Time, Clocks, and the Ordering of Events in a Distributed System, CACM 1978
  • Resolution 4 of the 27th CGPM (2022), On the use and future development of UTC
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Distributed Systems · 4 minWhat are CRDTs? How shared documents stay in syncDistributed Systems · 4 minHow does TCP work on bad Wi-Fi?Distributed Systems · 3 minWhy mobile apps are hard: your phone is on bad internet
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03What is a Bloom filter? A tiny "definitely not" checker