如何转换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
此处查找文档。
如果有localtime_s
或localtime_r
可用,则应优先使用localtime
。
还有许多其他方法可以执行此操作,但是虽然大多数情况下更易于使用,但它们会产生一些预定义的字符串表示形式。您可以将以上所有内容“隐藏”在一个函数中,以易于使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句