How time works on computers

← Home
  1. Preamble
    1. How do computers count time?
    2. Why should everyone's clock show the same time?
    3. How to count time correctly?
  2. How does NTP work?
    1. User level
    2. NTP client level
    3. NTP server level
    4. Stratum 0 server level
  3. Become an NTP server

Preamble

How do computers count time?

To keep track of time, computers rely either on a separate programmable timer (aka programmable interval timer, PIT) (as PCs do, for example) or on the frequency of their own processor (as SoCs or other embedded systems do).

КР580ВИ53

КР580ВИ53 (KR580VI53), soviet PIT from 80s. Clone of Intel 8253. Picture source

For example, if the PIT has a frequency of 1,000 Hz, then each tick will cause an interrupt, increasing the internal timer by 1 ms. Of course, timers have a much higher frequency, and the time counting is more complicated than I described, but for simplicity, let's stick with this explanation.

Why should everyone's clock show the same time?

If you set the wrong time on your computer, you will not be able to use the modern Internet. At the very least, OpenSSL will not be able to adequately check the correctness of certificates and, accordingly, the reliability of data transfer:

$ sudo date 01010000
Password:
Wed Jan  1 12:00:00 AM +05 2025
$ curl https://google.com
curl: (60) SSL certificate problem: certificate is not yet valid
More details here: https://curl.se/docs/sslcerts.html

curl failed to verify the legitimacy of the server and therefore could not
establish a secure connection to it. To learn more about this situation and
how to fix it, please visit the webpage mentioned above.
$ curl http://google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

Sites without encryption and old protocols, such as FTP or Gopher, will most likely work without problems.

Embedded systems can base the duration of some operations on the internal clock. Incorrect timing in a part of the system can lead to both incorrect sensor polling frequency and, for example, to disruption of some nuclear reactor.

How to count time correctly?

Processor frequency can change, for example due to changes in power supply, and programmable timers, although for several microseconds, do not correspond to the declared characteristics. It turns out that we need some kind of reliable source that can count time at a very high frequency, but at the same time not depend on the voltage in our outlet.

How does NTP work?

If you want to know more about the history of NTP, this Wikipedia page describes it in detail.

User level

From the point of view of the average user, NTP servers allow you to set the exact time on any device that can communicate via NTP. Often, NTP clients come with a pre-installed list of NTP servers, so many people don't even think about how it all works.

$ cat /etc/ntp.conf
...
# Pools for Gentoo users
server 0.gentoo.pool.ntp.org
server 1.gentoo.pool.ntp.org
server 2.gentoo.pool.ntp.org
server 3.gentoo.pool.ntp.org
...
$ ntpdate 0.gentoo.pool.ntp.org
18 Mar 04:13:19 ntpdate[30118]: adjust time server 91.206.16.3 offset +0.006778 sec

Default NTPD configuration from Gentoo distribution and normal output after synchronization

Windows 7 Internet Time Settings window

Time synchronization window in Windows NT

NTP client level

In general, the scheme for obtaining time looks like this:

  1. Send (via NTP/SNTP protocol) our time to the server.
  2. Receive from the server the time of receiving the request from us, the time of processing the request and the time of sending the request.
  3. Calculate the time difference and set the clock.

NTP server level

I have already described the algorithm for processing a request from a client, so we should talk about how the servers themselves know the exact time.

Servers are divided into several stratums from 0 (the most accurate) to 15. Stratum 16 means that the server or client is not synchronized. A server that receives time from stratum 2 becomes a server with stratum 3, and so on.

Servers with stratum 0 are the most accurate, but access to them is limited. The average user will usually not receive a response from such servers. On the other hand, a regular user does not need such clock accuracy and can easily request time from servers with a stratum ≥4 without any problems.

Stratum 0 server level

Servers with stratum 0 take their time from various sources, such as special modems, GPS systems, satellites, etc. Each of them receives and processes signals in their own way. These methods are beyond my interest in this topic, so let's just look at pictures of servers that do this.

U.S. Naval Observatory Alternate Master Clock

The U.S. Naval Observatory Alternate Master Clock at Schriever AFB (Colorado). Photo source

VNIIFTRI

The State Primary Standard of Units of Time at VNIIFTRI (Moscow). Photo source

Become an NTP server

If you have a server, you can join the open pool of enthusiasts who maintain NTP servers at NTP Pool Project.

The project page contains all the details, as well as instructions for setting up NTPD. You can use any other NTP server like OpenNTPD or even built-in NTP server in Mikrotik routers.

The server hosting this site is also included in the NTP Project pool with stratum 2, so you can (almost) always ask momijide.su for the exact time! This is what other NTP clients from Russia, Brazil, Ukraine, India, USA and others do.


Last update: