简体   繁体   English

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

[英]Does fmt library handle printing std::chrono::time_point?

The documentation for the C++ fmt libary says, about the "chrono format specifiers": 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 .具有日历组件的说明符,例如 'd'(月份中的日期)仅对 std::tm 有效,而对持续时间或时间点无效。

But, I can create std::chrono::time_point and print it using %d and other letters.但是,我可以创建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

Maybe I'm not understanding the docs;也许我不理解文档; I'm not 100% sure about the meaning of some of the terms, like "calendaric component".我不能 100% 确定某些术语的含义,例如“日历组件”。

I just want to print a time point in ISO 8601 format ( YYYY-mm-ddTHH:MM:SSZ ) and for some reason this does not seem to be available in the standard library along with the chrono types.我只想以ISO 8601 格式( YYYY-mm-ddTHH:MM:SSZ ) 打印一个时间点,由于某种原因,这似乎与chrono类型一起在标准库中不可用。

Is printing a time_point (like I did above) supported?是否支持打印time_point (就像我上面所做的那样)?

This part of the documentation is a bit outdated.这部分文档有点过时了。 With recent versions of {fmt} you can use d and similar format specifiers with time_point , eg对于 {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神螺栓

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM