Discussion:
[boost][chrono] time_since_epoch different on windows and linux
Ralf Globisch
2011-12-08 18:09:02 UTC
Permalink
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!
Ralf
Klaim - Joël Lamotte
2011-12-08 21:57:58 UTC
Permalink
Hi,

The documentation makes clear that epoch is implementation dependent :

"The library leaves epochs unspecified."
From : http://www.boost.org/doc/libs/1_48_0/doc/html/chrono.html
"In each of the examples above, a different epoch is implied. Sometimes
an epoch has meaning for several millennia. Other times the meaning of an
epoch is lost after a while (such as the start of a timer, or when the
computer booted). However, if two
time_point<http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/reference.html#chrono.reference.cpp0x.time_point_hpp.time_point>s
are known to share the same epoch, they can be subtracted, yielding a valid
duration<http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/reference.html#chrono.reference.cpp0x.duration_hpp.duration>,
even if the definition of the epoch no longer has meaning.

In *Boost.Chrono*, an epoch is a purely abstract and unspecified concept.
There is no type representing an epoch. It is simply an idea that relates
(or doesn't) time_point<http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/reference.html#chrono.reference.cpp0x.time_point_hpp.time_point>s
to a clock, and in the case that they share a clock,
time_point<http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/reference.html#chrono.reference.cpp0x.time_point_hpp.time_point>s
to one another.
time_point<http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/reference.html#chrono.reference.cpp0x.time_point_hpp.time_point>s
associated with different clocks are generally not interoperable unless the
relationship between the epochs associated with each clock is known."
From http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/users_guide.html
My understanding is that chrono isn't meant to provide absolute time
"position", but time measurement and manipulations.
The difference being that epoch is an arbitrary referential used to allow
now() to be written and worked with.

Joël Lamotte
Ralf Globisch
2011-12-09 04:50:08 UTC
Permalink
Post by Klaim - Joël Lamotte
On 08/12/2011 at 23:57, in message
<CAOU91OOrXc_BkOcFt1fK=e+oC5VkC-***@mail.gmail.com>,
Klaim -
Post by Klaim - Joël Lamotte
Hi,
"The library leaves epochs unspecified."
From : http://www.boost.org/doc/libs/1_48_0/doc/html/chrono.html
"In each of the examples above, a different epoch is implied. Sometimes
an epoch has meaning for several millennia. Other times the meaning of an
epoch is lost after a while (such as the start of a timer, or when the
computer booted). However, if two
time_point<http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/reference.htm
Post by Klaim - Joël Lamotte
l#chrono.reference.cpp0x.time_point_hpp.time_point>s
are known to share the same epoch, they can be subtracted, yielding a valid
duration<http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/reference.html#
Post by Klaim - Joël Lamotte
chrono.reference.cpp0x.duration_hpp.duration>,
even if the definition of the epoch no longer has meaning.
In *Boost.Chrono*, an epoch is a purely abstract and unspecified concept.
There is no type representing an epoch. It is simply an idea that relates
(or doesn't)
time_point<http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/reference.htm
Post by Klaim - Joël Lamotte
l#chrono.reference.cpp0x.time_point_hpp.time_point>s
to a clock, and in the case that they share a clock,
time_point<http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/reference.htm
Post by Klaim - Joël Lamotte
l#chrono.reference.cpp0x.time_point_hpp.time_point>s
to one another.
time_point<http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/reference.htm
Post by Klaim - Joël Lamotte
l#chrono.reference.cpp0x.time_point_hpp.time_point>s
associated with different clocks are generally not interoperable unless the
relationship between the epochs associated with each clock is
known."
Post by Klaim - Joël Lamotte
From
http://www.boost.org/doc/libs/1_48_0/doc/html/chrono/users_guide.html
Post by Klaim - Joël Lamotte
My understanding is that chrono isn't meant to provide absolute time
"position", but time measurement and manipulations.
The difference being that epoch is an arbitrary referential used to allow
now() to be written and worked with.
Joël Lamotte
Thanks a lot for the clarification Joël, I had read the quoted section,

but interpreted it incorrectly.

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.
Vicente J. Botet Escriba
2011-12-08 22:20:09 UTC
Permalink
Post by Ralf Globisch
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;
}
Sec: 1323362917 Min: 2.2056e+07 Hours: 367601 Days: 15316.7 Years: 41.9636
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
Ralf Globisch
2011-12-09 04:52:32 UTC
Permalink
"Vicente J.
Post by Ralf Globisch
Post by Ralf Globisch
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;
}
Sec: 1323362917 Min: 2.2056e+07 Hours: 367601 Days: 15316.7 Years: 41.9636
Sec: 82934819 Min: 1.38225e+006 Hours: 23037.4 Days: 959.894
2.62985
Post by Ralf Globisch
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
Post by Ralf Globisch
instead
boost::chrono:time_system_clock::to_time_t which gives you the number
of
Post by Ralf Globisch
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
http://lists.boost.org/mailman/listinfo.cgi/boost-users
Hi Vicente,

That helps a lot, thanks!

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.
Ralf Globisch
2011-12-09 05:21:06 UTC
Permalink
"Vicente J.
Post by Ralf Globisch
Post by Ralf Globisch
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;
}
Sec: 1323362917 Min: 2.2056e+07 Hours: 367601 Days: 15316.7 Years: 41.9636
Sec: 82934819 Min: 1.38225e+006 Hours: 23037.4 Days: 959.894
2.62985
Post by Ralf Globisch
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
Post by Ralf Globisch
instead
boost::chrono:time_system_clock::to_time_t which gives you the number
of
Post by Ralf Globisch
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
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.
Continue reading on narkive:
Loading...