c ++如何在考虑夏令时的情况下找到国外的时间?

2022-01-16 00:00:00 timezone c++ boost

比如说,如果纽约 (EST) 是 13:00,那么新西兰 (NZST) 就是 06:00.如果新西兰进入夏令时,那么当纽约时间为 13:00(仍为 EST)时,新西兰(现为 NZDT)将是 07:00.

Say, if it's 13:00 at new york (EST) , then it's 06:00 at new zealand (NZST). If new zealand goes into summer time, then when it's 13:00 at new york (still EST), it's going to be 07:00 at new zealand (now NZDT).

我阅读了升压时间库,但在我看来,必须自己确定夏令时规则才能从本地时间"的角度找出外国的时间..

I read the boost time library, but it seems to me one has to determine the daylight saving rules oneself to find out the time in foreign country from the 'localtime' perspective..

例如

 nyc_string = "EST-05:00:00EDT+01:00:00,M4.1.0/02:00:00,M10.5.0/02:00:00";
 // above basically defines the daylight saving rule
 time_zone_ptr nyc_2(new posix_time_zone(nyc_string));

 std::cout << "The second zone is in daylight savings from:
 " 
  << nyc_2->dst_local_start_time(2004) << " through "
  << nyc_2->dst_local_end_time(2004) << std::endl;

来源:http://www.boost.org/doc/libs/1_39_0/doc/html/date_time/examples.html

也许有些事情我还不知道?boost 是否使用任何跟踪夏令时规则的数据库?我想知道是否有一种很好的方法可以在 c++ 中将本地时间调整为不同的时区,同时考虑到夏令时规则.如果我能举个例子,那就太好了!

Maybe there's something I'm not yet aware of? Does boost use any database that keeps track of daylight saving rules? I wonder if there's a nice way to adjust local time to different time zone in c++, taking the daylight saving rules into account..If I could have an example, that'd be so great!

推荐答案

Boost.DateTime 有一个名为 date_time_zonespec.csv 的时区数据库,位于 libs/date_time/data 中.飞行时间示例 显示了如何访问和使用它.此数据库不包含时区更改的历史记录.似乎也没有托管此数据库更新的地方(除了 Boost 库本身).

Boost.DateTime has a timezone database named date_time_zonespec.csv located in libs/date_time/data. The Flight Time Example in the documentation shows how to access and use it. This database doesn't contain the history of timezone changes. There also doesn't seem to be a place hosting updates to this database (other than the Boost library itself).

如果您需要准确、最新的时区数据,请查看 IBM 流行的 ICU 国际化库的 ICU 时区类.如更新时区数据 部分:

If you need accurate, up-to-date, time zone data, then check out the ICU Time Zone Classes of the popular ICU internationalization library from IBM. As mentioned in the Updating the Time Zone Data section:

ICU 中的时区数据由行业标准 TZ 生成使用 tzcode 的数据库(http://source.icu-project.org/repos/icu/icu/trunk/source/tools/tzcode/) 工具.具有最近时区数据的 ICU 数据文件可以从更新网址,http://source.icu-project.org/repos/icu/data/trunk/tzdata/icunew .

The time zone data in ICU is generated from the industry-standard TZ database using the tzcode (http://source.icu-project.org/repos/icu/icu/trunk/source/tools/tzcode/ ) tool. The ICU data files with recent time zone data can be downloaded from the update URL, http://source.icu-project.org/repos/icu/data/trunk/tzdata/icunew .

ICU 时区数据库源自 tz 数据库,现在由 ICANN.

The ICU timezone database is derived from the tz database now maintained by ICANN.

要在 C++ 程序中通过 http 下载文件,可以使用 libcurl 或 cURLpp C++ 包装器.在您的操作系统上设置调度程序以定期下载最新数据库可能会更容易.

To download a file via http in a C++ program, you can use libcurl or the cURLpp C++ wrapper. It might be easier to setup a scheduler on your OS to regularly download the latest database.

正如评论中已经提到的,始终使用 UTC 进行存储和业务逻辑.仅出于显示/输入目的与本地时间相互转换.

As already mentionned in the comments, use UTC consistently for storage and business logic. Only convert to/from local times for display/input purposes.

相关文章