Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost C++ date_time microsec_clock and second_clock

I discovered a strange result in Boost C++ date time library. There is inconsistency between microsec_clock and second_clock, and I don't understand why is that. I am using Windows XP 32-bits

My snip of code:

using namespace boost::posix_time;
...
ptime now = second_clock::universal_time();
std::cout << "Current Time is: "<< to_iso_extended_string(now)<< std::endl;
ptime now_2 = microsec_clock::universal_time();
std::cout << "Current Time is: "<< to_iso_extended_string(now_2)<< std::endl;
...

The print-out I expected are current time without miliseconds and with milliseonds. However, what I have in my pc is:

2009-10-14T16:07:38  
1970-06-24T20:36:09.375890

I don't understand why there is a weired date (year 1970???) in my microsec_clock time. Related documentation for Boost: link to boost date time

like image 226
Lily Avatar asked Oct 14 '09 16:10

Lily


3 Answers

Not sure what could be wrong for you; the exact same code works for me.

$ cat > test.cc
#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>
using namespace boost::posix_time;
int main() {
    ptime now = second_clock::universal_time();
    std::cout << "Current Time is: "<< to_iso_extended_string(now)<< std::endl;
    ptime now_2 = microsec_clock::universal_time();
    std::cout << "Current Time is: "<< to_iso_extended_string(now_2)<< std::endl;
    return 0;
}
^D
$ c++ -lboost_date_time test.cc
$ ./a.out
Current Time is: 2009-10-14T16:26:55
Current Time is: 2009-10-14T16:26:55.586295

Implementation-wise, second_clock uses time and microsec_clock uses gettimeofday or GetSystemTimeAsFileTime underneath, depending on the platform. Something appears wrong with your platform -- what is your OS and version?


What is your Boost version? If it is 1.38 or lower, upgrade to 1.39 or apply the fix to #2809 manually.

--- boost/date_time/filetime_functions.hpp  (revision 53621)
+++ boost/date_time/filetime_functions.hpp  (revision 53622)
@@ -96,9 +96,7 @@
     {
         /* shift is difference between 1970-Jan-01 & 1601-Jan-01
         * in 100-nanosecond intervals */
-        const uint64_t c1 = 27111902UL;
-        const uint64_t c2 = 3577643008UL; // issues warning without 'UL'
-        const uint64_t shift = (c1 << 32) + c2;
+        const uint64_t shift = 116444736000000000ULL; // (27111902 << 32) + 3577643008

         union {
             FileTimeT as_file_time;

Windows FileTime has a different offset from UNIX time, and the code that was in Boost before would not generate the correct offset difference in certain optimizing compilers.

like image 72
ephemient Avatar answered Oct 11 '22 03:10

ephemient


The 1970 date most likely comes from the way unix time is represented, as seconds from January 1 1970. I would guess that maybe it is somehow getting the system uptime in milliseconds and interpreting it as seconds since 1/1/1970. An uptime of a little over 4 hours would come up with this date.

like image 21
Dolphin Avatar answered Oct 11 '22 02:10

Dolphin


Unlike second_clock, the microsec_clock::universal_time documentation mentions: Returns the UTC time based on computer settings.
You should check your hardware clock settings (or where-ever microsec gets its values from).

edit:
If its not related to your computers settings it would have to be an misbehaviour in boost, which i highly doubt.

like image 44
Georg Fritzsche Avatar answered Oct 11 '22 01:10

Georg Fritzsche