How To Deal With Time Zones in Database and Distributed Systems
How To Deal With Time Zones in Database and Distributed Systems
Time zones can be one of the hardest and most confusing sources of technical
issues in modern software systems. In this article we’re going to go through some
common rules of thumb for dealing with datetimes when architecting those
systems.
First, a standard
The standard representation you should use for datetimes by default is ISO 8601.
The ISO 8601 standard looks like this:
YYYY-MM-DDTHH:mm:SS±hh:mm
For a concrete example, the date and time that Marty McFly went back to the future
was:
1955-11-12T22:04:00-08:00
There are obvious advantages to this standard. For one, it will natively sort
lexicographically in any programming language. For another, it includes the time
zone to apply maximum specificity.
But the most important advantage is that it is a standard, and if you stick to it
whenever possible you will find your stack easier to develop as more and more
https://tech.hbc.com/2019-04-19-time-zones.html 1/6
30/05/2023 02:37 How to Deal With Time Zones in Database and Distributed Systems
Unfortunately it’s probably not in your best interests to include the time zone within
the stored records of the database, this is because this would break the pattern
SQL was designed to use, and for a legacy database require you do rather complex
mass data operations to bring columns up to your new spec. (Daylight Saving Time
begins and ends on different days every year, so it’s not merely a matter of adding
the zone.)
https://tech.hbc.com/2019-04-19-time-zones.html 2/6
30/05/2023 02:37 How to Deal With Time Zones in Database and Distributed Systems
haphazardly chop out the time zones, reducing the accuracy of data when viewing
your webpage internationally.
This one should be a no-brainer in certain industries. For instance television, sports,
and other live-streamed events absolutely positively need those time zones
included for good user experience.
Final thoughts
If you can, use ISO 8601 with the time zone when communicating between
services.
When writing database code make sure the DAO is in charge of deciding what
time zone to store data in. Don’t rely on upstream systems to translate datetimes
into the DAO’s time zone.
Avoid writing code that manually does time zone conversions, always trust the
language library to do this for you rather than broadly assuming you need to
apply a specific offset.
https://tech.hbc.com/2019-04-19-time-zones.html 4/6
30/05/2023 02:37 How to Deal With Time Zones in Database and Distributed Systems
Daniel Kirby
https://tech.hbc.com/2019-04-19-time-zones.html 6/6