Boost C++ date_time microsec_clock和second_clock

Lil*_*ily 6 c++ datetime boost utc clock

我在Boost C++日期时间库中发现了一个奇怪的结果.microsec_clock和之间存在不一致second_clock,我不明白为什么会这样.我使用的是Windows XP 32位

我的代码片段:

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;
...
Run Code Online (Sandbox Code Playgroud)

我预期的打印输出是没有毫秒和毫秒的当前时间.但是,我在电脑里面的是:

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

我不明白为什么在我的microsec_clock时间里有一个受欢迎的日期(1970年???).Boost的相关文档:链接到提升日期时间

eph*_*ent 5

不确定你有什么不对劲; 完全相同的代码适合我.

$ 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

在实施方面,second_clock使用timemicrosec_clock使用gettimeofdayGetSystemTimeAsFileTime在下面,取决于平台.您的平台出现了问题 - 您的操作系统和版本是什么?


你的Boost版本是什么?如果是1.38或更低,请升级到1.39或手动将修复程序应用到#2809.

--- 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与UNIX时间具有不同的偏移量,之前Boost中的代码在某些优化编译器中不会生成正确的偏移量差异.