如何格式化std :: chrono持续时间?

里昂22

有没有简便的方法可以格式化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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何格式化持续时间

如何格式化持续时间

std :: min of std :: chrono ::不同类型的持续时间

检查std :: chrono持续时间小于0的惯用方式

std :: chrono time_since_epoch的默认持续时间

如何使用Rust chrono获得1天的持续时间?

如何使用Chrono返回持续时间类型

如何将 std::chrono::duration 转换为提升持续时间

尝试强制转换std :: chrono :: duration给出“ rep不能是持续时间”编译错误

使用std :: chrono进行计数的持续时间应该很长时会给出0纳秒

区分具有相同类型和比率的std :: chrono持续时间?

如何计算两个chrono :: DateTime之间的持续时间?

如何使用Howard Hinnant的date.h单位打印“ chrono”持续时间?

如何从<chrono>中获取持续时间(以毫秒为单位)和浮点秒?

如何在Java中格式化持续时间?(例如,格式H:MM:SS)

如何格式化Momentjs持续时间以使其易于阅读?

如何在Python(timedelta)中格式化持续时间?

如何使用Moment.js完全格式化持续时间?

时间跨度或持续时间格式

在Excel中格式化时间(持续时间)

如何使动画的持续时间动态化?

Moment js格式持续时间

持续时间的条件格式公式

如何在Lua中将ISO 8601持续时间转换为格式化的字符串?

如何使用GNU工具而不是结果日期来计算和格式化日期持续时间?

如何存储SSIS包的持续时间

如何检测30秒持续时间

如何不舍入持续时间

如何设置音频持续时间