C++ 如何找到当前系统时区?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3118582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 12:06:38  来源:igfitidea点击:

How do I find the current system timezone?

c++clinuxtimezone

提问by alex tingle

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.

在 Linux 上,我需要找到当前配置的时区作为 Olson 位置。我希望我的(C 或 C++)代码能够移植到尽可能多的 Linux 系统。

For example. I live in London, so my current Olson location is "Europe/London". I'm notinterested in timezone IDs like "BST", "EST" or whatever.

例如。我住在伦敦,所以我目前在奥尔森的位置是“欧洲/伦敦”。我对“BST”、“EST”等时区 ID感兴趣。

Debian and Ubuntu have a file /etc/timezonethat 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.

Debian 和 Ubuntu 有一个/etc/timezone包含此信息的文件,但我认为我不能依赖该文件始终存在,对吗?Gnome 有一个函数oobs_time_config_get_timezone(),它也返回正确的字符串,但我希望我的代码在没有 Gnome 的系统上工作。

So, what's the best general way to get the currently configured timezone as an Olson location, on Linux?

那么,在 Linux 上将当前配置的时区作为 Olson 位置的最佳通用方法是什么?

回答by psmears

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

这是很难得到一个可靠的答案。依靠之类的东西/etc/timezone可能是最好的选择。

(The variable tznameand the tm_zonemember of struct tm, as suggested in other answers, typically contains an abbreviation such as GMT/BSTetc, rather than the Olson time string as requested in the question).

(正如其他答案中所建议的那样,变量tzname和 的tm_zone成员struct tm通常包含一个缩写,例如GMT/BST等,而不是问题中要求的 Olson 时间字符串)。

  • On Debian-based systems (including Ubuntu), /etc/timezoneis 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.
  • 在基于 Debian 的系统(包括 Ubuntu)上,/etc/timezone是一个包含正确答案的文件。
  • 在某些基于 Redhat 的系统(至少包括 CentOS、RHEL、Fedora 的某些版本)上,您可以使用 readlink() on 获取所需信息/etc/localtime,它是(例如)的符号链接/usr/share/zoneinfo/Europe/London
  • OpenBSD 似乎使用与 RedHat 相同的方案。

However, there are some issues with the above approaches. The /usr/share/zoneinfodirectory also contains files such as GMTand GB, so it's possible the user may configure the symlink to point there.

但是,上述方法存在一些问题。该/usr/share/zoneinfo目录还包含诸如GMTand 之类的文件GB,因此用户可以将符号链接配置为指向那里。

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:

解决此问题的一种可能性(似乎适用于 Debian、RedHat 和 OpenBSD)是将 /etc/localtime 文件的内容与 /usr/share/zoneinfo 下的文件进行比较,并查看哪些匹配:

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 disadvantageis 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/timezoneif it exists; otherwise try parsing /etc/localtimeas a symlink; if that fails, search for matching timezone definition files; if that fails - give up and go home ;-)

最好的办法可能是结合上述方法:/etc/timezone如果存在就使用;否则尝试解析/etc/localtime为符号链接;如果失败,搜索匹配的时区定义文件;如果失败 - 放弃并回家;-)

(And I have no idea whether any of the above applies on AIX...)

(而且我不知道上述任何一项是否适用于 AIX ......)

回答by Evan Teran

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

对此没有标准的c 或 c++ 函数。但是,GNU libc 有一个扩展。它struct tm有两个额外的成员:

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

这意味着,如果您使用填充 a struct tm(例如localtimegmtime)的函数之一,您可以使用这些额外的字段。这当然只有在您使用 GNU libc(以及它的足够新版本)时才适用。

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:

许多系统也有一个int gettimeofday(struct timeval *tv, struct timezone *tz);函数(POSIX),它会填写一个struct timezone. 这具有以下字段:

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

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

不完全是你所要求的,但关闭...

回答by Howard Hinnant

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

我一直在开发一个免费的开源 C++11/14 库,它在一行代码中解决了这个问题:

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:

它旨在跨所有最新版本的 Linux、macOS 和 Windows 移植。对我来说,这个程序输出:

America/New_York

If you downloadthis library, and it doesn't work you, bug reportsare welcome.

如果你下载了这个库,但它对你不起作用,欢迎报告错误

回答by Duane McCully

I see two major linux cases:

我看到两个主要的 linux 案例:

  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"
  1. 乌班图。应该有一个 /etc/timezone 文件。此文件应仅包含时区,不包含其他任何内容。
  2. 红帽。应该有一个 /etc/sysconfig/clock 包含如下内容: ZONE="America/Chicago"

In addition, Solaris should have an /etc/TIMEZONE file that contains a line like: TZ=US/Mountain

此外,Solaris 应该有一个 /etc/TIMEZONE 文件,其中包含如下一行: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).

因此,基于上述内容,我相信这里有一些直接的 C 可以回答 OP 的问题。我已经在 Ubuntu、CentOS(红帽)和 Solaris(额外奖励)上对其进行了测试。

#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;
}

回答by sumwale

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

当天很晚,但我正在寻找类似的东西,发现 ICU 库提供了获取奥尔森时区 ID 的规定:http: //userguide.icu-project.org/datetime/timezone

It is now installed on most linux distributions (install the libicu-dev package or equivalent). Code:

它现在安装在大多数 linux 发行版上(安装 libicu-dev 包或同等版本)。代码:

#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):

并获取缩写/POSIX 时区名称(也应该适用于 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;

回答by Almir de Oliveira Duarte Jr.

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

我喜欢 psmears 发表的帖子,并实现了这个脚本来读取列表的第一个输出。当然,必须有更优雅的方法来做到这一点,但是你...

    /**
     * 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

在我的巴西 Fedora 12 中,它返回:
Brazil/East

And does exactly what I need.

并且正是我所需要的。

Thank you psmears

谢谢你

回答by Serge Wautier

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

FWIW,RHEL/Fedora/CentOS 有/etc/sysconfig/clock

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

回答by user361446

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

这是适用于大多数 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;
} 

回答by Tobu

The libc accesses the Olson database when tzsetis called, and uses simplified time zones afterwards. tzsetlooks at the TZenvironment variable first, and falls back to parsing the binary data in /etc/localtime.

libc 在调用tzset时访问 Olson 数据库,然后使用简化的时区。 tzset首先查看TZ环境变量,然后回退到解析/etc/localtime.

At first systemdstandardised 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}.

起初systemd标准化在具有在奥尔森时区名称/etc/timezoneDebian的风格。在 systemd 190 和 /usr 合并之后,systemd 只读取和更新/etc/localtime,额外要求文件是/usr/share/zoneinfo/${OLSON_NAME}.

Looking at TZ, then readlink("/etc/localtime"), is the most reliable way to match the libc's tzsetlogic 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.

TZ那么readlink("/etc/localtime"),查看是匹配 libctzset逻辑并仍然保留符号 Olson 名称的最可靠方法。对于不遵循 systemd 符号链接约定的系统,阅读/etc/timezone(并可能检查/usr/share/zoneinfo/$(</etc/timezone)与 相同/etc/localtime)是一个很好的后备。

If you can live without symbolic names, parsingthe /etc/localtimetzfileis 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).

如果你生活中可以没有符号名称,解析/etc/localtimetzfile是便携,因为它得到,虽然复杂得多。仅读取最后一个字段会为您提供 Posix 时区(例如:)CST5CDT,M3.2.0/0,M11.1.0/1,它可以与一些时间处理库互操作,但会删除一些元数据(无历史转换信息)。

回答by torak

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

根据页面,看起来您#include <time.h>是否会声明以下内容。

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

Does that give you the information that you need?

这是否为您提供了所需的信息?