StackIsLife

Timezones and how to deal with them when programming

September 03, 2019

Time itself is just an arbitrary unit that enables society to function. There needs to be some way to convey that you will meet someone at a given moment for coffee, or that you and your coworkers will arrive to work together. When we program computers to do smart things, what’s really happening is someone is programming all of the details of time’s intricacies into that program. At its simplest, time can be described as a counting of seconds. Sixty seconds make a minute, 60 minutes make an hour, etc.

If only it was that simple… Time would be very easy to factor into various programs. Our lives as humans are dependent on the hours of the sun. If we all used the same time, ordering an Uber at 1:00 A.M. could be at night if you ordered it in New York City, but in the afternoon if ordered in Tokyo.

As an aside: We can still get along without clocks, but it may get pretty weird. For example, you can agree to meet someone at sunrise. You can also plan a barbeque that starts after your 20th sneeze.

As a programmer, time is difficult, and you want to make it as easy as possible for yourself. Unix time started on January 1st, 1970, and has been counting the number of seconds elapsed since. If you’re making a standard application, try really hard to use this as you’ll avoid the complexities of time zones and all the other troubles around time.

If you ever have to deal with timezones, make sure it’s absolutely necessary to do so. Try every workaround and hack you can think of to avoid having to deal with the complexity. If you still can’t get out of it, think about the expensive therapy sessions you’ll have to go to after experiencing such horrors, and weigh that against leaving your job. And never, and I mean NEVER, try to tackle something like this yourself. Use common libraries and then thank the angels that wrote that library code so that you didn’t have to. And if those common libraries fail you, such as those in the stack overflow article linked below, may God save your soul.

Link to stack overflow article - Subtracting timezones

Other related links:

The Problem With Timezones by Computerphile

Why Some Days Aren’t 24 Hours by minutephysics


Written by Scott Hansen who works in New York City building great stuff.