精度为毫秒的 C++20 格式 sys_time

C++20 format sys_time with milliseconds precision

我正在尝试使用 /stc:c++latest 在 MSVC 19.11 中编写具有毫秒精度的时间字符串。

有了这个我得到了 7 位数的精度,这是我不想要的。

auto now = std::chrono::system_clock::now();
std::cout << std::format("{0:%d.%m.%Y %H:%M:%S}", now);

我尝试了 std::cout << std::format("{0:%d.%m.%Y %H:%M:%S.3}", now);std::setprecision(3) 的一些可能性都是徒劳的。

是否有格式化的解决方案,或者我是否需要更改“now”?

auto now = std::chrono::floor<std::chrono::milliseconds>(std::chrono::system_clock::now());
std::cout << std::format("{0:%d.%m.%Y %H:%M:%S}", now);

或:

auto now = std::chrono::system_clock::now();
std::cout << std::format("{0:%d.%m.%Y %H:%M:%S}", std::chrono::floor<std::chrono::milliseconds>(now));

即输出的精度由输入的精度控制。

旁白:如果需要,您也可以使用 %T 代替 %H:%M:%S