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

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

问题描述

在Linux上,我需要找到当前配置的时区为奥尔森的位置。我希望我的(C或C ++)code可以移植到尽可能多的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 /时区包含此信息,但我不认为我可以靠这个文件总是在那里,可以吗?侏儒有一个函数 oobs_time_config_get_timezone()这也返回正确的字符串,但我希望我的code,而不侏儒在系统上运行。

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上最好的一般方式?

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

推荐答案

这是<一个href=\"http://web.archive.org/web/20130209072352/http://www.chronos-st.org/Discovering%20the%20Local%20Time%20Zone--Why%20It%27s%20a%20Hard%20Problem.html\">hard要得到一个可靠的答案。依托之类的东西的/ etc /时区可能是最好的选择。

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

(变量 TZNAME 结构TM tm_zone 成员$ C>,在其他的答案提出,通常包含的缩写,如 GMT / BST 等,而不是奥尔森在这个问题要求的时间字符串)。

(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 /时区是包含正确的答案的文件。

  • 在某些基于红帽的系统(包括CentOS的,RHEL,Fedora的至少某些版本),你的的/ etc /本地时间,这是一个符号链接(例如)的/ usr /共享/ zoneinfo中/欧洲/伦敦

  • 的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 /共享/ zoneinfo中目录还包含文件,如 GMT GB ,所以有可能用户可以配置符号链接指向那里。

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 /本地时间文件的内容比较在/ usr /共享/ 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 /时区(如果存在);否则尝试解析的/ etc /本地时间作为一个符号链接;如果失败,搜索匹配时区定义文件;如果失败 - 放弃回家; - )

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天全站免登陆