Boost logo

Boost Users :

Subject: Re: [Boost-users] [boost][chrono] time_since_epoch different on windows and linux
From: Vicente J. Botet Escriba (vicente.botet_at_[hidden])
Date: 2011-12-08 17:20:09


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 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