[erlang-questions] Timezone calculations

Bruce Fitzsimons Bruce@REDACTED
Fri Sep 19 05:09:53 CEST 2008

Juan Jose Comellas wrote:
> I need to perform time calculations based on arbitrary (user-supplied) 
> timezones and I haven't found anything in Erlang/OTP that lets me 
> determine the offset (in minutes or seconds) that a timezone has 
> against UTC. The calendar module only supports converting times from 
> UTC into the local timezone and the other way around, but what if what 
> I want is to convert a time to another timezone that is not my local one?
> I'd prefer a portable solution, but even if there is an easy way to 
> access the Olson/zoneinfo tz database on Linux that would be acceptable.
Hi Juan,

There is no easy answer, TZ's are annoying. The Olson files (or 
Olson-based OS files) are the best source reference but the POSIX OS 
routines for accessing them are horrible and not at all oriented to what 
you (and I, and others) want to use them for. They're old and clunky and 
don't quite behave the way it reads on the box. They're also subtly 
different between platforms, I suspect there are reasons harking back to 
the birth of Un*x.

The way I have done it before is to, and bear with me here, is to swap 
the system (program instance) timezone for the destination timezone for 
the calculations that need performing. Basically you play with the TZ 
environment variable. Depending on your use case(s) it may also be 
necessary to change the date(!) so the offset changes e.g. DST are done 
correctly. The last time I did this it was in C/C++, the suggestions 
that you use a port program or linked-in driver would go well with this. 
TonyGJ's little driver that he used on the Erlang jukebox would be a 
fine model.

I would fully support the effort to parse the Olson files for Erlang, 
although it may well drive you mad. Parsing is only the first step 
really, getting a decent data structure around them is not simple since 
they are quite rich and the supplied Olson C code is, let me be 
charitable here, cryptic. I've called it worse but I have a grudging 
respect for it since it does work.

In these days of cheaper memory I would like to see a data structure 
that was more verbose, such as listing each time segment for every TZ 
for every year, which would make the calculations rather easier. You'd 
still need to parse the Olson files into this though since they are the 
primary (only?) single source that is comprehensive and kept current.

I'd love to hear other thoughts :-)


More information about the erlang-questions mailing list