Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find the current system timezone?

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?

like image 317
alex tingle Avatar asked Jun 25 '10 13:06

alex tingle


People also ask

Which commands will display the current system time zone?

However, you can use date or timedatectl command to display the current time and timezone as well.

What is system timezone?

The system time zone is the client-specific time zone of the system time and the system date and cannot be changed while the system is running. It is contained in the column TZONESYS in the client-specific database table TTZCU.

How do I know my timezone by date?

You can also check the timezone by simply running date command as shown below. In the above output you can see current date and time but if you are interested only in checking the timezone then you can use date "+%Z" command as shown below. You can also check the timezone in numeric form by using below date command.


1 Answers

It's hard to get a reliable answer. Relying on things like /etc/timezone may be the best bet.

(The variable tzname and the tm_zone member of struct tm, as suggested in other answers, typically contains an abbreviation such as GMT/BST etc, rather than the Olson time string as requested in the question).

  • On Debian-based systems (including Ubuntu), /etc/timezone is a file containing the right answer.
  • On some Redhat-based systems (including at least some versions of CentOS, RHEL, Fedora), you can get the required information using readlink() on /etc/localtime, which is a symlink to (for example) /usr/share/zoneinfo/Europe/London.
  • OpenBSD seems to use the same scheme as RedHat.

However, there are some issues with the above approaches. The /usr/share/zoneinfo directory also contains files such as GMT and GB, 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:

eta:~% md5sum /etc/localtime 410c65079e6d14f4eedf50c19bd073f8  /etc/localtime eta:~% find /usr/share/zoneinfo -type f | xargs md5sum | grep 410c65079e6d14f4eedf50c19bd073f8 410c65079e6d14f4eedf50c19bd073f8  /usr/share/zoneinfo/Europe/London 410c65079e6d14f4eedf50c19bd073f8  /usr/share/zoneinfo/Europe/Belfast 410c65079e6d14f4eedf50c19bd073f8  /usr/share/zoneinfo/Europe/Guernsey 410c65079e6d14f4eedf50c19bd073f8  /usr/share/zoneinfo/Europe/Jersey 410c65079e6d14f4eedf50c19bd073f8  /usr/share/zoneinfo/Europe/Isle_of_Man ... ... 

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...)

like image 200
psmears Avatar answered Sep 23 '22 21:09

psmears