c++ - fmt 库是否处理打印 std::chrono::time_point?

标签 c++ c++-chrono fmt

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);
}

godbolt

关于c++ - fmt 库是否处理打印 std::chrono::time_point?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73657049/

相关文章:

c++ - 什么时候将 std::common_type 与单个参数一起使用?

c++ - 如何配置 Qt Creator 以在 centos 6.7 上使用 RH 的 devtoolset-2?

c++ - Qt - 如何从 const 函数追加到本地列表

c++ - 如何成功地将功能对象(或 lambda)传递给轨迹栏回调的第二个参数(void*)?

c++ - std::hash 用于 std::chrono::duration

c++ - 带有编译时格式字符串检查的自定义 {fmt} 格式化函数

c++ - 基类私有(private)部分的虚函数

c++ - 计时: How can I calculate a millsecond duration from two high resolution time points?

c++ - 如何减小可执行文件的大小?

c++ - 在C++ {fmt}中格式化琐碎结构的扩展计划?