Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
617 views
in Technique[技术] by (71.8m points)

boost - c++ How to find the time in foreign country taking into account daylight saving?

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

e.g.

 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;

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

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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

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:

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 .

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

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.

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...