documentation对于 C++ fmt 库说,关于“计时格式说明符”:
Specifiers that have a calendaric component such as 'd' (the day of month) are valid only for std::tm and not durations or time points.
但是,我可以创建 std::chrono::time_point
并使用 %d 和其他字母打印它。
using std::chrono::system_clock;
using std::chrono::time_point;
time_point<system_clock> t = system_clock::now();
fmt::print("{:%Y-%m-%d %H:%M:%S}", t);
// Prints: 2022-09-08 21:27:08
也许我不理解文档;我不是 100% 确定某些术语的含义,例如“日历组件”。
我只想打印ISO 8601 format中的一个时间点(YYYY-mm-ddTHH:MM:SSZ
) 并且出于某种原因,这在标准库中似乎与 chrono
类型一起可用。
是否支持打印 time_point
(就像我上面做的那样)?
最佳答案
这部分文档有点过时了。在最新版本的 {fmt} 中,您可以将 d
和类似的格式说明符与 time_point
一起使用,例如
#include <fmt/chrono.h>
int main() {
using std::chrono::system_clock;
using std::chrono::time_point;
time_point<system_clock> t = system_clock::now();
fmt::print("{:%Y-%m-%d %H:%M:%S}", t);
}
关于c++ - fmt 库是否处理打印 std::chrono::time_point?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73657049/