When you DIY a Time Zone library
Tags: programming, timezone, webdev
Published: 2019-04-03 11:42:46

---
title: When you DIY a Time Zone library
published: true
description: The pitfalls of implementing everything yourself
tags: programming, timezone, webdev
cover_image: https://thepracticaldev.s3.amazonaws.com/i/u9bsfgq2xotkhsvnn7dx.png
canonical_url: https://turnerj.com/blog/when-you-diy-a-time-zone-library
---

_[Cover image by Jon Tyson on Unsplash](https://unsplash.com/photos/FlHdnPO6dlw)_

You are an experienced developer who works on a Calendar application, allowing users to add events to their calendar including time and location. A new JIRA issue comes in for you to let users know when there is X amount of time before the event starts. To do this accurately with events potentially being in different regions, you need to think about time zones.

You being the experienced developer you know there has to be a list of time zones by country, all you need is that and calculate the difference between the timezones. When you find [that list](https://en.wikipedia.org/wiki/List_of_time_zones_by_country), you realise it is a bit more complicated than that with a number of countries having 2 or more time zones but no sweat, you have the location information of the event and the time zone the current user is in. You find [the more detailed list of time zones](https://en.wikipedia.org/wiki/List_of_tz_database_time_zones) and continue on your development journey.

Time zones are typically in 30-minute increments for most countries with the exception of [Nepal](https://en.wikipedia.org/wiki/UTC%2B05:45) which has been at offset +05:45 since 1986. Not a problem though, you wouldn't have constrained your system to only hold the data in 30 minute increments - you probably have it being able to support individual minute increments.

You might recall that [weird thing that happens twice a year](https://en.wikipedia.org/wiki/Daylight_saving_time) which throws the time back or forward an hour depending on the time of year and hemisphere your in. That starts to complicate things because now you need to have data on [all the countries that follow daylight savings time as well as which direction they go](https://en.wikipedia.org/wiki/Daylight_saving_time_by_country) at different points in the year. Did I mention that DST doesn't occur on the same day for everyone?

Hopefully you have realised by now that typing this data into your code or database would take way too long so you're looking up something you can import into your code, you know, _if_ the data ever changes. You find that the Internet Assigned Numbers Authority (IANA) has such [a database that you can download](https://www.iana.org/time-zones). Brilliant thing with this is that it contains DST information too! It seems like your luck has turned and this will still be easy.

Unfortunately you also realise that this database was modified very recently and has an ["Announcement" mailing list](https://mm.icann.org/mailman/listinfo/tz-announce), that doesn't bode well for a once-off import. No problem though, that is a CRON job calling an importer script in the scheme of things.

You're gonna call it here and say this is good enough. You don't want to worry about political time zone issues, you don't need to worry about historic time zone issues and you definitely don't need to worry about interplanetary time zone issues. You tidy up the rest of your code, resolve the JIRA issue and go on your merry way.

At least your project wasn't needing to calculate the seconds between two points in time, dependent on time zone...

---

This post is inspired by a fantastic [Computerphile video about time zones](https://www.youtube.com/watch?v=-5wpm-gesOY) by [Tom Scott](https://twitter.com/tomscott). His example goes into some of the more nitty gritty issues like the problems with historic times or leap seconds and I highly recommend you watch it if this article was remotely interesting.