Why Programmers are so bad at handling Time? (Part 1)

Raphael Moutard
7 min readMar 18, 2024

--

From payment terminals not working to EA video game crashing, on the 29th of February 2024, someone made an inventory of all the bugs worldwide caused by the Leap Day!

If you think it’s a rare glitch, given this date anomaly only happens every four years, then you would be wrong. Time is a minefield of misconceptions for programmers. “A day is always 24 hours”. “There are always 12 months in a year”. “A day always starts at midnight”. All those preconceptions are wrong. In this first part, we will debunk some falsehoods and understand how time works.

🙈 Falsehood: “29th of February only happens every 4 years”. False: The rule is more complex. The Leap Day is added in each year that is an integer multiple of four, unless it is evenly divisible by 100 but not by 400.

How do we measure time?

Historically, time was based on the Earth’s rotational period measured via the apparent movement of the Sun.

Photo by boris misevic on Unsplash

Fraction of a Solar Day

The earliest methods to measure time relied on sundials, like Egyptian obelisks (3500 BC) or gnomon in China. A solar day is the time between two consecutive solar noons when the Sun is at its highest. Then Babylonians divided the solar day into 24 hours. 12 hours of daylight and 12 hours of night (based on the number of finger joints excluding the thumb). Each hour was divided into 60 minutes (5 fingers x 12 joints), and each minute was divided into 60 seconds.

Fraction of an extrapolated year

The problem is that the apparent solar day is not constant throughout the year! Babylonian astronomers knew that the hours of daylight could vary up to 16 minutes from the mean. The main reason is the eccentricity of Earth’s orbit (an ellipsis, not a circle) so the Earth moves faster when it is nearest the Sun.

To avoid this variation problem, British mariners defined Greenwich Mean Time (GMT). It arbitrarily defines a Mean Solar Day as the yearly average length of the apparent solar day measured when the Sun reaches solar noon at the Royal Observatory in Greenwich (UK). The mean solar day contains 86,400 mean solar seconds. This measure is more accurate as the Earth’s orbit around the Sun (a year) is more stable than the Earth’s rotation.

In 1960, The Bureau of Weights and Measures improved the definition of a second. Instead of relying on an apparent Solar Day, the SI second was then defined as 1/31,556,925.9747 of the tropical year of reference 1900.

By choosing the right length for a pendulum, mechanical clocks can approximate this mean solar second without relying on solar measurements. In 1927, the quartz clock was manufactured by Bell Lab. Stressed by an electric signal, quartz crystals bend and oscillate with a predefined frequency. Captors count the number of oscillations: one SI second is approximately 32,768 cycles.

Period of caesium radiation

In the previous definition, the SI second is still not constant. The Earth's rotation slows down, due to tidal friction. Following the idea of the quartz clock, the International Bureau of Weights and Measures (BIPM) introduced, for the first time, a measure that does not depend on the Earth’s rotation anymore, making it an absolute metric.

In 1967, the SI second was redefined as the duration of 9,192,631,770 periods of radiation corresponding to the transition between the two hyperfine levels of the ground state of the cesium-133 atom , measured by Atomic Clocks.

Time Standards

As we just saw, the second had 3 different definitions. Agreeing on which definition to use is critical to have consistent measures across the globe.

Photo by Lukas Tennie on Unsplash

One-second measures a time interval, but it doesn’t define a date. A date requires a shared reference point in time. Time Standards are international conventions defining which second definition to use and what epoch to start with.

  • Universal Time (UT1) measures the Earth Rotation. Historically GMT used the Sun’s position to measure the Earth’s rotation. Although for higher precision, UT1 uses the position of distant quasars using interferometry. The 1st of January of 4713 BC at noon is the reference epoch. This is called the Julian Day.
  • International Atomic Time (TAI) — maintained by the International Bureau of Weights and Measures (BIPM) is based on the combined input of many atomic clocks around the world. The epoch is fixed at 1 January 1958 00:00:00.

As mentioned before, since the second is defined using an atomic clock, 86400 seconds do not sync anymore with the apparent solar day. In other words, TAI and UT1 are not in sync. As humans rely a lot on the alternance of light and dark, UTC was created to approximate UT1 with TAI.

  • Coordinated Universal Time (UTC) is based on the atomic time second but designed to approximate UT1 (solar). The idea is that when TAI drifts too far from UT1, UTC introduces a “leap second” to close the gap. So UTC is a discontinuous time scale. UTC is kept within 0.9 seconds of UT1.

🙈 Falsehood: “One day is always 24 hours long”. False: To sync UT1 and TAI, leap seconds will be added or removed to keep UTC in sync with TAI. Some days can be 23h59:59 seconds long.

Summary of all the time standards
Summary of the different time standards

🙈 Debunk: You can not predict leap seconds. They are announced only 6 months in advance. If your library is not updated they will not take them into account.

🙈 Fun Fact: Because Time is relative, atomic clocks have to account for their altitude as they don’t move at the same speed.

Time Zones

Time standard like UTC, defines a point in time that doesn’t depend on your position in the globe. This is convenient for plane communication or computer-to-computer communication.

Map of the time zones

But humans are not computers. At midnight UTC, it’s night 🌑 in London and daylight ☀️ in Tokyo. To solve this incoherence, we introduced Time Zones. They give the local time. Timezones are defined as an offset of UTC. Japan Standard Time (JST) is UTC+9, so at midnight UTC, it’s 9 am local time in Tokyo.

Politics and History have reshaped time zones. For instance, France used to be aligned with London UTC+0. During WW2 when Germany occupied France, the military aligned the timezone with Berlin to run trains more easily. Since WW2 France is on the Central European Timezone (UTC+1).

🙈 Falsehood: “Offsets are always integer.” False: Some offsets are a fraction of hours, like India (UTC+5:30) or New Zealand’s Chatham Islands (UTC+13:45) during daylight saving time.

🙈 Falsehood: “There are 24 timezones.” False: 38 in total. 24 basic time zones (1 for each hour), 10 special offset time zones, like India, Iran and 4 time zones around the International Date Line (UTC -12, UTC +13, UTC +13:45, and UTC +14) for Pacific Islands.

Daylight Saving Time

UTC does not change with seasons, but local time may change if a time zone jurisdiction observes daylight saving time (summer time). For example, The United Kingdom uses GMT (UTC+0) and British Summer Time (UTC+1) timezones from the last Sunday in March.

🙈 Falsehood: “GMT and UTC are the same.” False: UTC is Standard Time while GMT is a timezone. (Rq: GMT was also the former name of a Time Standard based on the Sun as written on the map. This name is not used anymore and adds to the confusion).

Calendar

So far we talked about how to measure days and hours. How do we count months and years?

The Julian calendar was proposed in 46 BC by (and takes its name from) Julius Caesar. It is a solar calendar of 365 days every year with an additional leap day every fourth year. This Calendar was replaced by the Gregorian Calendar at the request of Pope Gregory XIII to reduce the drift against the solar year. The current Gregorian year is 2024.

But there is a catch:

  • 5 countries (Ethiopia, Afghanistan, Iran, Nepal, and Vietnam) are not using the Gregorian calendar. The Ethiopian calendar (based on the old Alexandrian calendar) has 13 months and not twelve.
  • Thailand also uses the Thai lunar calendar. It is based on the birth of Buddha, 2,565 years ago. So, in Thailand, it is both 2024 AND 2567!
  • Countries like Egypt, Syria, Saudi Arabia and Morocco use two calendars: the Gregorian for administrative purposes, and the Muslim called “Hijri”, for religious holidays. This calendar starts at sunrise.

🙈 Falsehood: “The calendar days are continuous”. False, the Calendar has undergone important adjustments throughout history. For example, Pope Gregory XIII introduced a leap of 10 days, on October 5th 1582 becoming October 15th.

Conclusion (Part 1)

Time is complex. Programmers don’t learn the theory and assume too many things. Rethink your assumptions and be aware of all the nasty edge cases.

During Part 2, we will discuss how computers track time. Then we will dive deep into the code. There are so many traps depending on the programming language. We will also analyse the code of some open-source time libraries to learn how they properly implement complex patterns.

--

--

Raphael Moutard

VP engineering - Former @Amazon — Passionate about tech and Theater.