以秒为单位获得boost :: posix_time :: time_duration

Eag*_*gle 9 c++ boost

boost::posix_time::ptime用来测量我的模拟运行时间和其他东西.

assuimg

boost::posix_time::ptime start, stop;
boost::posix_time::time_duration diff;
start = boost::posix_time::microsec_clock::local_time();
sleep(5);
stop = boost::posix_time::microsec_clock::local_time();
diff = stop - stop;
Run Code Online (Sandbox Code Playgroud)

现在

std::cout << to_simple_string( diff ) << std::endl;
Run Code Online (Sandbox Code Playgroud)

hh:mm:ss.ssssss格式返回时间,我也想有时间ss.sssssss.

为此,我试过了

boost::posix_time::time_duration::sec_type x = diff.total_seconds();
Run Code Online (Sandbox Code Playgroud)

但是这给了我ss和seconds()return 格式的答案返回归一化的秒数(0..60).

我的问题怎么能在ss.ssssss格式的几秒钟内得到我的模拟时间?

编辑

我能做到:

 std::cout << diff.total_seconds() << "." <<  diff.fractional_seconds() << std::endl;
Run Code Online (Sandbox Code Playgroud)

是否有优雅的东西可以绘制ss.sssssss?

nab*_*lke 15

total_seconds()返回一个long未规范化为0..60s的值.

所以这样做:

namespace bpt = boost::posix_time;

int main(int , char** )
{
    bpt::ptime start, stop;
    start = bpt::microsec_clock::local_time();
    sleep(62);
    stop = bpt::microsec_clock::local_time();

    bpt::time_duration dur = stop - start;

    long milliseconds = dur.total_milliseconds();

    std::cout << milliseconds << std::endl; // 62000

    // format output with boost::format
    boost::format output("%.2f");
    output % (milliseconds/1000.0);
    std::cout << output << std::endl; // 62.00
}
Run Code Online (Sandbox Code Playgroud)