On Linux, I need to find the currently configured timezone as an Olson location. I want my (C or C++) code to be portable to as many Linux systems as possible.
For example. I live in London, so my current Olson location is "Europe/London". I'm not interested in timezone IDs like "BST", "EST" or whatever.
Debian and Ubuntu have a file /etc/timezone
that contains this information, but I don't think I can rely on that file always being there, can I? Gnome has a function oobs_time_config_get_timezone()
which also returns the right string, but I want my code to work on systems without Gnome.
So, what's the best general way to get the currently configured timezone as an Olson location, on Linux?
It's hard to get a reliable answer. Relying on things like
/etc/timezone
may be the best bet.(The variable
tzname
and thetm_zone
member ofstruct tm
, as suggested in other answers, typically contains an abbreviation such asGMT
/BST
etc, rather than the Olson time string as requested in the question)./etc/timezone
is a file containing the right answer./etc/localtime
, which is a symlink to (for example)/usr/share/zoneinfo/Europe/London
.However, there are some issues with the above approaches. The
/usr/share/zoneinfo
directory also contains files such asGMT
andGB
, so it's possible the user may configure the symlink to point there.Also there's nothing to stop the user copying the right timezone file there instead of creating a symlink.
One possibility to get round this (which seems to work on Debian, RedHat and OpenBSD) is to compare the contents of the /etc/localtime file to the files under /usr/share/zoneinfo, and see which ones match:
Of course the disadvantage is that this will tell you all timezones that are identical to the current one. (That means identical in the full sense - not just "currently at the same time", but also "always change their clocks on the same day as far as the system knows".)
Your best bet may be to combine the above methods: use
/etc/timezone
if it exists; otherwise try parsing/etc/localtime
as a symlink; if that fails, search for matching timezone definition files; if that fails - give up and go home ;-)(And I have no idea whether any of the above applies on AIX...)
Pretty late in the day, but I was looking for something similar and found that ICU library has the provision to get the Olson timezone ID: http://userguide.icu-project.org/datetime/timezone
It is now installed on most linux distributions (install the libicu-dev package or equivalent). Code:
And to get the abbreviated/POSIX timezone names (should also work on Windows):
I liked the post made by psmears and implemented this script to read the first output of the list. Of course there must have more elegant ways of doing this, but there you are...
In my Brazilian Fedora 12, it returns:
Brazil/East
And does exactly what I need.
Thank you psmears
I see two major linux cases:
In addition, Solaris should have an /etc/TIMEZONE file that contains a line like: TZ=US/Mountain
So based on the above, here is some straight C that I believe answers the OP's question. I have tested it on Ubuntu, CentOS (Red Hat), and Solaris (bonus).
FWIW, RHEL/Fedora/CentOS have
/etc/sysconfig/clock
:There is no standard c or c++ function for this. However, GNU libc has an extention. its
struct tm
has two extra members:This means that if you use one of the functions which populates a
struct tm
(such aslocaltime
orgmtime
) you can use these extra fields. This is of course only if you are using GNU libc (and a sufficiently recent version of it).Also many systems have a
int gettimeofday(struct timeval *tv, struct timezone *tz);
function (POSIX) which will fill in astruct timezone
. This has the following fields:Not exactly what you asked for, but close...