Boost logo

Boost Users :

Subject: Re: [Boost-users] [DateTime] from time_t to ptime and back - or not
From: Roland Bock (rbock_at_[hidden])
Date: 2010-08-23 04:08:29


On 2010-08-20 17:23, Christoph Duelli wrote:
> [I am using Boost 1.43.0 on linux, gcc 4.4.1]
>
> I am having trouble with date_time's ptime.
> I am passing it a time_t, and store that in a ptime object.
> When I retrieve the time_t, I am getting a different value than the one I
> passed to it originally.
> (the difference seems to be 2 hours - in all probability due to UTC vs local
> time issues).
>
> 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?

Since struct tm is so weird, here is what I do:

boost::posix_time::ptime epoch(boost::gregorian::date(1970,1,1));
time_t also_now = (pt - epoch).total_seconds();

HTH

Roland


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