Boost logo

Boost Users :

Subject: Re: [Boost-users] [date_time] dst_local_start_time
From: Roland Bock (rbock_at_[hidden])
Date: 2008-11-05 09:25:33


I had a similar problem in getting the current wall time in New York. I
wrote the following working function and added a bunch of comments,
since the library is counter-intuitive here IMHO:

// Get local time of day, based on time zone
const boost::posix_time::time_duration
boost::local_time::time_zone_ptr timeZone)
    /* This looks a bit perverted (and it is in my eyes :-))

       Calling time_of_day on a local_date_time object implicitly turns
it into a UTC ptime object first, thus yielding UTC time_of_day instead
of the wall time (I do not really understand why it works this way, but
it does).

       Calling local_time on a local_date_time object returns a ptime
object which in this case represents wall time within the given time
zone (again, I don't know why the behavior is this way, but it is).

Please let me know if that helped or if you found a better



Jean-Pierre Bergamin wrote:
> Hello
> I'm confused about the returned time of the dst_local_start_time()
> function. First of all I don't understand why a ptime is returned and
> not a local_date_time, buy anyway.
> The ptime I get returned for the timezone Europe/Zurich for 2007 is:
> 2007-Mar-25 02:00:00
> Assuming that this is a UTC time, the local time in Zurich would then be
> 2007-Mar-25 04:00:00 (because during summertime, Zurich is 2h ahead of
> UTC), but the correct local dst start time would be 2007-Mar-25 03:00:00
> (because the clocks are shifted from 2 to 3).
> To correctly calculate the dst start time, I have to add the dst_offset
> to the returned time_of_day.
> The returned ptime 2007-Mar-25 02:00:00 seems to be the endtime of
> wintertime as a local time.
> time_zone_ptr zh_tz = tz_db.time_zone_from_region("Europe/Zurich");
> ptime zh_dst_start_time = zh_tz->dst_local_start_time(2007);
> std::cout << zh_dst_start_time << std::endl; // 2007-Mar-25 02:00:00
> local_date_time zh_dst_local_start_time =
> local_date_time(zh_dst_start_time, zh_tz);
> std::cout << zh_dst_local_start_time << std::endl; // 2007-Mar-25
> 04:00:00 CEST which is wrong!
> ptime zh_dst_start_time_ok = zh_dst_start_time + zh_tz->dst_offset();
> local_date_time zh_dst_local_start_time_ok =
> local_date_time(,
> zh_dst_start_time_ok.time_of_day(), zh_tz, true);
> std::cout << zh_dst_local_start_time_ok << std::endl; // 2007-Mar-25
> 03:00:00 CEST which is correct
> So what time exactly is local_start_time() returning?
> Is this a bug or am I missing something?
> Regards
> James
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at