diff --git a/absl/time/duration.cc b/absl/time/duration.cc index 31b7bf2a1f7..19407080a7f 100644 --- a/absl/time/duration.cc +++ b/absl/time/duration.cc @@ -549,50 +549,6 @@ Duration DurationFromTimeval(timeval tv) { // // Conversion to other duration types. // - -int64_t ToInt64Nanoseconds(Duration d) { - if (time_internal::GetRepHi(d) >= 0 && - time_internal::GetRepHi(d) >> 33 == 0) { - return (time_internal::GetRepHi(d) * 1000 * 1000 * 1000) + - (time_internal::GetRepLo(d) / kTicksPerNanosecond); - } - return d / Nanoseconds(1); -} -int64_t ToInt64Microseconds(Duration d) { - if (time_internal::GetRepHi(d) >= 0 && - time_internal::GetRepHi(d) >> 43 == 0) { - return (time_internal::GetRepHi(d) * 1000 * 1000) + - (time_internal::GetRepLo(d) / (kTicksPerNanosecond * 1000)); - } - return d / Microseconds(1); -} -int64_t ToInt64Milliseconds(Duration d) { - if (time_internal::GetRepHi(d) >= 0 && - time_internal::GetRepHi(d) >> 53 == 0) { - return (time_internal::GetRepHi(d) * 1000) + - (time_internal::GetRepLo(d) / (kTicksPerNanosecond * 1000 * 1000)); - } - return d / Milliseconds(1); -} -int64_t ToInt64Seconds(Duration d) { - int64_t hi = time_internal::GetRepHi(d); - if (time_internal::IsInfiniteDuration(d)) return hi; - if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi; - return hi; -} -int64_t ToInt64Minutes(Duration d) { - int64_t hi = time_internal::GetRepHi(d); - if (time_internal::IsInfiniteDuration(d)) return hi; - if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi; - return hi / 60; -} -int64_t ToInt64Hours(Duration d) { - int64_t hi = time_internal::GetRepHi(d); - if (time_internal::IsInfiniteDuration(d)) return hi; - if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi; - return hi / (60 * 60); -} - double ToDoubleNanoseconds(Duration d) { return FDivDuration(d, Nanoseconds(1)); } diff --git a/absl/time/time.h b/absl/time/time.h index 4f88c304d77..d73a204c12c 100644 --- a/absl/time/time.h +++ b/absl/time/time.h @@ -620,12 +620,12 @@ ABSL_ATTRIBUTE_CONST_FUNCTION Duration Hours(T n) { // // absl::Duration d = absl::Milliseconds(1500); // int64_t isec = absl::ToInt64Seconds(d); // isec == 1 -ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Nanoseconds(Duration d); -ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Microseconds(Duration d); -ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Milliseconds(Duration d); -ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Seconds(Duration d); -ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Minutes(Duration d); -ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64Hours(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Nanoseconds(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Microseconds(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Milliseconds(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Seconds(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Minutes(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Hours(Duration d); // ToDoubleNanoseconds() // ToDoubleMicroseconds() @@ -1864,6 +1864,56 @@ ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromTimeT(time_t t) { return time_internal::FromUnixDuration(Seconds(t)); } +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Nanoseconds(Duration d) { + if (time_internal::GetRepHi(d) >= 0 && + time_internal::GetRepHi(d) >> 33 == 0) { + return (time_internal::GetRepHi(d) * 1000 * 1000 * 1000) + + (time_internal::GetRepLo(d) / time_internal::kTicksPerNanosecond); + } + return d / Nanoseconds(1); +} + +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Microseconds(Duration d) { + if (time_internal::GetRepHi(d) >= 0 && + time_internal::GetRepHi(d) >> 43 == 0) { + return (time_internal::GetRepHi(d) * 1000 * 1000) + + (time_internal::GetRepLo(d) / + (time_internal::kTicksPerNanosecond * 1000)); + } + return d / Microseconds(1); +} + +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Milliseconds(Duration d) { + if (time_internal::GetRepHi(d) >= 0 && + time_internal::GetRepHi(d) >> 53 == 0) { + return (time_internal::GetRepHi(d) * 1000) + + (time_internal::GetRepLo(d) / + (time_internal::kTicksPerNanosecond * 1000 * 1000)); + } + return d / Milliseconds(1); +} + +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Seconds(Duration d) { + int64_t hi = time_internal::GetRepHi(d); + if (time_internal::IsInfiniteDuration(d)) return hi; + if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi; + return hi; +} + +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Minutes(Duration d) { + int64_t hi = time_internal::GetRepHi(d); + if (time_internal::IsInfiniteDuration(d)) return hi; + if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi; + return hi / 60; +} + +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64Hours(Duration d) { + int64_t hi = time_internal::GetRepHi(d); + if (time_internal::IsInfiniteDuration(d)) return hi; + if (hi < 0 && time_internal::GetRepLo(d) != 0) ++hi; + return hi / (60 * 60); +} + ABSL_NAMESPACE_END } // namespace absl