diff --git a/include/fmt/chrono.h b/include/fmt/chrono.h index 0f8af080d203..7bfa0352ffde 100644 --- a/include/fmt/chrono.h +++ b/include/fmt/chrono.h @@ -437,6 +437,19 @@ template struct make_unsigned_or_unchanged { using type = typename std::make_unsigned::type; }; +#if FMT_SAFE_DURATION_CAST +// throwing version of safe_duration_cast +template +To fmt_safe_duration_cast(std::chrono::duration from) { + int ec; + To to= safe_duration_cast::safe_duration_cast(from,ec); + if (ec) { + FMT_THROW(format_error("cannot format duration")); + } + return to; +} +#endif + template ::value)> inline std::chrono::duration get_milliseconds( @@ -446,27 +459,14 @@ inline std::chrono::duration get_milliseconds( #if FMT_SAFE_DURATION_CAST using CommonSecondsType = typename std::common_type::type; - int ec; - const auto d_as_common = - safe_duration_cast::safe_duration_cast(d, ec); - if (ec) { - FMT_THROW(format_error("value would cause UB or the wrong result")); - } + const auto d_as_common = fmt_safe_duration_cast(d); const auto d_as_whole_seconds = - safe_duration_cast::safe_duration_cast(d_as_common, - ec); - if (ec) { - FMT_THROW(format_error("value would cause UB or the wrong result")); - } + fmt_safe_duration_cast(d_as_common); // this conversion should be nonproblematic const auto diff = d_as_common - d_as_whole_seconds; - const auto ms = safe_duration_cast::safe_duration_cast< - std::chrono::duration>(diff, ec); - if (ec) { - FMT_THROW(format_error("value would cause UB or the wrong result")); - } + const auto ms = + fmt_safe_duration_cast>(diff); return ms; - #else auto s = std::chrono::duration_cast(d); return std::chrono::duration_cast(d - s);