From 6ecde30513ba0a00acc54feb5d229cb57e1a50f6 Mon Sep 17 00:00:00 2001 From: Stepan Ponomarev Date: Fri, 21 Oct 2022 11:48:32 +0200 Subject: [PATCH] Make write_fractional_seconds more readable --- include/fmt/chrono.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 451bc482..cff225c4 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -1079,9 +1079,10 @@ void write_fractional_seconds(OutputIt& out, Duration d, int precision) { uint32_or_64_or_128_t n = to_unsigned(to_nonnegative_int(subseconds, max_value())); int num_digits = detail::count_digits(n); - int zeroes = std::min(num_fractional_digits - num_digits, precision); + int zeroes = + std::min(std::max(0, num_fractional_digits - num_digits), precision); if (num_fractional_digits > num_digits) out = std::fill_n(out, zeroes, '0'); - int remaining = precision - (zeroes > 0 ? zeroes : 0); + int remaining = precision - zeroes; if (remaining < num_digits) { n /= to_unsigned(detail::pow10(to_unsigned(num_digits - remaining))); out = format_decimal(out, n, remaining).end;