提升微秒

时间:2014-05-19 22:15:56

标签: c++ boost time

我正在做以下

using namespace boost::posix_time;
ptime epoch = time_from_string("1970-01-01 00:00:00.000");
ptime other = time_from_string("2011-08-09 17:27:00.000.000");
time_duration const diff = other - epoch;
long long ms = diff.total_microseconds();
cout<<"diff is"<<ms<<endl;

我得到的回声是     差异是1312910820000000

然后当我将ptime更改为

ptime other = time_from_string("2011-08-09 17:27:00.000.100");

我得到同样的回音,为什么?

1 个答案:

答案 0 :(得分:3)

格式不正确,您有太多.个分隔符: Live On Coliru

#include <iostream>
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>

int main() 
{
    using namespace boost::posix_time;
    ptime epoch = from_time_t(0);
    ptime other = time_from_string("2011-08-09 17:27:00.000000");
    std::cout << "diff is " << (other - epoch).total_microseconds() << std::endl;

    other = time_from_string("2011-08-09 17:27:00.00001");
    std::cout << "diff is " << (other - epoch).total_microseconds() << std::endl;
}

打印

diff is 1312910820000000
diff is 1312910820000010