如何使用FMT库和Chrono设置秒或精度
我正在尝试使用 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);
}
编辑: 仅在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);
}
Edit:
Just checked with std::format
on msvc; the results are the same.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要控制宽度,请在“:”之后包括对齐和宽度规范作为第一件事。例如
“ {:&gt; 20 ...”
与宽度20对齐。要控制精度,请更改输入持续时间的单位。例如,具有3个小数位数,
floor&lt;毫秒&gt;(d)
:输出:
如果您愿意,可以使用
{:&gt; 20%T}
代替{:&gt; 20%H:%M:%S}
。对于两个分数数字,创建一个百分之一的持续时间很方便:
通过在这里追逐球门柱有点缠绕... ;-)
outputs:
我让这项工作用于指定格式字符串中的精度,但这退出了计时 -格式之前完全类型系统:
输出:
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)
:Output:
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:
Getting a little winded from chasing the goalposts here ... ;-)
Output:
I got this working for specifying the precision in the format string, but this exits the chrono-type system completely prior to formatting:
Output: