admin管理员组文章数量:1418958
I have this simple code
#include <chrono>
#include <format>
#include <iostream>
int main()
{
auto time = std::chrono::system_clock::now();
std::cout << std::format("Current time is '{0:%T}'.", time) << '\n';
}
it prints:
Current time is '11:47:50.5070349'.
I tested it using / with "x64 msvc v19.latest".
I would like remove the decimal digit at the end of formatting, I see on cpp reference that is possible specify precision... but it is not clear how do it.
I tried {0:.0%T}
and does not works, instead {0:^20%T}
correcly print the time in the center of 20 characters.
I have this simple code
#include <chrono>
#include <format>
#include <iostream>
int main()
{
auto time = std::chrono::system_clock::now();
std::cout << std::format("Current time is '{0:%T}'.", time) << '\n';
}
it prints:
Current time is '11:47:50.5070349'.
I tested it using https://godbolt./ with "x64 msvc v19.latest".
I would like remove the decimal digit at the end of formatting, I see on cpp reference that is possible specify precision... but it is not clear how do it.
I tried {0:.0%T}
and does not works, instead {0:^20%T}
correcly print the time in the center of 20 characters.
3 Answers
Reset to default 3The easiest way to control the precision of the output is to control the precision of the time_point
sent to format
. For example:
#include <chrono>
#include <format>
#include <iostream>
int main()
{
auto time = std::chrono::floor<std::chrono::seconds>(std::chrono::system_clock::now());
std::cout << std::format("Current time is '{:%T}'.", time) << '\n';
}
floor
rounds down. Also available are ceil
and round
for round up and round to nearest respectively.
time_point_cast
also works, but is not recommended for this application because it rounds in an unexpected fashion: towards the epoch. In this example it rounds down for time points after 1970-01-01 UTC and up for time points prior to that epoch.
As we can see here, it is not currently possible specify second's precision on std::formatter<sys_time>. The proposal shows current workaround:
std::cout << std::format("Current time is '{0:%H:%M:%S}'.", std::chrono::time_point_cast<std::chrono::seconds>(time)) << '\n'; //no decimal digit
std::cout << std::format("Current time is '{0:%H:%M:%S}'.", std::chrono::time_point_cast<std::chrono::milliseconds>(time)) << '\n'; // 3 digits
std::cout << std::format("Current time is '{0:%H:%M:%S}'.", std::chrono::time_point_cast<std::chrono::microseconds>(time)) << '\n'; // 6 digits
If you can use your current locale or are willing/able to set the locale, you can use
std::cout << std::format("Current time is '{0:%H:%M:%OS}'.", time) << std::endl;
It will print the seconds without any decimals, tested locally and validated on godbolt:
Program returned: 0
Program stdout
Current time is '13:33:09'.
本文标签: cspecify precision using stdformatter with stdchronosystimeStack Overflow
版权声明:本文标题:c++ - specify precision using std::formatter with std::chrono::sys_time - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745300153a2652323.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
Current time is '11:47:50'
? – doctorlove Commented Jan 29 at 12:03Current time is '11:47:50.50'
... looking better I see that cpp reference says >precision is valid only for std::chrono::duration types where the representation type Rep is a floating-point type," so looks like it is not possible – Perry Commented Jan 29 at 13:19