|
Boost Users : |
Subject: Re: [Boost-users] [DateTime] from time_t to ptime and back - or not
From: Christoph Duelli (duelli_at_[hidden])
Date: 2010-08-23 06:40:46
John B. Turpish wrote:
Thank you.
> 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.
[snip]
> 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
Yes, with t = time(0)
t == mktime(localtime(t))
but
t != mktime(gmtime(t))
> 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.
However, that does not really help, because the struct tm that Boost ptime
produces is different from both gmtime and localtime:
DUMP gmtime
t.tm_sec=50
t.tm_min=31
t.tm_hour=9
t.tm_mday=23
t.tm_mon=7
t.tm_year=110
t.tm_wday=1
t.tm_yday=234
t.tm_isdst=0
t.tm_gmtoff=0
t.tm_zone=GMT
diff(time,mktime(gmtime(time)))=3600
DUMP localtime
t.tm_sec=50
t.tm_min=31
t.tm_hour=11
t.tm_mday=23
t.tm_mon=7
t.tm_year=110
t.tm_wday=1
t.tm_yday=234
t.tm_isdst=1
t.tm_gmtoff=7200
t.tm_zone=CEST
diff(time,mktime(localtime(time)))=0
DUMP boost from_time_t -> to_tm
t.tm_sec=50
t.tm_min=31
t.tm_hour=9
t.tm_mday=23
t.tm_mon=7
t.tm_year=110
t.tm_wday=1
t.tm_yday=234
t.tm_isdst=-1
t.tm_gmtoff=0
t.tm_zone=NULL
diff(time,mktime(to_tm(from_time_t(time))))=7200
It is not obvious how to fix the tm obtained from to_tm.
Probably it is easier to fix the resulting time_t.
I guess I will settle for the approach given in
http://lists.boost.org/boost-users/2007/07/29567.php
and stop using from_time_t and to_tm
Best regards
Christoph
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