Dave McGinnis
Dave McGinnis

Reputation: 615

Get List of Time Zones Across Platforms in C

I am developing a program in C which needs to return an array of strings of all of the possible regions the computer has access to information from (ostensibly from tz database). I need to make this cross-platform between Linux (or at least Ubuntu) and Windows, so any common approaches between the two platforms would make my life much easier. I am aware of a few ways to do it on Windows (through the registry, or the approach described here http://msdn.microsoft.com/en-us/library/ms171251(v=sql.90).aspx), but I haven't been able to find any way of doing this in Linux besides hard coding an array in. What would be the best way to accomplish this in Ubuntu, and is there a better way in Windows than the two I mentioned above?

Upvotes: 0

Views: 1255

Answers (3)

Alexis Wilke
Alexis Wilke

Reputation: 20720

There is a C++ library (I know you said C but you can write a wrapper...) called ICU which has all the necessary functions to load the list of timezones.

http://icu-project.org/apiref/icu4c/classTimeZone.html

Look for this function to get a complete list (other functions can be used if you'd like to filter the list in some ways):

/**
 * Returns an enumeration over all recognized time zone IDs. (i.e.,
 * all strings that createTimeZone() accepts)
 *
 * @return an enumeration object, owned by the caller.
 * @stable ICU 2.4
 */
static StringEnumeration* U_EXPORT2 createEnumeration();

Upvotes: 0

Laszlo Valko
Laszlo Valko

Reputation: 2743

Dave, the contents of the zoneinfo directory is consistent not only across Linux distros, but also across many (most) other Unices.

The exact location may be different in some cases (I know only one: on Linux, many-many years ago it used to be under /usr/lib/zoneinfo, when there was no such thing as /usr/share), so it should be configurable in your app. But you can use /usr/share/zoneinfo as a default, and it'll work in 99.9% of the cases.

Upvotes: 3

Oliver Charlesworth
Oliver Charlesworth

Reputation: 272467

In Linux, would walking the contents of /usr/share/zoneinfo be sufficient?

Upvotes: 0

Related Questions