升C++date_time microsec_clock和second_clock
题
我发现一个奇怪的结果在提高C++日期的时间图书馆。有不一致之处 microsec_clock
和 second_clock
, 我不明白为什么是这样。我使用的Windows XP32位的
我剪断的代码:
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;
...
打印出我的预期是目前的时间而没有毫秒和用milliseonds.但是,我在我的电脑是:
2009-10-14T16:07:38 1970-06-24T20:36:09.375890
我不明白为什么有只不过生活的日期(年1970???) 我 microsec_clock
时间。相关文件提升: 链接,以提高日期的时间
解决方案
不确定什么可能是错误的;这完全相同的代码对我的作品。
$ 猫>test.cc #包括 <boost date_time="" gregorian="" gregorian.hpp=""> #包括 <boost date_time="" posix_time="" posix_time.hpp=""> 使用的名称空间增强::posix_time;int主(){ ptime现在=second_clock::universal_time();std::状 << "目前的时间是:"<< to_iso_extended_string(现在)<< std::endl;ptime now_2=microsec_clock::universal_time();std::状 << "目前的时间是:"<< to_iso_extended_string(now_2)<< std::endl;return0;} ^D $ c++lboost_date_time test.cc $ ./a.出来 Current Time is: 2009-10-14T16:26:55 Current Time is: 2009-10-14T16:26:55.586295
执行明智的, second_clock
使用 time
和 microsec_clock
使用 gettimeofday
或 GetSystemTimeAsFileTime
下面,根据平台。东西出现的错你的平台--什么是您的操作系统版本?
什么是你提升的版本吗?如果这是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时间和代码,这是在提高之前,不会产生正确的偏差在某些优编译器。
其他提示
在1970年日期最有可能来自于路 UNIX时间是表示,如从秒1月1日1970年我猜想,也许这是某种越来越以毫秒为单位的系统正常运行和自1/1/1970将其解释为秒。的一点在4小时内正常运行时间会想出这个日期。
不同于second_clock
,所述microsec_clock::universal_time
文档中提到:返回基于的计算机设置UTC时间强> 的结果,
您应该检查你的硬件时钟设置(或其中的一次微秒从获取其值)。
编辑:结果 如果它不涉及您的计算机的设置,将必须在提升的不当行为,这我很怀疑。