如何找到当前的系统时区? [英] How do I find the current system timezone?

查看:27
本文介绍了如何找到当前的系统时区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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 和 Ubuntu 有一个文件 /etc/timezone 包含此信息,但我认为我不能依赖该文件始终存在,可以吗?Gnome 有一个函数 oobs_time_config_get_timezone() 也返回正确的字符串,但我希望我的代码在没有 Gnome 的系统上工作.

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.

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

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

推荐答案

很难得到可靠的答案.依赖像 /etc/timezone 这样的东西可能是最好的选择.

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

(如其他答案中所建议的,变量 tznamestruct tmtm_zone 成员通常包含缩写,例如 GMT/BST 等,而不是问题中要求的 Olson 时间字符串.

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

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

但是,上述方法存在一些问题./usr/share/zoneinfo 目录还包含 GMTGB 等文件,因此用户可以配置符号链接指向那里.

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.

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

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

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

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

(而且我不知道以上是否适用于 AIX...)

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

这篇关于如何找到当前的系统时区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆