Definition
A wall clock is a clock that reports the current date and time on a shared civil time scale, usually UTC (Coordinated Universal Time), the world’s reference time. A computer’s wall clock can be set, corrected and stepped backward. Clock synchronization is the process of estimating how two clocks relate, through their offset (the difference between their readings at the same instant) and their rate difference, and then either correcting one clock or converting readings from one clock to the other, with a stated bound on the error.
The idea
A host computer logs an event at 12:00:00.020 and a connected device logs another event at 12:00:00.010. The device’s event looks 10 milliseconds (ms) earlier. But if the host clock runs 20 ms ahead of the device clock, the host event happened at 12:00:00.000 in device time, 10 ms before the device event: the true order is the reverse of the printed one. Two timestamps written in the same format by two different clocks are readings of two instruments, not positions on one shared timeline.
Two people comparing wristwatches are the picture. If one watch is two minutes fast and also gains a second every hour, a note that says “meet at 3:00” needs both corrections, and the second one grows over time. Synchronization is the act of finding those corrections. The picture stops being accurate in one respect. People can look at both watches at the same instant, but two computers learn each other’s time only through messages that take time to travel, and that travel time leaves an uncertainty.
How it works
Over a stated interval, model a clock against a reference time , the time of a clock chosen as the reference: the clock reads approximately . The offset is a constant shift. The rate says how fast the clock runs relative to the reference; means 20 parts per million (ppm) fast, so its error grows by 20 microseconds every second, or s, which is 72 ms, every hour. This departure of the rate from 1 is called clock drift §6.18, and it changes slowly itself, for example with temperature. Whatever the model leaves over, the residual, collects reading noise and timestamping delays.
Offset from a message exchange. The Network Time Protocol (NTP) synchronizes computer clocks over networks this way, and the same exchange works between any host and device:
The device notes its own clock reading and sends a request.
The host notes its clock reading when the request arrives.
The host notes when it sends the reply, and puts and into the reply.
The device notes when the reply arrives.
The round-trip delay, the total time the two messages spent traveling, is . The estimated offset of the host clock relative to the device clock is . The estimate is exact when the two directions take equal time. When they do not, the estimate is wrong by up to .
Rate from repeated exchanges. Repeat the exchange after a known interval. The change in the offset divided by the elapsed time is the rate difference. With offset and rate known, a reading taken at any time between or shortly after the exchanges can be converted from one clock to the other.
Other ways to relate clocks.
A shared physical event: both sides timestamp the same signal, such as one electrical pulse on a wire connected to both. This removes the uncertainty of message travel time.
Hardware timestamping: the Precision Time Protocol (PTP), defined by the standard IEEE 1588 [301], records the instant a message passes the network interface rather than the instant software handles it, and reaches agreement better than a microsecond on suitable local networks.
Both clocks synchronized to UTC: each side’s error against UTC adds to the error between the two sides. NTP usually keeps a clock within tens of milliseconds of UTC over the public Internet, and often within 1 ms on a local network.
How a wall clock is corrected. Stepping sets it to a new value at once, possibly backward. Slewing makes it run slightly faster or slower until the error is gone. UTC also inserts leap seconds, occasional extra seconds that keep it close to the Earth’s rotation. Most systems count seconds since 1970 without leap seconds, a count called Unix time, so around a leap second their clock repeats one second, or is smeared, meaning it runs slightly slow for some hours to absorb the extra second. Each of these corrections makes differences of wall-clock readings unreliable as durations; local durations are measured with a monotonic clock §9.35, a clock whose readings never decrease.
Worked example
An exchange with real values, all in milliseconds. The device sends at on its own clock. The host receives at and replies at on its clock. The device receives at . The round-trip delay is ms. The offset is ms: the host clock is 20 ms ahead. The error of this estimate is at most ms, so the true offset lies between and ms.
Ten minutes later, after 600,000 ms of host time, a second exchange gives an offset of 32 ms. The offset grew by ms in 600,000 ms, a rate difference of , which is 20 ppm: the device clock loses ms every second relative to the host. For a device reading taken 300,000 ms of host time after the first exchange, the offset to apply is ms.
Now order two events. A device event has the device reading 7010 ms, taken while the offset is 20 ms with an error bound of 5 ms. In host time it happened at ms. A host event has the host reading 7022 ms. Each side’s own timestamp is good to 1 ms. The difference is ms, and its error is at most ms, so the true difference lies between and ms. The whole range is positive: the device event came after the host event. Had the difference been 4 ms, the range would run from to ms. That range contains 0, so the data cannot say which event came first.
Stated precisely
Let be reference time. Model the reading of clock as , where is the rate (a pure number, 1 for a perfect clock), is the offset in time units and is the residual in time units. The model is an approximation valid over a stated interval; a restart, a step correction or a change of temperature can end that interval.
For one exchange, with and read on the device clock and and read on the host clock, If both one-way travel times are nonnegative and the two clocks run at the same rate during the exchange, the true offset of the host clock relative to the device clock lies between and .
Ordering rule. Let be the difference between two event times after both are mapped to one time base, and let be a bound on the total error of that difference: the sum of the error bound of the mapping and the error bounds of the two timestamps. The order of the two events is established when . When , the order is unknown.
Limits and common mistakes
Synchronized clocks do not make a timestamp name the right event. Software reads the clock some time after the event, for example after an interrupt handler §6.12 starts running; that delay belongs to the timestamp’s error, however good the synchronization.
A mapping expires. A restart resets a device’s counter, a step correction moves a wall clock, and temperature changes the rate. Measure again, or bound how far the error can have grown since the last measurement.
Wall-clock differences are not durations. A step or a leap-second correction during an interval changes the difference. Measure local durations with a monotonic clock.
Printed precision is not accuracy. A timestamp written to the microsecond can be milliseconds wrong.
The exchange cannot see asymmetric delay. If one direction is always slower than the other, the offset estimate is off by up to half the round-trip delay, and repeating the exchange does not reveal it; only an independent reference does.
Local time is not UTC. Time zones and daylight saving time shift displayed times by hours, or in some zones by parts of an hour. Store and compare UTC.
Questions answered
How do you compare timestamps from host and device clocks?
Convert both timestamps to one time base using an estimated offset and rate between the two clocks, and compare them only together with the error bound of that conversion. The offset is the difference between the two clocks’ readings at the same instant; the rate difference says how fast that offset grows. Estimate them with timed message exchanges, as the Network Time Protocol (NTP) does, or by timestamping one shared physical signal on both sides, and repeat the estimate often enough that the offset cannot drift beyond the error bound between estimates.
Then add up the error bounds: the conversion error plus the timestamping error on each side. Two events are ordered only when their mapped difference is larger than that total; otherwise their order is unknown. Never subtract raw readings of two clocks with unrelated zero points. Record with the result how and when the mapping was made, the measured round-trip delay, and the assumption that both directions take equal time.
Related entries: Monotonic clock (§9.35), Counter quantization (§11.35), Measurement uncertainty (§11.33), Clock source and drift (§6.18), Clock domain (§13.29), Resolution versus precision versus accuracy (§11.34). Further reading: [33, 301].