Geek warning: This post is a bit of a total geek out but in any case here’s a bit of code that some of you might find useful. For the rest of youzes here is a pretty picture:
Yesterday I ended up writing a small piece of code to determine what timezone code a person is in. This was not as trivial as it sounds.
In fact it was a learning experience about how complex our organization of time is – living on a sphere and all. Timezone maps are not country maps, in fact time-zone maps are a complicated politic cleaving apart regions that have a minimum number of people – like some kind of crazy voronoi diagram of clusters of human populations. Country boundaries play a strong role, but timezones are more like a mold that has grown over the existing history of a landscape. Alberta has a time zone that juts into Saskatchewan just to capture one town. Argentina slices timezones horizontally towards the South Pole to conserve sunlight for farmers. Chile doesn’t give a damn and puts all of Chile in one gigantic vertical strip of a timezone – so that in the winter it is dark at 6:00 in Santiago but sunny at 6:00 in Tierra Del Fuego. In other places of the world things like small islands show up more clearly than in country maps because each island tends to be a well defined separation of human populations and thus a good opportunity to have a single time zone code. A lost history is left traced in palimpsests here of residual boundaries.
If you look at the zoneinfo article on Wikipedia you can get a sense of what is going on here : http://en.wikipedia.org/wiki/Zoneinfo
The backstory is that the OpenBSD folks wanted help to automate configuration of timezones for installs. What we did was take the geolocation of their IP and use it to look up a timezone code.
There are 27000+ polygonal boundaries that make up the timezones. And there an odd 370+ timezones including all the various cases.
The goals were:
- We wanted an extremely fast computation.
- We wanted it to be static ( not require a database ).
- Return Olson posix string like “Europe/Paris” .
I did fix one bug which was that in asking MapServer to spit out .png files it was palettizing the colors into an 8 bit deep image – whereas I needed 9 bits… So I had to ask mapserver to print out a tiff. If you see any other bugs….
Here are the source files in any case:
You can build it by typing
And you can test it running with a longitude, latitude pair – for example:
./a.out -114 53
Which should return to you a string showing the time zone you are in.
The way it works is that I read in a timezone shape file from this place
This was piped to the following program:
Which instructed my mapserver to generate a special kind of cloropleth map – which can be seen here ( but don’t bother because it chews my machine ) :
This data file can now be used as a bitmapped query interface for discovery of unique time zone codes as done above – or as done in ruby here :
You need ImageMagick installed.
If you wish, you can convert the image into a ppm file or something and embed it directly in the C program. Or memory map it and have a query gateway to it… that would be fastest.