有没有简便的方法可以格式化std::chrono::duration
为指定格式?
std::chrono::high_resolution_clock::time_point now, then;
then = std::chrono::high_resolution_clock::now();
// ...
now = std::chrono::high_resolution_clock::now();
auto duration = now - then;
// base in microseconds:
auto timeInMicroSec =
std::chrono::duration_cast<std::chrono::microseconds>(duration);
如何设置格式timeInMicroSec
一样ss::ms::us
?
一个人可以使用类似:
#include <iomanip>
#include <sstream>
//...
auto c(timeInMicroSec.count());
std::ostringstream oss;
oss << std::setfill('0') // set field fill character to '0'
<< (c % 1000000000) / 1000000 // format seconds
<< "::"
<< std::setw(3) // set width of milliseconds field
<< (c % 1000000) / 1000 // format milliseconds
<< "::"
<< std::setw(3) // set width of microseconds field
<< c % 1000; // format microseconds
auto formatted(oss.str());
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句