2009-10-14 162 views
6

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

我的代码剪断:

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 

我不明白为什么会出现在我的microsec_clock时间是weired日期(1970年???)。为加速相关文档:link to boost date time

回答

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时间偏移,而且在加速前不会产生一定的优化编译器正确的偏移差的代码。

+0

我正在使用Win32系统,Windows XP SP2 32位准确。 – Lily 2009-10-14 16:48:27

+0

我已经在Eclipse 3.4.1和MingW 3.4中使用了1.39。此外,我有警告:说明\t \t资源路径\t \t位置类型 C:\t类型CommercialDetection线101 \t C/C的/boost/boost_1_39/boost/date_time/filetime_functions.hpp左移位计数> =宽度++问题如以及 – Lily 2009-10-14 19:15:51

+0

嗯,我认为这个修正是在1.39,但我可以仔细检查。 – ephemient 2009-10-14 19:19:10

1

1970年的日期最有可能来自于这样unix time表示,如秒内从1月1日1970年我猜想,也许这是某种越来越以毫秒为单位的系统正常运行时间,并自其解释为秒1/1/1970。这个日期会有4个多小时的正常运行时间。

1

不像second_clock,将microsec_clock::universal_time文档中提到:返回基于计算机设置 UTC时间。
你应该检查你的硬件时钟设置(或者从哪里得到它的数据)。

编辑:
如果它不涉及您的计算机的设置,将必须在提升的不当行为,这我很怀疑。

+0

非常好的一点,我也发现这个: 使用亚秒分辨率时钟获取UTC时间。在Unix系统上,这是使用GetTimeOfDay实现的。在大多数Win32平台上,它使用ftime来实现。通过这个API,Win32系统通常不会达到微秒级的分辨率。如果更高的分辨率对您的应用至关重要,请测试您的平台以查看实现的分辨率 ===> 我使用的是Win32系统,所以在我的电脑中可能没有这样的分辨率。这可能是原因。然而,打印输出确实给了我一个日期,那么这些数字从哪里来呢?... – Lily 2009-10-14 16:46:51

+0

如果我看到它,它会从当前日期生成一个time_type,并在create_time()中生成ftime()。 – 2009-10-14 16:59:10

+0

如果ftime()不支持亚秒级分辨率,我预计会失去高分辨率并回退到次最佳分辨率。 – 2009-10-14 17:07:24

相关问题