Boost c ++ date_time microsec_clock and second_clock

I found a strange result in the Boost C ++ time-time library. There is an inconsistency between microsec_clock and second_clock , and I do not understand why this is so. I am using 32-bit versions of Windows XP

My snapshot 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 imprint I expected is the current time without miliseconds and with milliseasons. However, I have in my computer:

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

I do not understand why in my microsec_clock time there is a date dragged in (year 1970 ???). Related documentation for Boost: accelerated time link

+6
c ++ boost datetime utc clock
source share
3 answers

Not sure what might be wrong for you; the 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, second_clock uses time and microsec_clock uses gettimeofday or GetSystemTimeAsFileTime at the bottom, depending on the platform. Something is wrong with your platform - what is your OS and version?


What is your version of Boost? If it is 1.38 or lower, upgrade it to 1.39 or apply the hotfix 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 will not generate the correct offset difference in some optimizing compilers.

+5
source share

The 1970 date most likely comes from the unix time path, as seconds since January 1, 1970. I would suggest that perhaps this somehow gets the system uptime in milliseconds and interprets it as seconds since January 1, 1970. This date will be uptime of just over 4 hours.

+1
source share

Unlike second_clock , the documentation for microsec_clock::universal_time is mentioned: Returns UTC time based on computer settings .
You need to check your hardware clock settings (or get your values ​​from somewhere in the microsecond).

edit:
If this is not related to your computer settings, this should be the wrong behavior in boost, and I really doubt it.

+1
source share

All Articles