Date and time gotchas
Tags: date, time, timezone, iana
Published: 2020-08-16 15:49:47

Developing software requires to model the world. We, we developers, generally assume that models are regular. It always comes as a surprise when they are not.

> All models are wrong, but some are useful.

When it comes to dates, a calendar is a *model of organizing time* into days, weeks, months, and years. A date references a specific day in a specific calendar.

During the course of history, there has been changes to calendars _e.g._ from one calendar to another, or even within a calendar. In this post, I'd like to browse some "interesting" changes, that highlight the wrong model quote.

There are many reasons for changes, but they can be arranged under a few categories. While I'm not expert, I could identify the following:

1. Religious
2. Societal
3. Economical

## From the Julian to the Gregorian Calendar

The Julian calendar was proposed by Julius Caesar. It included 365 days a year, with a leap occurring every 4 years. In a leap year, an extra day was introduced: thus, the length of the year was 365.25 days. Considering the scientific knowledge at that time, it was being a pretty good approximation. However, there was a slight gap between its duration, and the real duration of the revolution of the Earth around the sun. This caused the official date to slowly drift from the solar year.

Interestingly enough, it was the Church that took the initiative to bring both in sync. Their motivation was to bring Easter back to its "original" date. For the record, the date of Easter is computed as follows:

>Easter is traditionally celebrated on the first Sunday after the Paschal full moon, which is the first full moon on or after 21 March (an approximation of the March equinox).
>
>-- [Wikipedia](https://en.wikipedia.org/wiki/Computus)

The equinox, and the solar year, are at the cornerstone of this computation. Since the Julian calendar drifted in regard to the solar year, Easter drifted as well. This didn't suit the Church, and it came up with the Gregorian calendar for this reason.

In regard to the subject of this post, the interesting part is that the change from the old Julian calendar to the new Gregorian calendar didn't happen simultaneously across all territories. Here's a sample of the jump depending on the country:

| Year of adoption | Country
| --- | ---
| 1582 | Dutch Republic
| | France
| | Lorraine
| | Poland
| | Portugal
| | Southern Netherlands
| | Spain
| | Tuscany
| | Venice
| 1583 | Holy Roman Empire - Catholic states
| 1700 | Denmark
| | Holy Roman Empire - Protestant states
| 1752 | Great Britain (and the British Empire)
| 1753 | Sweden
| 1918 | Russia

That means that a date between two countries might have been different!

That's the reason why the Orthodox Christmas and New Year are not respectively set to December 25<sup>th</sup> and January 1<sup>st</sup>.

## The rise of fascism and WWII

Before, and during WWII, Europe was consumed by fascism. Dictators rose to power in some countries, and they formed an alliance. Countries were invaded, such as France, or Poland. A lot has been written about the period. In the context of time, let's consider two decisions that are still in place at the time of this writing:

* Before the war, Spain's timezone was UTC, lagging one hour behind Germany's UTC+1:00. Though fascist in nature, Franco's Spain officially espoused neutrality in the conflict. Yet, [it aligned itself with Germany's timezone in 1940](https://en.wikipedia.org/wiki/Time_in_Spain#Central_European_Time). This change was made permanent in 1942 to be in line with occupied Europe.
* Likewise, France's timezone was UTC. When it surrendered to Germany in 1940, the country was divided into two zones: the Northern occupied one, and the Southern "free" one. The former was [moved to the German timezone](https://en.wikipedia.org/wiki/Time_in_France#History), while the latter kept the standard French timezone. In May '41, the Vichy government unified the Southern timezone with its Northern counterpart.

Both countries are still using UTC+1:00, with the addition of [Daylight Saving Time](#_daylight_saving_time). Today, though aligned on the same longitude, the United Kingdom and France are one timezone apart.

![Timezones of Europe](https://upload.wikimedia.org/wikipedia/commons/7/71/Time_zones_of_Europe.svg)

| Color | Timezone
|---|---
| ![UTC](https://dev-to-uploads.s3.amazonaws.com/i/j6rn11z4dah4csuyysy0.png) | Western European Time / Greenwich Mean Time (UTC)
| ![UTC/UTC+1](https://dev-to-uploads.s3.amazonaws.com/i/ndnxn6w9e27d7mbv1se9.png) | Western European Time / Greenwich Mean Time (UTC) & Western European Summer Time (UTC+1:00)
| ![UTC+1/UTC+2](https://dev-to-uploads.s3.amazonaws.com/i/gy8ucexenvle170i3bqq.png) | Central European Time (UTC+1:00) & Central European Summer Time (UTC+2:00)
| ![UTC+2](https://dev-to-uploads.s3.amazonaws.com/i/5inbhviyc241in4vg9a1.png) | Eastern European Time / Kaliningrad Time (UTC+2:00)
| ![UTC+2/UTC+3](https://dev-to-uploads.s3.amazonaws.com/i/ibmzccfh2v4auuu4ibx5.png) | Eastern European Time (UTC+2:00)
| ![UTC+3](https://dev-to-uploads.s3.amazonaws.com/i/prf5lg90gbt3z9ihbeh7.png) | Further-eastern European Time / Moscow Time / Turkey Time (UTC+3:00)

## Daylight Saving Time<span id="_daylight_saving_time"></span>

Following the fist oil shock 1973, the price of oil dramatically increased. To cope with this higer energy price, governments decided to move to Daylight Saving Time: move the clock one hour forward during summer to benefit from one hour more of sunlight, saving electricity.

By living in a country that has <abbr title="Daylight Saving Time">DST</abbr>, one might be unaware that not every country hasn't.

![Daylight Saving Time map (Wikipedia by Heitordp)](https://dev-to-uploads.s3.amazonaws.com/i/f81uqbre563kgsd0pkzj.png)

| Color | Category
| --- | ---
| ![Northern](https://dev-to-uploads.s3.amazonaws.com/i/fi7mc1ncwne8e1oxevda.png) | Northern Hemisphere Summer
| ![Southern](https://dev-to-uploads.s3.amazonaws.com/i/2erao32yhojij06t39kp.png) | Southern Hemisphere Summer
| ![Former](https://dev-to-uploads.s3.amazonaws.com/i/soucriveyjyxfnz20fc8.png) | Formerly used Daylight Saving
| ![Never](https://dev-to-uploads.s3.amazonaws.com/i/ikbg3306bbl3i4xiy3hs.png) | Never used Daylight Saving

As can be seen, it's even more complex than DST vs. non-DST. Some countries that used DST in the past don't use it anymore... or [stay on DST all year long](https://en.wikipedia.org/wiki/Daylight_saving_time#Permanent_daylight_saving_time).

And some countries have an even more complex history. Russia is an interesting example in that regard:

* Until 2011, there were standard bi-yearly changes between DST and non-DST
* From 2011 to 2014, Russia moved to permanent DST
* From 2014 on, it switched to standard time again

From an European standpoint, it's also quite surprising to notice that different states inside the USA can have different policies regarding DST.

## The last change so far

It might seem that date time changes are a thing of the past. However, nothing could be further from the truth. According to my knowledge, the last change happened in the 21<sup>st</sup> century..

Until the end of 2011, the [independent state of Samoa](https://en.wikipedia.org/wiki/Samoa) was located UTC-11:00 (with DST UTC-10:00), **east** of the International Date Line. However, most of its economical activity was targeted toward Asia, **west** of the aforementioned line. To more easily trade with Asian countries, the government decided to cross to the other side of the line.

In 2011, the the end of December the 29<sup>th</sup>, it moved to December the 31<sup>st</sup>, skipping December the 30<sup>th</sup>. Samoa has been following UTC+13:00 (with UTC+14:00) since then.

## A note about timezones

While not about changes, I'd like to share one fact about timezones that might challenge one's assumptions. Most people assume timezones are 1 hour apart. If you've been working with India, you might know that [India Standard Time](https://en.wikipedia.org/wiki/Indian_Standard_Time) is UTC+5:30. Beside IST, there are actually a couple of other timezones with a 30 minutes offset.

But it doesn't stop there! There actually are 3 timezones with a 45 minutes offset:

* [UTC+05:45](https://en.wikipedia.org/wiki/UTC%2B05:45)
* [UTC+08:45](https://en.wikipedia.org/wiki/UTC%2B08:45)
* and [UTC+12:45](https://en.wikipedia.org/wiki/UTC%2B12:45)

## Conclusion

1. Never assume anything regarding date/time: adding one minute to _23:59_ to date of day _X_ doesn't necessarily mean day _X + 1_ at _00:00_. Common sense is not your friend.
2. If software needs to manage events, it needs to be aware *where* those events did happen. Location has an impact on the calendar.
3. Please don't try to reinvent the wheel, as there are too many corner-cases. There's a community-curated database called the [tz database](https://en.wikipedia.org/wiki/Tz_database), _aka_ the <title="Internet Assigned Numbers Authority">IANA database</abbr>.

The JDK uses a static version of the latter. It can be updated using the [TZ Updater](https://www.oracle.com/java/technologies/javase/tzupdater-readme.html). Other technology stacks most probably have something similar.

**To go further:**

* [Julian calendar](https://en.wikipedia.org/wiki/Julian_calendar)
* [Difference between Gregorian and Julian calendar dates](https://en.wikipedia.org/wiki/Gregorian_calendar#Difference_between_Gregorian_and_Julian_calendar_dates)
* [Computus](https://en.wikipedia.org/wiki/Computus)
* [Daylight saving time](https://en.wikipedia.org/wiki/Daylight_saving_time)
* [International Date Line Historic alterations](https://en.wikipedia.org/wiki/International_Date_Line#Historic_alterations)
* [Time in Samoa](https://en.wikipedia.org/wiki/Time_in_Samoa)
* [IANA time zone database](https://en.wikipedia.org/wiki/Tz_database)

_Originally published at [A Java Geek](https://blog.frankel.ch/date-time-gotchas/) on August 16<sup>th</sup>, 2020_