提高微秒

Boost Microseconds

本文关键字:高微秒      更新时间:2023-10-16

我正在做以下事情

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 other改成

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

我得到相同的回显,为什么?

格式不正确,您有太多的.分隔符: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