如何使用FMT库和Chrono设置秒或精度

发布于 2025-01-24 10:47:40 字数 1760 浏览 0 评论 0原文

我正在尝试使用 fmt 's chrono inpland 并指定 precision 在秒部分中。

我正在阅读文档了解如何做;无论我尝试什么,它要么不按预期格式格式,要么编译器报告错误。

这是我到目前为止尝试的方法:(

#include <chrono>
#include <ratio>
#define FMT_HEADER_ONLY
#include <fmt/chrono.h>
#include <iostream>

int main() {
    std::chrono::duration<std::int64_t, std::nano> const d{3601123456789};
    std::cout << fmt::format("1a {:%H:%M:%S}\n", d);
    std::cout << fmt::format("1_ {: >15%S} <-- width works; precision won't compile\n", d);
    // The fmt docs say that "Precision is valid only for std::chrono::duration types with a floating-point representation type."
    // The following format doesn't compile with "d" whose ticks are represented as integer.
    // It does with "dd" whose ticks are represented as double, but the result is still not as expected.
    std::cout << fmt::format("2_ {:.2%S} <-- this is confusing; precision doesn't work\n", dd);
    std::cout << fmt::format("2_ {: >15.2%S} <-- width works; precision doesn't\n", dd);
}

godbolt link

编辑: 仅在MSVC上使用std ::格式检查; 结果是相同的。

I'm trying to format a duration with fmt's chrono impl and specify the precision for the seconds part.

I'm reading the documentation but think i don't understand how to do it; whatever i try, it either doesn't format as expected or the compiler reports an error.

Here's what i tried so far:

#include <chrono>
#include <ratio>
#define FMT_HEADER_ONLY
#include <fmt/chrono.h>
#include <iostream>

int main() {
    std::chrono::duration<std::int64_t, std::nano> const d{3601123456789};
    std::cout << fmt::format("1a {:%H:%M:%S}\n", d);
    std::cout << fmt::format("1_ {: >15%S} <-- width works; precision won't compile\n", d);
    // The fmt docs say that "Precision is valid only for std::chrono::duration types with a floating-point representation type."
    // The following format doesn't compile with "d" whose ticks are represented as integer.
    // It does with "dd" whose ticks are represented as double, but the result is still not as expected.
    std::cout << fmt::format("2_ {:.2%S} <-- this is confusing; precision doesn't work\n", dd);
    std::cout << fmt::format("2_ {: >15.2%S} <-- width works; precision doesn't\n", dd);
}

(godbolt link)

Edit:
Just checked with std::format on msvc; the results are the same.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

青衫负雪 2025-01-31 10:47:40

要控制宽度,请在“:”之后包括对齐和宽度规范作为第一件事。例如“ {:&gt; 20 ...”与宽度20对齐。

要控制精度,请更改输入持续时间的单位。例如,具有3个小数位数,floor&lt;毫秒&gt;(d)

std::cout << fmt::format("d: {:>20%H:%M:%S}\n", std::chrono::floor<std::chrono::milliseconds>(d));

输出:

d:         00:02:03.456

如果您愿意,可以使用{:&gt; 20%T}代替{:&gt; 20%H:%M:%S}

对于两个分数数字,创建一个百分之一的持续时间很方便:

using centiseconds = std::chrono::duration<std::int64_t, std::centi>;
std::cout << fmt::format("d: {:>20%T}\n", std::chrono::floor<centiseconds>(d));

通过在这里追逐球门柱有点缠绕... ;-)

using centiseconds = chrono::duration<int64_t, centi>;
auto d = 3601123456us;
auto m = floor<minutes>(d);
d -= m;
cout << fmt::format("{}:{:%S}", m/1min, floor<centiseconds>(d)) << '\n';

outputs:

60:01.12

我让这项工作用于指定格式字符串中的精度,但这退出了计时 -格式之前完全类型系统:

auto d = 3601123456.us;
auto m = floor<minutes>(d);
d -= m;
cout << fmt::format("{}:{:0>5.2f}", m/1min, d/1s) << '\n';

输出:

60:01.12

To control the width, include an alignment and width specification as the first thing after the ':'. For example "{:>20..." to say align right with width 20.

To control the precision, alter the units of the input duration. For example to have 3 decimal digits, floor<milliseconds>(d):

std::cout << fmt::format("d: {:>20%H:%M:%S}\n", std::chrono::floor<std::chrono::milliseconds>(d));

Output:

d:         00:02:03.456

If you prefer, you can use {:>20%T} in place of {:>20%H:%M:%S}.

For two fractional digits it would be convenient to create a centiseconds duration:

using centiseconds = std::chrono::duration<std::int64_t, std::centi>;
std::cout << fmt::format("d: {:>20%T}\n", std::chrono::floor<centiseconds>(d));

Getting a little winded from chasing the goalposts here ... ;-)

using centiseconds = chrono::duration<int64_t, centi>;
auto d = 3601123456us;
auto m = floor<minutes>(d);
d -= m;
cout << fmt::format("{}:{:%S}", m/1min, floor<centiseconds>(d)) << '\n';

Output:

60:01.12

I got this working for specifying the precision in the format string, but this exits the chrono-type system completely prior to formatting:

auto d = 3601123456.us;
auto m = floor<minutes>(d);
d -= m;
cout << fmt::format("{}:{:0>5.2f}", m/1min, d/1s) << '\n';

Output:

60:01.12
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文