Syncing Time Over the Internet
A new MacBook can show the wrong time before it connects to the internet. Once it joins your Wi-Fi, the clock corrects itself. I wanted to understand how it gets that time and accounts for the delay in receiving it.
If you go to System Settings > General > Date & Time on a Mac, you'll see something like this:
The time.apple.com entry points to Apple's time server, which your Mac contacts when it gets an internet connection. Asking another computer "what time is it?" sounds straightforward, until we account for the time the answer spends travelling.
The Two Friends Problem
Imagine you and a friend are chatting over the internet. Your friend is in Tokyo. You're in New York. Your watch has stopped, and you need your friend to tell you the exact time down to the second.
Your friend looks at their clock and types: "It's 2:30:05 PM."
Before you can read the message and set your watch, it has to travel through cables, routers, and switches across the Pacific Ocean. That might take 150 milliseconds, or 400. A routing hiccup could delay it by 2 full seconds.
By the time you read "2:30:05 PM," the actual time could already be 2:30:07 PM. You'd set your watch wrong by 2 seconds and never know it.
The delay matters more for a server processing financial transactions, where timestamps determine the order of trades. In a distributed database, replicas may need to agree on when something happened. A few milliseconds of disagreement can mean lost data or duplicated records.
Messages don't take a fixed amount of time to cross the internet, and neither side knows exactly how long the delay was. Telling time across a network therefore involves estimating that delay.
How NTP Actually Solves This
The Network Time Protocol, or NTP, was designed in 1985 by David Mills to address this problem. NTP measures the round trip, rather than relying on a timestamp sent in one direction.
An exchange records four timestamps:
-
Your computer sends a request to a time server and notes the exact moment it sent it. Let's call this T1 = 2:30:00.000.
-
The time server receives the request and notes when it arrived: T2 = 2:30:00.150.
-
The time server sends back a response with its current time, noting when it sent it: T3 = 2:30:00.152.
-
Your computer receives the response and notes the arrival time: T4 = 2:30:00.300.
Now your computer has four timestamps. From these, it can calculate two things:
Round-trip delay: The total time the messages spent traveling on the network:
delay = (T4 - T1) - (T3 - T2)
= (0.300 - 0.000) - (0.152 - 0.150)
= 0.300 - 0.002
= 0.298 seconds
Clock offset: How far off your clock is from the server's clock:
offset = ((T2 - T1) + (T3 - T4)) / 2
= ((0.150 - 0.000) + (0.152 - 0.300)) / 2
= (0.150 + (-0.148)) / 2
= 0.001 seconds
Your clock is about 1 millisecond behind. NTP adjusts accordingly.
This calculation assumes that the network delay is roughly symmetric: the message takes about the same time going to the server as it does coming back. That isn't always true. By repeating the process with multiple servers and averaging the results, NTP can get close enough for modern implementations to achieve accuracy within a few milliseconds over the public internet.
Why Not Just Use GPS?
Satellites already broadcast precise time signals, so GPS is another option.
Some systems do use GPS. Stratum 0 time sources, the most accurate clocks in the NTP hierarchy, are often atomic clocks or GPS receivers. Putting a GPS receiver in every laptop and phone isn't practical, though. GPS needs a clear view of the sky, uses extra power, and adds hardware cost. With NTP, devices can reach those high-precision clocks through a chain of servers.
The hierarchy works like this:
- Stratum 0: Atomic clocks, GPS receivers (the reference clocks themselves)
- Stratum 1: Servers directly wired to a stratum 0 source
- Stratum 2: Servers that sync from stratum 1
- Stratum 3: Servers that sync from stratum 2
- And so on...
Your MacBook connecting to time.apple.com is talking to a stratum 1 server. Apple runs a fleet across multiple US cities, each directly connected to reference clocks, one hop from an atomic clock. Most public NTP pools operate at stratum 2 or 3. Even at those levels, accuracy is well within a few tens of milliseconds, enough for daily use.
Why Does This Matter to You?
Several everyday systems rely on accurate clocks.
HTTPS certificates have validity windows. Kerberos authentication tokens expire within minutes, and two-factor authentication codes rotate every 30 seconds. If your clock is significantly wrong, any of these can fail with errors that don't make the cause obvious.
Build systems, file synchronization tools like Dropbox, and version control systems use timestamps to determine which version of a file is newer. A wrong clock can make your computer treat an old file as the latest version and overwrite recent work.
The servers behind cloud services also need to agree on time. Databases use timestamps to order writes, log aggregation systems use them to reconstruct incidents, and payment systems use them to sequence transactions. When clocks disagree, events can appear out of order or simultaneous when they weren't. That makes debugging much harder.
Calendar reminders, meeting notifications, message ordering in chat apps, flight check-ins, and auction deadlines also depend on your device having a reasonably accurate clock.
It's Running Right Now
Phones, laptops, servers, and smart TVs periodically contact time servers and use those four timestamps to adjust their clocks. It's work we rarely need to think about.
Your Mac checks time.apple.com regularly, perhaps every few minutes at first, then every few hours once the clock is stable. Sudden clock jumps can confuse running programs, so it normally slews the clock, speeding it up or slowing it down slightly until it catches up. Abrupt steps are only used when the offset exceeds 128 milliseconds and persists for more than 300 seconds, which rarely happens under normal conditions.
I like that a calculation this small is part of a decades-old protocol used billions of times a day. The network delays remain uncertain, but repeated measurements and a hierarchy of reference clocks let computers keep reasonably accurate time.