簡體   English   中英

來自boost posix_time的意外結果

[英]Unexpect result from boost posix_time

當我在我的ubuntu中運行它時,我得到了最低結果。 怎么會出錯呢? 結果年份是1970年沒有辦法,並且看我何時重新轉換ms ,結果不一樣。 但是當我在http://coliru.stacked-crooked.com/上運行時,它很好。

#include <boost/date_time/gregorian/gregorian.hpp>
#include <boost/date_time/posix_time/posix_time.hpp>

#include <string>
#include <iostream>

int main()
{
    using namespace boost::posix_time;
    ptime time_t_epoch(boost::gregorian::date(1970,1,1));

    auto ms = (boost::posix_time::microsec_clock::local_time() - time_t_epoch).total_microseconds();
    std::cout << "ms: " << ms << std::endl;
     std::cout << "ms: " << boost::posix_time::microseconds(ms).total_microseconds() << std::endl;
    ptime newTime = time_t_epoch + boost::posix_time::microseconds(ms);
    std::cout << boost::posix_time::to_iso_string(time_t_epoch) << std::endl;
    std::cout << boost::posix_time::to_iso_string(newTime) << std::endl;
    std::stringstream ss;
    ss << newTime;
    std::cout <<  ss.str()<< std::endl;
}

結果:

ms: 1424976249761218

ms: 4576956085582

19700101T000000

19700222T232236.085582

1970-Feb-22 23:22:36.085582

機器環境= Ubuntu 12 + cmake 2.8.7 +提升1.48.0.2

1970/01/01 + 4576956085582微秒 - > 1970年2月22日是正確的

1970/01/01 + 1424976249761218微秒 - > 2015年2月26日所以ms大概是正確的

不正確的是

boost::posix_time::microseconds(ms).total_microseconds() != ms

它可能與這個bug有關https://svn.boost.org/trac/boost/ticket/3487雖然我不確定。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM