Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost][chrono] time_since_epoch different on windows and linux
From: Ralf Globisch (RGlobisch_at_[hidden])
Date: 2011-12-09 00:21:06


>>> On 09/12/2011 at 00:20, in message <4EE13819.20403_at_[hidden]>,
"Vicente J.
Botet Escriba" <vicente.botet_at_[hidden]> wrote:
> Le 08/12/11 19:09, Ralf Globisch a écrit :
>> Hi,
>>
>> I've run into a bit of a funny one and I'm not sure if I'm missing
>> something or whether I've encountered a bug.
>>
>> The following code excerpt *seems* to have 2 different results on
>> windows 7 (VS2010) and linux (gcc4.6) using boost version 1.47.
>>
>> #include<iostream>
>> #include<cstdint>
>> #include<boost/chrono.hpp>
>>
>> int main(int argc, char** argv)
>> {
>> boost::chrono::duration<double> sec =
>> boost::chrono::system_clock::now().time_since_epoch();
>> uint32_t uiSeconds = sec.count();
>> double dMinutes = uiSeconds/60.0;
>> double dHours = dMinutes/60.0;
>> double dDays = dHours/24.0;
>> double dYears = dDays/365.0;
>>
>> std::cout<< "Sec: "<< uiSeconds<<
>> " Min: "<< dMinutes<<
>> " Hours: "<< dHours<<
>> " Days: "<< dDays<<
>> " Years: "<< dYears<< std::endl;
>>
>> return 0;
>> }
>>
>>
>> This outputs the following:
>> Linux:
>> Sec: 1323362917 Min: 2.2056e+07 Hours: 367601 Days: 15316.7 Years:
41.9636
>>
>> Windows:
>> Sec: 82934819 Min: 1.38225e+006 Hours: 23037.4 Days: 959.894
Years:
> 2.62985
>>
>> The linux results is the expected result: 1970 + 41 years = ~2011.
>> Why does the windows result differ? I've run the windows version
>> on two different windows machines with the same result.
>>
>> Am I using the library incorrectly? I'm trying to obtain seconds
since 1970
>> so that I can from there calculate seconds since 1900 to calculate
>> an NTP timestamp.
>>
>> Thanks for any help/suggestions!
> Hi,
>
> time_since_epoch() gives the time since an undetermined epoch. I will
use
> instead
> boost::chrono:time_system_clock::to_time_t which gives you the number
of
> nanoseconds since 1970.
>
> std::time_t sec =
> boost::chrono:time_system_clock::to_time_t(
> boost::chrono::system_clock::now().time_since_epoch()
> );
>
>
> Hth,
> Vicente
>
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users

Have verified that I get the correct time now.
Just a minor correction in case someone
stumbles on this thread and fails to compile:

It should be
std::time_t sec =
     boost::chrono:system_clock::to_time_t(
      boost::chrono::system_clock::now()
     );

Thanks for writing and maintaining this wonderful library Vicente.

Regards,
Ralf

-- 
This message is subject to the CSIR's copyright terms and conditions, e-mail legal notice, and implemented Open Document Format (ODF) standard. 
The full disclaimer details can be found at http://www.csir.co.za/disclaimer.html.
This message has been scanned for viruses and dangerous content by MailScanner, 
and is believed to be clean.

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