template<bool IsFloatingPoint>
struct folly::detail::CheckOverflowToDuration< IsFloatingPoint >
Check for overflow when converting to a duration type that is second granularity or finer (e.g., nanoseconds, milliseconds, seconds)
This assumes the input is normalized, with subseconds >= 0 and subseconds less than 1 second.
Definition at line 268 of file Conv.h.
template<bool IsFloatingPoint>
template<typename Tgt , typename SubsecondRatio , typename Seconds , typename Subseconds >
Definition at line 274 of file Conv.h.
References LIKELY, max, folly::NEGATIVE_OVERFLOW, folly::POSITIVE_OVERFLOW, folly::SUCCESS, folly::to_signed(), folly::to_unsigned(), UNLIKELY, and value.
276 Tgt::period::num == 1,
277 "this implementation should only be used for subsecond granularity " 282 SubsecondRatio::num == 1,
"subsecond numerator should always be 1");
284 if (
LIKELY(seconds >= 0)) {
286 constexpr
auto maxSeconds = maxCount / Tgt::period::den;
289 if (
LIKELY(unsignedSeconds < maxSeconds)) {
293 if (
UNLIKELY(unsignedSeconds == maxSeconds)) {
294 constexpr
auto maxRemainder =
295 maxCount - (maxSeconds * Tgt::period::den);
296 constexpr
auto maxSubseconds =
297 (maxRemainder * SubsecondRatio::den) / Tgt::period::den;
298 if (subseconds <= 0) {
309 constexpr
auto minCount =
310 to_signed(std::numeric_limits<typename Tgt::rep>::lowest());
311 constexpr
auto minSeconds = (minCount / Tgt::period::den);
312 if (
LIKELY(seconds >= minSeconds)) {
316 if (
UNLIKELY(seconds == minSeconds - 1)) {
317 constexpr
auto maxRemainder =
318 minCount - (minSeconds * Tgt::period::den) + Tgt::period::den;
319 constexpr
auto maxSubseconds =
320 (maxRemainder * SubsecondRatio::den) / Tgt::period::den;
321 if (subseconds <= 0) {
324 if (subseconds >= maxSubseconds) {
constexpr auto to_unsigned(T const &t) -> typename std::make_unsigned< T >::type
static const char *const value
constexpr auto to_signed(T const &t) -> typename std::make_signed< T >::type