Skip to content

Commit 57fb8b8

Browse files
committed
Simplify duration cast
1 parent 8318918 commit 57fb8b8

File tree

1 file changed

+6
-17
lines changed

1 file changed

+6
-17
lines changed

include/fmt/chrono.h

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ FMT_BEGIN_NAMESPACE
3838
// Copyright Paul Dreik 2019
3939
namespace safe_duration_cast {
4040

41+
// DEPRECATED!
4142
template <typename To, typename From,
4243
FMT_ENABLE_IF(!std::is_same<From, To>::value &&
4344
std::numeric_limits<From>::is_signed ==
@@ -161,17 +162,6 @@ auto safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
161162
int& ec) -> To {
162163
using From = std::chrono::duration<FromRep, FromPeriod>;
163164
ec = 0;
164-
if (std::isnan(from.count())) {
165-
// nan in, gives nan out. easy.
166-
return To{std::numeric_limits<typename To::rep>::quiet_NaN()};
167-
}
168-
// maybe we should also check if from is denormal, and decide what to do about
169-
// it.
170-
171-
// +-inf should be preserved.
172-
if (std::isinf(from.count())) {
173-
return To{from.count()};
174-
}
175165

176166
// the basic idea is that we need to convert from count() in the from type
177167
// to count() in the To type, by multiplying it with this:
@@ -441,11 +431,7 @@ auto duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
441431

442432
using common_rep = typename std::common_type<FromRep, typename To::rep,
443433
decltype(factor::num)>::type;
444-
445-
int ec = 0;
446-
auto count = safe_duration_cast::lossless_integral_conversion<common_rep>(
447-
from.count(), ec);
448-
if (ec) throw_duration_error();
434+
common_rep count = from.count(); // This conversion is lossless.
449435

450436
// Multiply from.count() by factor and check for overflow.
451437
if (const_check(factor::num != 1)) {
@@ -456,6 +442,7 @@ auto duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
456442
count *= factor::num;
457443
}
458444
if (const_check(factor::den != 1)) count /= factor::den;
445+
int ec = 0;
459446
auto to =
460447
To(safe_duration_cast::lossless_integral_conversion<typename To::rep>(
461448
count, ec));
@@ -469,6 +456,8 @@ template <typename To, typename FromRep, typename FromPeriod,
469456
std::is_floating_point<typename To::rep>::value)>
470457
auto duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
471458
#if FMT_SAFE_DURATION_CAST
459+
// Preserve infinity and NaN.
460+
if (!isfinite(from.count())) return static_cast<To>(from.count());
472461
// Throwing version of safe_duration_cast is only available for
473462
// integer to integer or float to float casts.
474463
int ec;
@@ -485,7 +474,7 @@ template <typename To, typename FromRep, typename FromPeriod,
485474
FMT_ENABLE_IF(
486475
!is_similar_arithmetic_type<FromRep, typename To::rep>::value)>
487476
auto duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
488-
// Mixed integer <-> float cast is not supported by safe_duration_cast.
477+
// Mixed integer <-> float cast is not supported by safe duration_cast.
489478
return std::chrono::duration_cast<To>(from);
490479
}
491480

0 commit comments

Comments
 (0)