简体   繁体   English

获取C中跨平台的时区列表

[英]Get List of Time Zones Across Platforms in C

I am developing a program in C which needs to return an array of strings of all of the possible regions the computer has access to information from (ostensibly from tz database). 我正在开发一个用C语言编写的程序,该程序需要返回计算机可以访问(可能是tz数据库)信息的所有可能区域的字符串数组。 I need to make this cross-platform between Linux (or at least Ubuntu) and Windows, so any common approaches between the two platforms would make my life much easier. 我需要在Linux(或至少是Ubuntu)和Windows之间建立这种跨平台,因此,两个平台之间的任何通用方法都将使我的生活变得更加轻松。 I am aware of a few ways to do it on Windows (through the registry, or the approach described here http://msdn.microsoft.com/en-us/library/ms171251(v=sql.90).aspx ), but I haven't been able to find any way of doing this in Linux besides hard coding an array in. What would be the best way to accomplish this in Ubuntu, and is there a better way in Windows than the two I mentioned above? 我知道在Windows上有几种方法(通过注册表或此处描述的方法http://msdn.microsoft.com/zh-cn/library/ms171251(v=sql.90).aspx ),但是除了在其中硬编码数组之外,我在Linux上找不到其他方法。在Ubuntu中完成此操作的最佳方法是什么?在Windows中,有没有比我上面提到的两种更好的方法?

Dave, the contents of the zoneinfo directory is consistent not only across Linux distros, but also across many (most) other Unices. 戴夫(Dave),zoneinfo目录的内容不仅在Linux发行版中而且在许多(大多数)其他Unices中都是一致的。

The exact location may be different in some cases (I know only one: on Linux, many-many years ago it used to be under /usr/lib/zoneinfo, when there was no such thing as /usr/share), so it should be configurable in your app. 在某些情况下,确切的位置可能有所不同(我只知道一种:在Linux上,很多年前,它曾经在/ usr / lib / zoneinfo下,而没有/ usr / share这样的东西),所以它应该在您的应用中可配置。 But you can use /usr/share/zoneinfo as a default, and it'll work in 99.9% of the cases. 但是您可以将/ usr / share / zoneinfo用作默认值,并且在99.9%的情况下都可以使用。

There is a C++ library (I know you said C but you can write a wrapper...) called ICU which has all the necessary functions to load the list of timezones. 有一个名为ICU的C ++库(我知道您说过C,但是您可以编写一个包装器...),该库具有加载时区列表的所有必要功能。

http://icu-project.org/apiref/icu4c/classTimeZone.html http://icu-project.org/apiref/icu4c/classTimeZone.html

Look for this function to get a complete list (other functions can be used if you'd like to filter the list in some ways): 查找此函数以获取完整列表(如果您希望以某种方式过滤列表,则可以使用其他函数):

/**
 * Returns an enumeration over all recognized time zone IDs. (i.e.,
 * all strings that createTimeZone() accepts)
 *
 * @return an enumeration object, owned by the caller.
 * @stable ICU 2.4
 */
static StringEnumeration* U_EXPORT2 createEnumeration();

在Linux中,遍历/usr/share/zoneinfo是否足够?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM