2009-10-14 32 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

相關問題