27

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?

9
  • Are you asking for something like what tzselect does, regardless of what the TZ settings are? Commented Jun 25, 2010 at 14:13
  • 1
    You're right that /etc/timezone doesn't always exist -- I've got access to a CentOS box which doesn't have it. Commented Jun 25, 2010 at 14:24
  • 4
    On (at least one version of) CentOS you can get the required information using readlink() on /etc/localtime, which is a symlink to (for example) /usr/share/zoneinfo/Europe/London. Again, far from ideal!
    – psmears
    Commented Jun 25, 2010 at 16:58
  • 1
    @psmears - that's actually a workable idea, you should make it a proper answer. I suspect that I'll have to try various ways in order, and use whichever one works. Commented Jun 25, 2010 at 17:07
  • @alex tingle: I have updated my answer to include that, and also another possible workaround...
    – psmears
    Commented Jun 25, 2010 at 17:32

13 Answers 13

26

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

3
  • The OP wanted this for Linux mostly. AIX does not play well in the portability arena on anything remotely related to system congfiguration, including time/date settings. IMO. Commented Jun 25, 2010 at 19:14
  • The OP doesn't seem to say anything about AIX, so solving it there is a non-requirement. Commented Jun 26, 2010 at 18:40
  • 1
    The same as one-liner: find /usr/share/zoneinfo -type f | xargs md5sum | grep `md5sum /etc/localtime | cut -f 1 -d " "`
    – vasek
    Commented Apr 12, 2018 at 11:00
12

Update for C++20

This C++11/14/17 library grew into a standards proposal and was accepted as part of C++20. You can now do this with:

#include <chrono>
#include <iostream>

int
main()
{
    std::cout << std::chrono::current_zone()->name() << '\n';
}

The expression std::chrono::current_zone()->name() returns a std::string.

Example output:

Europe/London

Demo.


I've been working on a free, open source C++11/14 library which addresses this question in a single line of code:

std::cout << date::current_zone()->name() << '\n';

It is meant to be portable across all recent flavors of Linux, macOS and Windows. For me this program outputs:

America/New_York

If you download this library, and it doesn't work you, bug reports are welcome.

8

There is no standard c or c++ function for this. However, GNU libc has an extention. its struct tm has two extra members:

long tm_gmtoff;           /* Seconds east of UTC */
const char *tm_zone;      /* Timezone abbreviation */

This means that if you use one of the functions which populates a struct tm (such as localtime or gmtime) 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 a struct timezone. This has the following fields:

struct timezone {
    int tz_minuteswest;     /* minutes west of Greenwich */
    int tz_dsttime;         /* type of DST correction */
};

Not exactly what you asked for, but close...

2
  • That's all correct, but as you say, it doesn't answer my question. Commented Jun 25, 2010 at 14:41
  • 4
    Re: gettimeofday, recent documentation contains the following note: "The use of the timezone structure is obsolete; the tz argument should normally be specified as NULL. The tz_dsttime field has never been used under Linux; it has not been and will not be supported by libc or glibc. Each and every occurrence of this field in the kernel source (other than the declaration) is a bug." Commented Dec 4, 2011 at 0:01
5

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:

#include <unicode/timezone.h>
#include <iostream>

using namespace U_ICU_NAMESPACE;

int main() {
  TimeZone* tz = TimeZone::createDefault();
  UnicodeString us;
  tz->getID(us);
  std::string s;
  us.toUTF8String(s);
  std::cout << "Current timezone ID: " << s << '\n';
  delete tz;

  return 0;
}

And to get the abbreviated/POSIX timezone names (should also work on Windows):

#include <time.h>

int main() {
  time_t ts = 0;
  struct tm t;
  char buf[16];
  ::localtime_r(&ts, &t);
  ::strftime(buf, sizeof(buf), "%z", &t);
  std::cout << "Current timezone (POSIX): " << buf << std::endl;
  ::strftime(buf, sizeof(buf), "%Z", &t);
  std::cout << "Current timezone: " << buf << std::endl;
5

I see two major linux cases:

  1. Ubuntu. There should be a /etc/timezone file. This file should only contain the timezone and nothing else.
  2. Red Hat. There should be a /etc/sysconfig/clock that contains something like: ZONE="America/Chicago"

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

#include <string.h>
#include <strings.h>
#include <stdio.h>

char *findDefaultTZ(char *tz, size_t tzSize);
char *getValue(char *filename, char *tag, char *value, size_t valueSize);

int main(int argc, char **argv)
{
  char tz[128];

  if (findDefaultTZ(tz, sizeof(tz)))
    printf("Default timezone is %s.\n", tz);
  else
    printf("Unable to determine default timezone.\n");
  return 0;
}


char *findDefaultTZ(char *tz, size_t tzSize)
{
  char *ret = NULL;
  /* If there is an /etc/timezone file, then we expect it to contain
   * nothing except the timezone. */
  FILE *fd = fopen("/etc/timezone", "r"); /* Ubuntu. */
  if (fd)
  {
    char buffer[128];
    /* There should only be one line, in this case. */
    while (fgets(buffer, sizeof(buffer), fd))
    {
      char *lasts = buffer;
      /* We don't want a line feed on the end. */
      char *tag = strtok_r(lasts, " \t\n", &lasts);
      /* Idiot check. */
      if (tag && strlen(tag) > 0 && tag[0] != '#')
      {
        strncpy(tz, tag, tzSize);
        ret = tz;
      }
    }
    fclose(fd);
  }
  else if (getValue("/etc/sysconfig/clock", "ZONE", tz, tzSize)) /* Redhat.    */
    ret = tz;
  else if (getValue("/etc/TIMEZONE", "TZ", tz, tzSize))     /* Solaris. */
    ret = tz;
  return ret;
}

/* Look for tag=someValue within filename.  When found, return someValue
 * in the provided value parameter up to valueSize in length.  If someValue
 * is enclosed in quotes, remove them. */
char *getValue(char *filename, char *tag, char *value, size_t valueSize)
{
  char buffer[128], *lasts;
  int foundTag = 0;

  FILE *fd = fopen(filename, "r");
  if (fd)
  {
    /* Process the file, line by line. */
    while (fgets(buffer, sizeof(buffer), fd))
    {
      lasts = buffer;
      /* Look for lines with tag=value. */
      char *token = strtok_r(lasts, "=", &lasts);
      /* Is this the tag we are looking for? */
      if (token && !strcmp(token, tag))
      {
        /* Parse out the value. */
        char *zone = strtok_r(lasts, " \t\n", &lasts);
        /* If everything looks good, copy it to our return var. */
        if (zone && strlen(zone) > 0)
        {
          int i = 0;
          int j = 0;
          char quote = 0x00;
          /* Rather than just simple copy, remove quotes while we copy. */
          for (i = 0; i < strlen(zone) && i < valueSize - 1; i++)
          {
            /* Start quote. */
            if (quote == 0x00 && zone[i] == '"')
              quote = zone[i];
            /* End quote. */
            else if (quote != 0x00 && quote == zone[i])
              quote = 0x00;
            /* Copy bytes. */
            else
            {
              value[j] = zone[i];
              j++;
            }
          }
          value[j] = 0x00;
          foundTag = 1;
        }
        break;
      }
    }
    fclose(fd);
  }
  if (foundTag)
    return value;
  return NULL;
}
3
  • 2
    Nice answer. A few days ago I needed something like this, first and foremost on Linux -- so I cooked this up into a package gettz for R which is now on CRAN. The source is on GitHub. Commented Sep 10, 2016 at 21:50
  • Awesome. Thank you. Commented Sep 13, 2016 at 1:17
  • 1
    No, thank you -- this was really helpful. Now we "just" need someone to fill in the gaps on OS X and Windoze (as R cares about those too...). Commented Sep 13, 2016 at 1:18
3

FWIW, RHEL/Fedora/CentOS have /etc/sysconfig/clock:

ZONE="Europe/Brussels"
UTC=true
ARC=false
2

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

    /**
     * Returns the (Linux) server default timezone abbreviation
     * To be used when no user is logged in (Ex.: batch job)
     * Tested on Fedora 12
     * 
     * @param void
     * @return String (Timezone abbreviation Ex.: 'America/Sao_Paulo')
     */
    public function getServerTimezone()
    {

        $shell = 'md5sum /etc/localtime';
        $q = shell_exec($shell);
        $shell = 'find /usr/share/zoneinfo -type f | xargs md5sum | grep ' . substr($q, 0, strpos($q, '/') - 2);
        $q = shell_exec($shell);
        $q = substr($q, strpos($q, 'info/') + 5, strpos($q, " "));
        return substr($q, 0, strpos($q, chr(10)));

    }

In my Brazilian Fedora 12, it returns:
Brazil/East

And does exactly what I need.

Thank you psmears

2

Here's code that works for most versions of Linux.

#include <iostream>
#include <time.h>
#include <unistd.h>
#include <stdio.h>
#include <sys/stat.h>
using namespace std;

void main()
{
    char filename[256];
    struct stat fstat;
    int status;

    status = lstat("/etc/localtime", &fstat);
    if (S_ISLNK(fstat.st_mode))
    {
        cout << "/etc/localtime Is a link" << endl;
        int nSize = readlink("/etc/localtime", filename, 256);
        if (nSize > 0)
        {
            filename[nSize] = 0;
            cout << "    linked filename " << filename << endl;
            cout << "    Timezone " << filename + 20 << endl;
        }
    }
    else if (S_ISREG(fstat.st_mode))
        cout << "/etc/localtime Is a file" << endl;
} 
2
  • That won't work on Ubuntu, at the very least. /etc/localtime is a regular file, not even a hard link. Commented Feb 13, 2012 at 0:46
  • 1
    Alex is right about Ubuntu, but on Ubuntu the file /etc/timezone contains a text version of the Olsen time. So you could do the above code, and in the "else" case just get the contents of that file.
    – CLWill
    Commented Dec 10, 2013 at 23:11
0

According to this page, it looks like if you #include <time.h> it will declare the following.

void tzset (void);
extern char *tzname[2];
extern long timezone;
extern int daylight;

Does that give you the information that you need?

3
  • Does anyone know why when I delete the space before the 't' in time.h above "<time.h>" dissapears?
    – torak
    Commented Jun 25, 2010 at 14:16
  • because inline code should be specified with backticks, not <code> blocks
    – Evan Teran
    Commented Jun 25, 2010 at 14:17
  • @Evan Teran: Thanks. Up 'til now <code> has just worked for me. Further, I just realised that I was effectively embeding a <time.h> html tag. Without a </time.h> tag no less. D'oh!
    – torak
    Commented Jun 25, 2010 at 14:34
0

On Linux, I need to find the current timezone as an Olson location. I want my (C or C++) code to be portable to as many Linux systems as possible.

If you want to be portable, then use only GMT internally. Due to multi-user heritge, *NIX system clock is normally is in GMT and there is no system wide timezone - because different users connected to the system might be living in different timezones.

The user specific timezone is reflected in TZ environment variable and you might need to use that only when converting internal date/time into the user readable form. Otherwise, localtime() takes care of it automatically for you.

8
  • This doesn't answer my question. TZ is rarely set, and even less often set to the Olsen location. The rest of your answer is either irrelevant or incorrect - there is no such timezone as GMT, you mean UTC. (I'll give you a bonus upvote if you can explain the difference :) Commented Jun 25, 2010 at 14:39
  • 1
    GMT is UTC+0/WET and might have DST. UTC is a universal coordinated time. Or whatever. I realized too late that I have answered wrong question. But TZ has to be set for local time to work properly. And I frankly hasn't seen a system where TZ wasn't set. E.g. our application requires TZ and is portable across many systems and across many timezones: we have business cases where we have to cover countries which span multiple timezones.
    – Dummy00001
    Commented Jun 25, 2010 at 14:46
  • 1
    How the times are derived isn't relevant actually. I wrote GMT in the post and it looks out of context - because I removed the reference to the gmtime(). It might return UTC, but it is still gmtime(). The downvote is OK as it is not an answer to your question.
    – Dummy00001
    Commented Jun 25, 2010 at 15:12
  • 1
    @Dummy00001: there are plenty of systems with no TZ environment var set. Mine for example (Gentoo Linux).
    – Evan Teran
    Commented Jun 25, 2010 at 15:12
  • 1
    Just double checked. HP-UX, Solaris and Linux all save default timezone in a default location (/etc/default/tz, /etc/default/init and /etc/localtime respectively) and work fine without $TZ. But AIX wants to have $TZ and otherwise dumbly give UTC if no $TZ is set. That's apparently the reason why our software puts the requirement to have the $TZ set.
    – Dummy00001
    Commented Jun 25, 2010 at 15:41
0

The libc accesses the Olson database when tzset is called, and uses simplified time zones afterwards. tzset looks at the TZ environment variable first, and falls back to parsing the binary data in /etc/localtime.

At first systemd standardised on having the Olson time zone name in /etc/timezone, Debian-style. After systemd 190 and the /usr merge, systemd only reads and updates /etc/localtime, with the extra requirement that the file be a symlink to /usr/share/zoneinfo/${OLSON_NAME}.

Looking at TZ, then readlink("/etc/localtime"), is the most reliable way to match the libc's tzset logic and still keep symbolic Olson names. For systems that don't follow the systemd symlink convention, reading /etc/timezone (and possibly checking that /usr/share/zoneinfo/$(</etc/timezone) is the same as /etc/localtime) is a good fallback.

If you can live without symbolic names, parsing the /etc/localtime tzfile is as portable as it gets, though a lot more complex. Reading just the last field gets you a Posix time zone (for example: CST5CDT,M3.2.0/0,M11.1.0/1), which can interoperate with a few time-handling libraries, but drops some of the metadata (no historical transition info).

0

Since tzselect was not mentioned by anyone and you do need a nearly goof-proof solution, work with what Olson did. Get the tzcode and tzdata files from elsie, plus tab files.

ftp://elsie.nci.nih.gov

In March 2017, the correct location to download from would be ftp://ftp.iana.org/tz/releases (and download tzcode2017a.tar.gz and tzdata2017a.tar.gz).

Then get tzselect.ksh from the glibc download. Then you can see how to reverse engineer timezones. One sticking point: you WILL sometimes have to ask what country and city the linux box is in. You can serialize that data if you want, and then verify it against the timezone data you can find.

There is no way to do this reliably all the time without the possibility of user intervention, for example, as part of program installation.

Good luck on Arizona in general, and in Western Indiana.... hopefully your code is going to run elsewhere.

2
  • tzselect is a user interface that allows the user to choose a timezone. It tells me nothing about the configured timezone on the local machine. Commented Jun 27, 2010 at 18:32
  • Wrong - it also allows you to see how to reverse engineer the data you need. Based on user input, when there are no other options. Commented Jun 27, 2010 at 23:40
0

The code below was tested successfully in bash shell on

  • SUSE Linux Enterprise Server 11
  • Ubuntu 20.04.5 LTS
  • CentOS Linux release 7.9.2009

Shell code:

echo $(hash=$(md5sum /etc/localtime | cut -d " " -f 1) ; find /usr/share/zoneinfo -type f -print0 | while read -r -d '' f; do md5sum "$f" | grep "$hash" && break ; done) | rev | cut -d "/" -f 2,1 | rev

Result example:

Europe/Vienna

Shorter, but did not work on SLES 11:

readlink /etc/localtime | rev | cut -d "/" -f 2,1 | rev

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.