C/C++ Time-zone Code
Download:
About:
While I was working on my stock program, I needed to know when local time-zones were going to change. For example, if the program is handling data for the New York Stock Exchange, it must know that on Sunday, March 13, 2011 at 2AM (localtime) the time changes from UTC-5 to UTC-4.
Unfortunately, obtaining a list of time changes is more complicated than it seems. Time-zones are frequently changed by regional legislation. The most accepted method of obtaining transition times is to use the Olson database.
The Olson database is a maintained list of all time-zone transitions. The data is stored in a compressed format. The format is described in the tzfile man-page.
My program was written to work on Ubuntu 9.04, and it accesses a local copy of the database files at /usr/share/zoneinfo/. If your system does not have these files, you can modify the following line in the program:
file_loc = (char *)"/usr/share/zoneinfo/";
Usage:
Download get_tz_transitions.tar.gz
Run the command tar -xvfz get_tz_transitions.tar.gz to extract the contents.
Type make to compile.
The program is written in C, but can be compiled to work with C++.
The program takes two arguments. The first argument is the name of a time-zone. Valid values for this parameter are listed in this file. The second argument is a UTC timestamp we will call 'ts-start'. The current time is used if 'ts-start' is the value "now".
The program returns all transition times occuring after 'ts-start'. The program returns an array of tz_transition structs. The struct has two properties: 'ts' and 'offset'. 'ts' is the transition time's unix timestamp (UTC), and 'offset' is the new time offset from UTC (+- seconds). The first tz_transition struct in the returned array contains the location's 'offset' at UTC time 'ts-start'. The 'ts' value is the time at which the transition occured (or -1 if it was the first known transition). The remaining tz_transition structs in the array are sorted in ascending order by 'ts'.
Example usage:
tz_transitions 'timezone' 'ts-start'
or
tz_transitions 'timezone' "now"
Examples:
./tz_transitions America/New_York now
This command was executed on June 12,2011.
./tz_transitions America/New_York 2077768800
When the 'ts-start' parameter equals a transition time, the returned data appears as if the transition just occured.
./tz_transitions Europe/Rome -1900000000
Note: Passing a large negative value for the 'ts-start' parameter will return all known transitions.