开发者

Boost C++ date_time microsec_clock and second_clock

开发者 https://www.devze.com 2022-12-09 01:06 出处:网络
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

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::开发者_如何学Pythoncout << "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


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消