Boost logo

Boost Users :

Subject: Re: [Boost-users] [DateTime] from time_t to ptime and back - or not
From: John B. Turpish (jbturp_at_[hidden])
Date: 2010-08-22 01:10:07


On Fri, Aug 20, 2010 at 11:23 AM, Christoph Duelli <duelli_at_[hidden]> wrote:
> Code:
>  time_t now = time(NULL);
>  boost::posix_time::ptime pt = boost::posix_time::from_time_t(now);
>  struct tm tm = to_tm(pt);
>  time_t should_be_now = mktime(&tm);
>  BOOST_CHECK_EQUAL(now, should_be_now);
>
> fails for me: now is by 7200 seconds (i.e. 2 hours) ahead of.
>
> Is this a bug in date_time or am I just failing to use it properly?
>
I am by no means an expert, so take this with a grain of salt.
I would guess the problem is with your assumptions. The documentation
on to_tm (http://www.boost.org/doc/libs/1_44_0/doc/html/date_time/posix_time.html#ptime_struct_tm)
says nothing about timezones, and a ptime stores UTC, so I'd guess the
struct tm members would be set to what that point in time would be in
UTC. mktime, on the other hand, assumes the tm represents your local
time (http://cplusplus.com/reference/clibrary/ctime/mktime/).
Whether I'm right or wrong should be easily testable, especially for
you (since you know what timezone your PC is set to). Add to your
program a few output lines showing members of the tm and see if it
looks right for your local time.


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net