10

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!

user945216
  • 251
  • 3
  • 14
  • 11
    Just say no and do everything in UTC. Then when absolutely needed (maybe for human output) convert between UTC and localtime. – Mark B Nov 29 '11 at 21:36
  • FWIW, New York and New Zealand are currently never on EST and NZST simultaneously. Daylight saving time overlaps on both ends, so either both are on EDT/NZDT or just one is. I totally agree with Mark B, just say no. – Greg Hewgill Nov 29 '11 at 21:39
  • Sorry, it was just a silly example that i had to come up in order to explain, and thanks for correcting it. By the way, I'm in a situation where i really have to change local time to different time zones, and can't just stick to UTC. Is there any other suggestions? Will be very much appreciated, – user945216 Nov 29 '11 at 21:42
  • Mark B does have a point though. Time zones and daylight saving time are the biggest piece of crap you can get involved with. It is a wise move to stay away from them and do everything in UTC. Although we live in the 21st century, governments are still not able to do something trivial as to switch daylight saving time the same day in different countries within the same time zone... so you can (will) have different outcomes this week and the next week. – Damon Nov 29 '11 at 21:49
  • But my task is to change local time to different time zones without having to change configuration every time a daylight saving change occurs.. So does it mean that there's no library in this world which I can use in order to keep track of this data? – user945216 Nov 29 '11 at 21:53
  • you can find a web service that will provide you with local time stamp. There were a number of them mentioned here: http://stackoverflow.com/questions/55901/web-service-current-time-zone-for-a-city – Gene Bushuyev Nov 29 '11 at 22:07
  • 1
    @user - Not only is there a lack of synchronization between countries, some of them also succeed in changing their rules from year to year. Sorry! – Bo Persson Nov 29 '11 at 23:26
  • 1
    Gee, I hope you guys don't work for an airline. If they wanted you to display arrival times in the destination's time zone, would you just throw your hands in the air and say: "No, it's too hard. Let's just display UTC and let ordinary people figure it out." – Emile Cormier Nov 30 '11 at 00:47
  • @Emile Cormier: The logic still holds: You do *all* your storage and calculations in UTC, as it will just make your life easier. (Such as calculating flight time: `time1 - time2` works in UTC, but your head explodes if you have to convert two times to UTC just to do the calculations.) Displaying "arrival time" is a case where the user wants to see it in local (sometimes: I'm perpetually doing TZ conversions on airline times when I send them to my girlfriend). Note though, you need to know where the airport is (and when, over the general case), which is data you have to have. – Thanatos Nov 30 '11 at 01:28
  • @Thanatos: I agree 100% about storage/calculations in UTC, but no one seemed to consider the OP's use case as legitimate and they sidestepped the question entirely. Its not so far-fetched that some transportation-related software would need to input/output a local time in some time zone different than the user's local time zone. – Emile Cormier Nov 30 '11 at 01:59
  • If you're going to display local times for arrivals and departures, it isn't enough to use rules that apply in most of a country. You'll have to look at rules that apply in each locality where an arrival or departure takes place. You might find that your own country's rules are more foreign than some foreign countries. – Windows programmer Nov 30 '11 at 02:03
  • @Windowsprogrammer: I though that ICANN's tz database maintained all such rules for each locality. Does it not? – Emile Cormier Nov 30 '11 at 02:25
  • @EmileCormier: Ah, yes, you're right: there is a bit of question side-stepping. I think it might have been the NYC→New Zealand, which normally would be NYC→UTC (store) (load) UTC→NZ – Thanatos Nov 30 '11 at 08:17

1 Answers1

8

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.

Emile Cormier
  • 28,391
  • 15
  • 94
  • 122