Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:\n " 
  << 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!

like image 530
user945216 Avatar asked Nov 29 '11 21:11

user945216


People also ask

How do I check my DST time?

Test transition of DST, i.e. when you are currently in summer time, select a time value from winter. Test boundary cases, such as a timezone that is UTC+12, with DST, making the local time UTC+13 in summer and even places that are UTC+13 in winter.

How does UTC handle daylight Savings?

The switch to daylight saving time does not affect UTC. It refers to time on the zero or Greenwich meridian, which is not adjusted to reflect changes either to or from Daylight Saving Time.

What is the function to offset the date for daylight saving in time series?

IsDaylightSavingTime(DateTimeOffset) Indicates whether a specified date and time falls in the range of daylight saving time for the time zone of the current TimeZoneInfo object.

Which of the following are valid concept regarding daylight saving time?

Statements 2 and 3 are correct: The Daylight saving time (DST) or summer time is the practice of advancing clocks during summer months by one hour so that evening daylight lasts an hour longer i.e fully utilizing the surplus sunlight in summers while compensating the short day length in winters.


1 Answers

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.

like image 94
Emile Cormier Avatar answered Sep 21 '22 15:09

Emile Cormier