Skip to content

Commit e041c50

Browse files
committed
Simplify duration cast
1 parent 8318918 commit e041c50

File tree

1 file changed

+2
-11
lines changed

1 file changed

+2
-11
lines changed

include/fmt/chrono.h

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,17 +161,6 @@ auto safe_duration_cast(std::chrono::duration<FromRep, FromPeriod> from,
161161
int& ec) -> To {
162162
using From = std::chrono::duration<FromRep, FromPeriod>;
163163
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-
}
175164

176165
// the basic idea is that we need to convert from count() in the from type
177166
// to count() in the To type, by multiplying it with this:
@@ -469,6 +458,8 @@ template <typename To, typename FromRep, typename FromPeriod,
469458
std::is_floating_point<typename To::rep>::value)>
470459
auto duration_cast(std::chrono::duration<FromRep, FromPeriod> from) -> To {
471460
#if FMT_SAFE_DURATION_CAST
461+
// Preserve infinity and NaN.
462+
if (!is_finite(from.count())) return static_cast<To>(from.count());
472463
// Throwing version of safe_duration_cast is only available for
473464
// integer to integer or float to float casts.
474465
int ec;

0 commit comments

Comments
 (0)