diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index ab3484e0f92c..3dc1bceeb015 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -451,12 +451,18 @@ struct chrono_formatter { *out++ = '-'; } - s = std::chrono::duration_cast(d); - ms = std::chrono::duration_cast(d - s); - if (is_floating_point) { + + if constexpr (is_floating_point) { + auto tmpseconds = std::chrono::duration_cast(d); + s=seconds{std::floor(tmpseconds.count())}; + ms = std::chrono::duration_cast(tmpseconds - s); if (!std::isfinite(s.count()) || !std::isfinite(ms.count())) { FMT_THROW(format_error("internal overflow of floating point duration")); } + } else { + s = std::chrono::duration_cast(d); + ms = std::chrono::duration_cast(d - s); + } } diff --git a/test/chrono-test.cc b/test/chrono-test.cc index a5cada192663..dc8117dd1b65 100644 --- a/test/chrono-test.cc +++ b/test/chrono-test.cc @@ -334,6 +334,6 @@ TEST(ChronoTest, OverflowingFloat) { TEST(ChronoTest, OverflowingFloat2) { const std::chrono::duration d{1.79400457e+31f}; - EXPECT_THROW(fmt::format("{:%S}", d), fmt::format_error); + fmt::format("{:%S}", d); } #endif // FMT_STATIC_THOUSANDS_SEPARATOR