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

吴世亨

如何转换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_slocaltime_r可用,则应优先使用localtime

还有许多其他方法可以执行此操作,但是虽然大多数情况下更易于使用,但它们会产生一些预定义的字符串表示形式。您可以将以上所有内容“隐藏”在一个函数中,以易于使用。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

C ++如何将std :: chrono :: time_point转换为long并返回

std :: string到std :: chrono time_point

将std :: chrono :: system_clock :: time_point转换为struct timeval并返回

如何将'std :: chrono :: duration <int,std :: ratio <2629746l,1l>>'转换为'int'类型?

将chrono :: duration转换为字符串或C字符串

将time_point转换为字符串的最漂亮方法是什么?

如何将`std :: chrono :: milliseconds`转换为`boost :: posix_time :: milliseconds`

如何将chrono :: format :: strftime静态转换为chrono :: format :: Item`?

如何将std :: chrono :: high_resolution_clock :: now()转换为毫秒,微秒,...?

std :: string中的std :: chrono :: time_point

将boost :: chrono :: steady_clock :: time_point转换为std :: chrono :: steady_clock :: time_point

无法在std :: chrono :: time_point之间转换

如何获取std :: chrono :: time_point时钟类型

从变量转换时std :: chrono :: time_point编译器错误

错误:从std :: chrono :: time_point浮点数转换为非标量类型long int

如何将std :: chrono :: duration转换为double(秒)?

从std :: chrono :: time_point转换后,C ++ std :: tm返回错误的值

std :: adjacent_difference与std :: chrono time_point

std :: chrono :: time_point线程安全吗?

C ++无法将chrono :: duration添加到chrono :: time_point

使用chrono将time_point转换为特定持续时间

如何将YYYY / MM / DD HH:MM:SS转换为std :: chrono :: system_clock :: time_point?

如何将“std::chrono::system_clock::now()”转换为双倍

如何将 std::chrono::high_resolution 类型转换为浮点类型?

将 std::chrono::milliseconds 转换为 ISO 8601 字符串

使用 std::format 将 time_point 转换为字符串,格式包括日期、时间和亚秒

std::chrono::from_stream 可以以微秒精度将字符串转换为 time_point 吗?

如何在 C++ 中将 Windows FILETIME 转换为 std::chrono::time_point<std::chrono::file_clock>

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