DEV Community

Cover image for When you DIY a Time Zone library
James Turner
James Turner

Posted on • Edited on • Originally published at turnerj.com

When you DIY a Time Zone library

Cover image by Jon Tyson on Unsplash

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, 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 and continue on your development journey.

Time zones are typically in 30-minute increments for most countries with the exception of Nepal 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 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 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. 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, 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 by Tom Scott. 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.

Top comments (8)

Collapse
 
ameliagapin profile image
Amelia Gapin

My first project at my first programming job was working on a calendar application. The company's main product, though, was time & attendance and payroll. So it was all dealing with times and timezones. We even had to be able to handle a worker starting in one timezone, crossing a DST change, and then ending in a different timezone.

Time is hard.

Collapse
 
tomasforsman profile image
Tomas Forsman

'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.'

Made me laugh so hard!

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

Got this assignment by a sadistic teacher in the late 90s. Had a lot of fun failing that one and learned to appreciate a good library.

Collapse
 
turnerj profile image
James Turner

Things like time zones and internationalization are funny beasts - they seem simple in one way but explode with complexity if you delve in remotely deep.

Got this assignment by a sadistic teacher in the late 90s. Had a lot of fun failing that one and learned to appreciate a good library.

That would be one hell of an assignment!

Collapse
 
tomasforsman profile image
Tomas Forsman • Edited

And, as you said, add history to the mix and you got a recipe for man made psychosis.

Collapse
 
ahansondev profile image
Alex Hanson

The thing that kills me is when someone provisions a server for my use and it's in my current timezone as opposed to GMT. Then things are all sorts of wacky when looking at logs and processing events 😰

Collapse
 
borgerstoon profile image
Toon Borgers

Obligatory link: youtu.be/-5wpm-gesOY

Collapse
 
turnerj profile image
James Turner

Yep, that video is great and was my inspiration for my post. I link to it at the end of my article. 🙂

Collapse
 
borgerstoon profile image
Toon Borgers

Ah yes, I didn't see that one. Nice post anyways!!