Physical Time
Published: 2023-02-21 05:37:52

## Physical Time

Have you ever thought about how time works? How do your wall clocks and phone clocks know when a second has passed?

Why do different timezones around the world have different times for the same event?  
Why does a clock in India show 10:30 AM on February 19th, but a clock in the United States shows 11:00 PM on February 18th?

Even if we are convinced that there are differences in how the sun rises and sets and have adjusted the time accordingly, how do you justify your watch and wall clock showing different times, even if the difference is only in minutes?

This is strange. Time was supposed to be something we could rely on, but it turns out to be more complicated than we thought.

Understanding how time works and its nuances is critical when developing distributed systems.

## History of Time

### Egyptian Standard Time  
The first recorded time measurements were made in Egypt around 1500 B.C. They divided the time between sunrise and sunset into 12 equal parts. However, this meant that each section was not identical, as sunrise and sunset are not identical.  
This was the first definition of time based on the motion of the earth and the sun.

### Quartz Clock  
As time passed, scientists began to notice an unusual behaviour in a material known as *Quartz*. Quartz is a piezoelectric material, which means that when electricity is passed through it, it generates mechanical stress, causing it to vibrate.

Scientists discovered that under static conditions, the vibration frequency remained constant no matter where you went. Quartz’s frequency can be controlled by the crystal’s size, shape, and temperature.  
This made the material ideal for creating a new mechanical clock because you could adjust its vibration and define time based on it.

This was a revolution in timekeeping. Quartz crystals are inexpensive to produce, and minor changes in environmental conditions have little effect on frequency.

Even today, Quartz Clocks are used in computers, watches, clocks, microwaves, and pretty much any other device that keeps time.

#### Cons of Quartz Clocks

This, however, was not a perfect solution. The size, shape, and temperature of a Quartz Crystal determine its frequency. It is impossible to make identical crystals because they must be cut. There will always be minor differences and manufacturing defects, no matter how hard we try.

This difference causes time differences between the two clocks, also known as *Skew*.

This is why we rarely notice a difference in time between two devices.

### Atomic Clocks

As science progressed, scientists discovered atoms and discovered that each atom has its own frequency. Because these frequencies are inherently stable over space and time, the idea of using atomic frequency to build clocks was proposed.

Many atoms were tested, but *Ceasium* emerged as the winner in accurately measuring time, and clocks using *Ceasium* to tell time were built.  
These timepieces were known as **Atomic Clocks**.

The frequency was so stable that it was formally recognised as the international time unit.

> 1 second = 9,192,631,770 Ceasium-113 atom frequency oscillations

This is so precise that Atomic Clocks have an error of one over three million years.

#### Problems with Atomic Clock

You should have realised by now that nothing is perfect, including atomic clocks. The issue with atomic clocks is not their margin of error, but rather their high cost. Atomic clocks cost around $1500, making them prohibitively expensive for use in every device. Cheap atomic clocks are available, but they are not as accurate.

This is why, even today, we use Quartz Clocks, which are less expensive and more convenient.

Satellites and GPS systems use atomic clocks.

## So, what’s up with time zones?

They recognised that this still does not work with the motion of the earth as we built highly accurate atomic clocks.  
When you define AM as morning and PM as evening, you must be consistent.  
However, because of the earth’s motion, when one half of the world experiences day, the other half experiences night.

By this point, we had two definitions of time: * GMT — Time defined by the rotation of the Earth, and * TAI — International Atomic Time based on Atomic Clocks.

We needed a means to strike a balance between these two formats of time, and thus the UTC was born.

## Coordinated Universal Time — UTC

UTC is essentially TAI with corrections for the TAI definition of a second and the rotation of the Earth. This is known as *Leap Seconds*.

The difference is calculated on a regular basis, and extra seconds are either added to or subtracted from TAI depending on how many Leap Seconds occurred in that timeframe.

Understanding how it’s calculated and how these differences are applied isn’t critical for us, but it is important to know that something like this occurs in the real world.

## Time Synchronization

We saw how time can differ between devices, causing Skew. However, as you may have noticed, this drift no longer occurs in digital systems. This is due to NTP (Network Time Protocol).

Network Time Protocol (NTP)  
This protocol was designed to synchronise time in devices all over the world.  
As previously stated, Atomic Clocks provide the most accurate time measurement, but they are too expensive to include in every device.

So, instead of having it in every machine, why not have it in thousands of machines all over the world, with normal machines communicating with these special servers to synchronise time?

On a high level, this is how NTP operates.

### Skew Adjustment  
Assume your computer communicated with an NTP server and discovered a 200ms skew from the standard time.

Your computer’s NTP client adjusts the time without your knowledge. This is accomplished through the use of Time Smearing.

Because softwares rely on time and instructions on a computer run in nanoseconds, a time correction in the order of milliseconds can be disastrous. As a result, when a drift is detected, the difference is applied incrementally over several minutes.  
Consider adding 200ms over the course of 10 minutes. The time difference will be insignificant.