加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c 11 – 如何将std :: chrono :: time_point转换为字符串

发布时间:2020-12-16 05:52:46 所属栏目:百科 来源:网络整理
导读:如何将std :: chrono :: time_point转换为字符串? 例如:“201601161125”. 解决方法 最灵活的方法是将其转换为struct tm,然后使用strftime(就像sprintf一样).就像是: std::chrono::system_clock::time_point now = std::chrono::system_clock::now();std:
如何将std :: chrono :: time_point转换为字符串?
例如:“201601161125”.

解决方法

最灵活的方法是将其转换为struct tm,然后使用strftime(就像sprintf一样).就像是:
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_c = std::chrono::system_clock::to_time_t(now);
std::tm now_tm = *std::localtime(&now_c);
/// now you can format the string as you like with `strftime`

查找strftime here的文档.

如果您有localtime_s或localtime_r可用,则应优先使用localtime.

还有很多其他方法可以做到这一点,但是,虽然大多数更容易使用,但这些方法会产生一些预定义的字符串表示.您可以在功能中“隐藏”上述所有内容以方便使用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读