Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

%S ignores precision for chrono durations where the decimal part should have been only zeros #3794

Closed
erikbs opened this issue Jan 9, 2024 · 1 comment · Fixed by #3814
Closed

Comments

@erikbs
Copy link

erikbs commented Jan 9, 2024

Any chrono duration n where 0 < n < 10⁻ formatted with {:.N%S} receives full precision instead of N decimals. This also holds if adding any number of whole seconds. In other words, any duration where the decimal part should have been rounded to .0, .00, .000 etc. will be printed with all decimals. For example, with {:.2%S}, 4567 milliseconds are printed as 04.56, while 4007 milliseconds are printed as 04.007 (instead of the expected 04.00). 4007000 microseconds are printed as 04.007000 while 4017000 microseconds are printed as 04.01.

If the decimal part is exactly zero (i.e., the duration is a whole number of seconds), there will be one zero more than the specified precision. Thus, 1000 milliseconds will be printed as 01.00 with {:.1%S}, 01.000 with {:.2%S} etc., whilst 1100 milliseconds will be printed as 01.1, 01.10 etc. (as expected) for the same format strings.

Code to reproduce:

#include <fmt/chrono.h>
#include <fmt/core.h>

int main()
{
  constexpr std::chrono::microseconds works{10'001};
  constexpr std::chrono::microseconds fails{9'999};
  fmt::println("{:.2%S} {:.2%S}", works, fails);
}

Expected output:

00.01 00.00

Actual output:

00.01 00.009999

Godbolt:
https://godbolt.org/z/rncqqGWW3

Tested with recent versions of Clang and GCC. Affects both fmt::format and fmt::println.

@vitaut
Copy link
Contributor

vitaut commented Jan 10, 2024

I agree that it's inconsistent although the fractional part should probably be rounded (00.01) not truncated.

netbsd-srcmastr pushed a commit to NetBSD/pkgsrc that referenced this issue Jul 10, 2024
Many improvemnts. Highlights include:

- Added formatters for `std::chrono::day`, `std::chrono::month`,
  `std::chrono::year` and `std::chrono::year_month_day`
- Fixed handling of precision in `%S` (fmtlib/fmt#3794,
- Added a formatter for `std::complex`
- Added a formatter for `std::expected`
- Added a formatter for `std::type_info`

and much faster compile times even without using the new cut down
fmt/base.h which provides the printf() equivalent family of functions.

For a full list see https://github.com/fmtlib/fmt/blob/11.0.1/ChangeLog.md
jperkin pushed a commit to pkgsrc-ci/pkgsrc that referenced this issue Jul 16, 2024
Many improvemnts. Highlights include:

- Added formatters for `std::chrono::day`, `std::chrono::month`,
  `std::chrono::year` and `std::chrono::year_month_day`
- Fixed handling of precision in `%S` (fmtlib/fmt#3794,
- Added a formatter for `std::complex`
- Added a formatter for `std::expected`
- Added a formatter for `std::type_info`

and much faster compile times even without using the new cut down
fmt/base.h which provides the printf() equivalent family of functions.

For a full list see https://github.com/fmtlib/fmt/blob/11.0.1/ChangeLog.md
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants