Refactor fmt_duration_cast to put #ifdef inside the function
This commit is contained in:
parent
bbfeea882f
commit
0b52401b2b
@ -430,41 +430,47 @@ auto write(OutputIt out, const std::tm& time, const std::locale& loc,
|
|||||||
return write_encoded_tm_str(out, string_view(buf.data(), buf.size()), loc);
|
return write_encoded_tm_str(out, string_view(buf.data(), buf.size()), loc);
|
||||||
}
|
}
|
||||||
|
|
||||||
#if FMT_SAFE_DURATION_CAST
|
template <typename Rep1, typename Rep2>
|
||||||
// throwing version of safe_duration_cast
|
struct is_same_arithmetic_type
|
||||||
// only available for integer<->integer or float<->float casts
|
: public std::integral_constant<bool,
|
||||||
template <typename To, typename FromRep, typename FromPeriod,
|
(std::is_integral<Rep1>::value &&
|
||||||
FMT_ENABLE_IF((std::is_integral<FromRep>::value &&
|
std::is_integral<Rep2>::value) ||
|
||||||
std::is_integral<typename To::rep>::value) ||
|
(std::is_floating_point<Rep1>::value &&
|
||||||
(std::is_floating_point<FromRep>::value &&
|
std::is_floating_point<Rep2>::value)> {
|
||||||
std::is_floating_point<typename To::rep>::value))>
|
};
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename To, typename FromRep, typename FromPeriod,
|
||||||
|
FMT_ENABLE_IF(is_same_arithmetic_type<FromRep, typename To::rep>::value)>
|
||||||
To fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) {
|
To fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) {
|
||||||
|
#if FMT_SAFE_DURATION_CAST
|
||||||
|
// throwing version of safe_duration_cast
|
||||||
|
// only available for integer<->integer or float<->float casts
|
||||||
int ec;
|
int ec;
|
||||||
To to = safe_duration_cast::safe_duration_cast<To>(from, ec);
|
To to = safe_duration_cast::safe_duration_cast<To>(from, ec);
|
||||||
if (ec) FMT_THROW(format_error("cannot format duration"));
|
if (ec) FMT_THROW(format_error("cannot format duration"));
|
||||||
return to;
|
return to;
|
||||||
}
|
|
||||||
// mixed integer<->float cast is not supported with safe_duration_cast
|
|
||||||
// fallback to standard duration cast in this case
|
|
||||||
template <typename To, typename FromRep, typename FromPeriod,
|
|
||||||
FMT_ENABLE_IF((std::is_integral<FromRep>::value !=
|
|
||||||
std::is_integral<typename To::rep>::value) &&
|
|
||||||
(std::is_floating_point<FromRep>::value !=
|
|
||||||
std::is_floating_point<typename To::rep>::value))>
|
|
||||||
To fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) {
|
|
||||||
return std::chrono::duration_cast<To>(from);
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
// standard duration cast, may overflow and invoke undefined behavior
|
// standard duration cast, may overflow and invoke undefined behavior
|
||||||
template <typename To, typename FromRep, typename FromPeriod>
|
return std::chrono::duration_cast<To>(from);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
template <
|
||||||
|
typename To, typename FromRep, typename FromPeriod,
|
||||||
|
FMT_ENABLE_IF(!is_same_arithmetic_type<FromRep, typename To::rep>::value)>
|
||||||
To fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) {
|
To fmt_duration_cast(std::chrono::duration<FromRep, FromPeriod> from) {
|
||||||
|
// mixed integer<->float cast is not supported with safe_duration_cast
|
||||||
|
// fallback to standard duration cast in this case
|
||||||
return std::chrono::duration_cast<To>(from);
|
return std::chrono::duration_cast<To>(from);
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
|
|
||||||
template <typename Duration>
|
template <typename Duration>
|
||||||
std::time_t to_time_t(
|
std::time_t to_time_t(
|
||||||
std::chrono::time_point<std::chrono::system_clock, Duration> time_point) {
|
std::chrono::time_point<std::chrono::system_clock, Duration> time_point) {
|
||||||
|
// cannot use std::chrono::system_clock::to_time_t() since this would first
|
||||||
|
// require a cast to std::chrono::system_clock::time_point, which could
|
||||||
|
// overflow.
|
||||||
return fmt_duration_cast<std::chrono::duration<std::time_t>>(
|
return fmt_duration_cast<std::chrono::duration<std::time_t>>(
|
||||||
time_point.time_since_epoch())
|
time_point.time_since_epoch())
|
||||||
.count();
|
.count();
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user