Date and time gotchas

Nicolas Fränkel - Aug 16 '20 - - Dev Community

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

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 25th and January 1st.

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. 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, 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. Today, though aligned on the same longitude, the United Kingdom and France are one timezone apart.

Timezones of Europe

Color Timezone
UTC Western European Time / Greenwich Mean Time (UTC)
UTC/UTC+1 Western European Time / Greenwich Mean Time (UTC) & Western European Summer Time (UTC+1:00)
UTC+1/UTC+2 Central European Time (UTC+1:00) & Central European Summer Time (UTC+2:00)
UTC+2 Eastern European Time / Kaliningrad Time (UTC+2:00)
UTC+2/UTC+3 Eastern European Time (UTC+2:00)
UTC+3 Further-eastern European Time / Moscow Time / Turkey Time (UTC+3:00)

Daylight Saving Time

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 DST, one might be unaware that not every country hasn't.

Daylight Saving Time map (Wikipedia by Heitordp)

Color Category
Northern Northern Hemisphere Summer
Southern Southern Hemisphere Summer
Former Formerly used Daylight Saving
Never 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.

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 21st century..

Until the end of 2011, the independent state of 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 29th, it moved to December the 31st, skipping December the 30th. 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 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:

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, aka the IANA database.

The JDK uses a static version of the latter. It can be updated using the TZ Updater. Other technology stacks most probably have something similar.

To go further:

Originally published at A Java Geek on August 16th, 2020

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .