Understanding Time Zones (Part 1)
Usually, our applications have to work in different countries with particular rules, languages, currency, number formats, etc. I mean every country might have its own regional settings so every point needs a special treatment.
I have worked fixing multiple bugs related to regional settings for some years so I realized that date/time bugs are the most dangerous, usual, and difficult to fix in particular if they are related to time zones because many developers have an incomplete picture of how time zones work. I would like to explain more about time zones that sometimes are a pain in the neck.
Intro
The earth spins around itself at the rate of one rotation per 24 hours, it means that every area in the world has its noon at a different time based on the longitude of its location.
In 1884, the International Meridian Conference chose a meridian to be employed as a common zero of longitude and standard of time reckoning throughout the world. It resulted in the recommendation of the Greenwich Meridian as the prime meridian. In addition, it has established to have 24 different time zones because the earth rotates 360 degrees in 24 hours, 15 degrees longitude every hour. It means each time zone is 15 degrees longitude apart from each other.
The following map can help us to clarify those criteria:
- The Greenwich Mean Time (GMT) is the primal meridian with 0 degrees.
- In East of Greenwich (Right), every 15 degrees is a time zone +1 hour.
- In West of Greenwich (Left), every 15 degrees is a time zone -1 hour.
In theory, to handle time zones would not be complicated following previous criteria and it works properly for all cases. We would always be able to calculate the local time in any place only based on its location, but NOT. Time zones can change:
- Countries and territories that cross these artificial lines (shown on the map) decided to draw these ones differently. e.g. China is the largest country with only 1 time zone, but it should have 5 time zones.
- Some countries set clocks forward by one hour in the spring and set clocks back by one hour in autumn (DST).
- Time zones are set by Governments, but governments often change their minds.
What is GMT?
The primal meridian/primary time zone.
Greenwich Mean Time or GMT is the clock time at the Royal Observatory in Greenwich, London.
GMT is no longer a time standard.
What is UTC?
A Standard, Not a Time Zone.
Coordinated Universal Time or UTC is the primary time standard by which the world regulates clocks. It uses highly precise atomic clocks combined with the Earth’s rotation.
This is the successor to GMT.
What is a Time Zone?
A time zone is a geographic region where the same standard time is used (real place on the surface of the Earth). It means that it follows the same local clock, and abides the same set of timekeeping rules.
A time zone is NOT an offset from GMT or UTC. The offset is simply one characteristic of a timezone. Also, more than one offset might apply, at different times of the year. e.g Summertime and Standard Time.
A time zone has a legal/common name e.g. Eastern Time, Mountain Time, Central Time, Pacific Time, etc.
What is DST?
DST is the practice of advancing clocks during warmer months so that darkness falls later each day according to the clock. It means to set clocks forward by one hour in the spring and set clocks back by one hour in autumn in order to return to standard time. It was first implemented for Germany during the first world war in order to save energy.
This practice is still used in North America and Western Europe, but Russia and most of Asia and Africa countries do not use DST.
e.g. MST and MDT
- Mountain Standard Time = UTC -07:00
- Mountain Daylight Time = UTC -06:00
or GMT and BST
- Greenwich Mean Time = UTC +00:00
- British Summer Time = UTC +01:00
There are two transitions, in the spring when DST takes effect, there is an hour in local time that does not exist, and a day that is only 23 hours long (transition starts). In the fall the opposite happens a whole hour occurs twice on the same day and that day is 25 hours long (transaction ends). It can be explained better with this schema.
Daylight saving time shows us that we cannot determine local time based only on location. We have to know if daylight saving time was in effect or special cases that apply.
Time zones can change
Governments often change their minds, this is the main reason why time zones rules change. Time zones are set by Governments so it is expected surprises and oddities. It might happen for different reasons such as commerce, religious observations, political posturing, rebellion, etc.
It means that the time zone is determined by law so it is able to modify DST Rules, transitions, offsets, and alignments.
e.g. The United States. In 2007, the laws changed DST transitions.
1987- 2006
- DST starts: 1st Sunday in April.
- DST ends: Last Sunday in October.
2007- Present
- DST starts: 2nd Sunday in March.
- DST ends: 1st Sunday in November.
e.g. Samoa jumps forward in time
- Was UTC -11:00
- Now: UTC +13:00
Skipped December 30th, 2011 to synchronize its time with its trading partners Australia and New Zealand.
e.g. The Netherlands offset unrounded.
- 1909 — 1937 UTC +0:19:32.13
- 1937 — 1940 UTC +00:20
- 1940 — Present UTC +01:00
Netherlands time zone used to be unrounded, but it was changed twice.
In those examples, we realized that time zone changes happen all over the world lots of times every year. See Time Zone News.
Time Zone Fallacies
Usually, we have a lot of assumptions about time zones, it causes multiples bugs in our code. We should know more about these wrong assumptions.
Fallacy 1 — A time zone is a numeric offset from UTC — FALSE
Many time zones share offsets.
e.g. UTC +02:00 works for.
- Central Europen Summer Time
- Israel Standard Time
- Central Africa Time
Fallacy 2 — Timezone name and abbreviations are unique — FALSE
CST could mean
- Central Standard Time (North America) -UTC -06:00
- Central Standard Time (Australia) -UTC +09:30
- Central Summer Time (North America) -UTC +10:30
- China Standard Time UTC +08:00
- Cuba Standard Time UTC -05:00
IST could mean
- Israel Standard Time UTC +02:00
- India Standard Time UTC +05:30
On the other hand, not everyone agrees on following conventions so they called its time zones in its own way.
e.g. Australia
- EST Eastern Standard Time
- AEST Australian Eastern Standard Time
- EST Eastern Summer Time
- AEST Australian Eastern Summer Time
- AEDT Australian Eastern Daylight Time
- EDT Eastern Daylight Time
Fallacy 3 — In the USA, If we know the state a person is in, we can determinate his timezone — FALSE
South Dakota has two time zones.
- The Western part follows Mountain time.
- The Eastern part follows Central Time.
Fallacy 4 — An entire state follows the same rules for daylight saving time — FALSE
A lot of people think that Arizona has no DST and its time zone is only MST UTC 07:00, but there is a part in the northeastern that has DST that belong to the Navajo Indian Reservation (Navajo Nation).
Fallacy 5 — If you know what country a person is. You can determine their time zone — FALSE
Brazil has 4 different time zones, and 2 have no DST.
Fallacy 6 — There are only 24 time zones possible offer since they are in one-hour increments — FALSE.
In Australia, time zone offsets can be UTC +09:30 or UTC +08:45.
- CST UTC +09:30
- EST: UTC +10:00
- WST: UTC +08:00
- CWST: UTC +08:45
Fallacy 7 — When DST is in effect, all time zones advance their clocks by 1 hour — FALSE
Lord Howe Island, Australia advance for 30 minutes.
- LHST: UTC +10:30
- LHDT: UTC +11:00
Fallacy 8 — Time Zone will never change, so I only have to think about then when I first build my application — FALSE
Timezone rules are set by politicians, you can not reliably know the UTC offset of the future calendar dates or changes is not announced at least one year in advance.
Fallacy 9 — There can only be two daylight saving time transitions in any given year — FALSE
Egypt in 2010 in Ramadan (Islamic holy month)
- First DST April 30th — August 10th
- Second DST September 10th — September 30th
Morocco in 2013 in Ramadan (Islamic holy month)
- First DST April 30th — August 10th
- Second DST September 10th — September 30th
DST was interfering with their religious celebration, so they canceled it when the celebration was over they resumed it
Time Zones in Computing
In order to keep track of the complexities of the world’s time zones, most computer operating systems and software applications use one of the following basically two databases.
The IANA Time Zone Database
Also known as the Olson database/ TZDB / tz / zoneinfo.
The IANA database has data on all the time zones of the world, going back to at least 1970. This one is the oldest and most comprehensive source of time zone information publicly available on the Internet.
In this database, time zones are identified with a name consisting of an Area and a Location, separated by a slash.
e.g. America/New_York, Europe/London, America/Indiana/Knox, Pacific/Honolulu, America/Sao_Pablo, Asia/Shanghai, etc.
This database is used in many projects, including OS, languages, libraries, and databases.
- OS: Linux/Unix, Mac OSX, iOS, Android
- Languages: Java, PHP, Ruby, Python, C++, Perl, .NET, JS
- Databases: Oracle, PostgreSQL, MySQL. RavenDB
It is updated frequently, whenever new information is discovered about the world’s ever-changing time zones.
There are over 400 time zones in this database (over 500 with aliases). These are shown on the following world map.
The Microsoft Time Zone Database
Microsoft maintains its own list of time zones. It is used by the Windows operating system and the TimeZoneInfo class in the .NET Framework. Microsoft time zones are identified by the registry key in which they are found in Windows:
- HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Time Zones
Each time zone has a Display Name.
e.g. The “AUS Eastern Standard Time” time zone has the display name of “(UTC+10:00) Canberra, Melbourne, Sydney”.
These names are shown when choosing a time zone in the Windows control panel.
Windows time zones are maintained by Microsoft and are updated much less frequently than IANA time zones. There are currently 107 Microsoft time zones.
IANA time zone names follow a standard convention, while Microsoft time zones id seems to have no conventions.
e.g. Microsoft Time Zone ID: Romance Standard Time
IANA implements its time zones based on reported information from what is actually in effect and used by people in the region. Microsoft implements only official time zones that are set by legislation or other official government policy.
Microsoft time zones can only implement two DST transitions in a given year, so they have problems representing some real-world scenarios like Egypt and Morocco DST in 2010 and 2013.
After learning more about time zones we realized that:
Just don’t make any assumptions.
Don’t hardcode any of these rules.
Use a timezone database.
To summarize, a time zone is a geographic region, not just a number. It has rules and history. We should not make assumptions about how it works, and not hardcoding any rules. There are a lot of changes every year, exceptions, and special cases for time zone rules, but a time zone database can help us.
I wrote this text according to my personal experience working as a support engineer and some notes that I took on the Date and Time Fundamentals course by Matt Johnson where I have learned a lot.