C 获取跨平台时区列表
我正在用 C 开发一个程序,它需要返回计算机可以访问信息的所有可能区域的字符串数组(表面上是来自 tz 数据库)。我需要在 Linux(或至少 Ubuntu)和 Windows 之间实现跨平台,因此两个平台之间的任何通用方法都会让我的生活变得更轻松。我知道有几种在 Windows 上执行此操作的方法(通过注册表或此处描述的方法 http://msdn.microsoft.com/en-us/library/ms171251(v=sql.90).aspx),但我无法除了对数组进行硬编码之外,在 Linux 中找到实现此目的的任何方法。在 Ubuntu 中实现此目的的最佳方法是什么,在 Windows 中是否有比我上面提到的两种更好的方法?
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). 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. 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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Dave,zoneinfo 目录的内容不仅在 Linux 发行版中是一致的,而且在许多(大多数)其他 Unices 中也是一致的。
在某些情况下,确切的位置可能会有所不同(我只知道一个:在 Linux 上,很多很多年前它曾经位于 /usr/lib/zoneinfo 下,当时没有 / 这样的东西) usr/share),因此它应该可以在您的应用程序中进行配置。但您可以使用 /usr/share/zoneinfo 作为默认值,它可以在 99.9% 的情况下工作。
Dave, the contents of the zoneinfo directory is consistent not only across Linux distros, but also across many (most) other 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. But you can use /usr/share/zoneinfo as a default, and it'll work in 99.9% of the cases.
在 Linux 中,遍历
/usr/share/zoneinfo
的内容就足够了吗?In Linux, would walking the contents of
/usr/share/zoneinfo
be sufficient?有一个名为 ICU 的 C++ 库(我知道你说的是 C,但你可以编写一个包装器...),它具有加载时区列表所需的所有函数。
http://icu-project.org/apiref/icu4c/classTimeZone.html
查找此函数以获得完整列表(如果您想以某种方式过滤列表,可以使用其他函数):
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.
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):