diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 5285d420..5f38a8c9 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -1663,15 +1663,16 @@ struct chrono_formatter { if (std::ratio_less::value) { *out++ = '.'; + // Don't convert long double to integer seconds to avoid overflow. + using sec = conditional_t< + std::is_same::value, + std::chrono::duration, std::chrono::seconds>; + auto fractional = detail::abs(d) - std::chrono::duration_cast(d); const auto subseconds = std::chrono::treat_as_floating_point< typename subsecond_precision::rep>::value - ? (detail::abs(d) - - std::chrono::duration_cast(d)) - .count() - : std::chrono::duration_cast( - detail::abs(d) - - std::chrono::duration_cast(d)) + ? fractional.count() + : std::chrono::duration_cast(fractional) .count(); uint32_or_64_or_128_t n = to_unsigned(to_nonnegative_int(subseconds, max_value()));