【问题标题】:C++20 format sys_time with milliseconds precision毫秒精度的 C++20 格式 sys_time
【发布时间】:2022-01-20 16:45:30
【问题描述】:

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

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

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

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

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

【问题讨论】:

    标签: c++ c++20 chrono fmt


    【解决方案1】:
    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-28
      • 2014-08-07
      • 2011-02-23
      • 2014-03-20
      • 2015-06-25
      • 1970-01-01
      相关资源
      最近更新 更多