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

Kon*_*rni 3 c++ c++-chrono c++20 fmt

我正在尝试使用 /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);
Run Code Online (Sandbox Code Playgroud)

我尝试了 std::cout << std::format("{0:%d.%m.%Y %H:%M:%S.3}", now);一些可能性,但std::setprecision(3)没有成功。

有没有解决方案可以在那里格式化它,或者我需要更改“ now”吗?

How*_*ant 6

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);
Run Code Online (Sandbox Code Playgroud)

或者:

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));
Run Code Online (Sandbox Code Playgroud)

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

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