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.

Share Improve this question asked Jan 29 at 11:54 PerryPerry 1,2172 gold badges12 silver badges23 bronze badges 2
  • Do you mean you want Current time is '11:47:50'? – doctorlove Commented Jan 29 at 12:03
  • 1 Correct, or Current 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
Add a comment  | 

3 Answers 3

Reset to default 3

The 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