From 1f8da02dc8b8b0581326f90510ed8c4009fc1017 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 20 Dec 2022 15:37:58 -0800 Subject: Tagged most functions in absl/time/time.h as ABSL_ATTRIBUTE_CONST_FUNCTION or ABSL_ATTRIBUTE_PURE_FUNCTION However, both absl_attributes are now unimplemented to avoid breaking existing users. PiperOrigin-RevId: 496769399 Change-Id: I9c00cb60b885526300d744f9ea7c0f2178f092bb --- absl/base/attributes.h | 21 +-- absl/time/time.h | 407 +++++++++++++++++++++++++++----------------- absl/time/time_benchmark.cc | 15 +- 3 files changed, 263 insertions(+), 180 deletions(-) diff --git a/absl/base/attributes.h b/absl/base/attributes.h index e11a064a..b7826e77 100644 --- a/absl/base/attributes.h +++ b/absl/base/attributes.h @@ -716,26 +716,9 @@ #define ABSL_CONST_INIT #endif -// ABSL_ATTRIBUTE_PURE_FUNCTION -// -// ABSL_ATTRIBUTE_PURE_FUNCTION is used to annotate declarations of "pure" -// functions. A function is pure if its return value is only a function of its -// arguments. The pure attribute prohibits a function from modifying the state -// of the program that is observable by means other than inspecting the -// function's return value. Declaring such functions with the pure attribute -// allows the compiler to avoid emitting some calls in repeated invocations of -// the function with the same argument values. -// -// Example: -// -// ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Milliseconds(Duration d); -#if ABSL_HAVE_CPP_ATTRIBUTE(gnu::pure) -#define ABSL_ATTRIBUTE_PURE_FUNCTION [[gnu::pure]] -#elif ABSL_HAVE_ATTRIBUTE(pure) -#define ABSL_ATTRIBUTE_PURE_FUNCTION __attribute__((pure)) -#else +// These annotations are not available yet due to fear of breaking code. #define ABSL_ATTRIBUTE_PURE_FUNCTION -#endif +#define ABSL_ATTRIBUTE_CONST_FUNCTION // ABSL_ATTRIBUTE_LIFETIME_BOUND indicates that a resource owned by a function // parameter or implicit object parameter is retained by the return value of the diff --git a/absl/time/time.h b/absl/time/time.h index 11796b4f..cc390082 100644 --- a/absl/time/time.h +++ b/absl/time/time.h @@ -78,6 +78,7 @@ struct timeval; #include #include #include +#include #include #include #include @@ -97,19 +98,24 @@ class TimeZone; // Defined below namespace time_internal { int64_t IDivDuration(bool satq, Duration num, Duration den, Duration* rem); -constexpr Time FromUnixDuration(Duration d); -constexpr Duration ToUnixDuration(Time t); -constexpr int64_t GetRepHi(Duration d); -constexpr uint32_t GetRepLo(Duration d); -constexpr Duration MakeDuration(int64_t hi, uint32_t lo); -constexpr Duration MakeDuration(int64_t hi, int64_t lo); -inline Duration MakePosDoubleDuration(double n); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixDuration(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration ToUnixDuration(Time t); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t GetRepHi(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr uint32_t GetRepLo(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi, + uint32_t lo); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi, + int64_t lo); +ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration MakePosDoubleDuration(double n); constexpr int64_t kTicksPerNanosecond = 4; constexpr int64_t kTicksPerSecond = 1000 * 1000 * 1000 * kTicksPerNanosecond; template -constexpr Duration FromInt64(int64_t v, std::ratio<1, N>); -constexpr Duration FromInt64(int64_t v, std::ratio<60>); -constexpr Duration FromInt64(int64_t v, std::ratio<3600>); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, + std::ratio<1, N>); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, + std::ratio<60>); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, + std::ratio<3600>); template using EnableIfIntegral = typename std::enable_if< std::is_integral::value || std::is_enum::value, int>::type; @@ -222,37 +228,61 @@ class Duration { }; // Relational Operators -constexpr bool operator<(Duration lhs, Duration rhs); -constexpr bool operator>(Duration lhs, Duration rhs) { return rhs < lhs; } -constexpr bool operator>=(Duration lhs, Duration rhs) { return !(lhs < rhs); } -constexpr bool operator<=(Duration lhs, Duration rhs) { return !(rhs < lhs); } -constexpr bool operator==(Duration lhs, Duration rhs); -constexpr bool operator!=(Duration lhs, Duration rhs) { return !(lhs == rhs); } +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<(Duration lhs, + Duration rhs); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>(Duration lhs, + Duration rhs) { + return rhs < lhs; +} +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>=(Duration lhs, + Duration rhs) { + return !(lhs < rhs); +} +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<=(Duration lhs, + Duration rhs) { + return !(rhs < lhs); +} +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator==(Duration lhs, + Duration rhs); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator!=(Duration lhs, + Duration rhs) { + return !(lhs == rhs); +} // Additive Operators -constexpr Duration operator-(Duration d); -inline Duration operator+(Duration lhs, Duration rhs) { return lhs += rhs; } -inline Duration operator-(Duration lhs, Duration rhs) { return lhs -= rhs; } +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration operator-(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator+(Duration lhs, + Duration rhs) { + return lhs += rhs; +} +ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator-(Duration lhs, + Duration rhs) { + return lhs -= rhs; +} // Multiplicative Operators // Integer operands must be representable as int64_t. template -Duration operator*(Duration lhs, T rhs) { +ABSL_ATTRIBUTE_CONST_FUNCTION Duration operator*(Duration lhs, T rhs) { return lhs *= rhs; } template -Duration operator*(T lhs, Duration rhs) { +ABSL_ATTRIBUTE_CONST_FUNCTION Duration operator*(T lhs, Duration rhs) { return rhs *= lhs; } template -Duration operator/(Duration lhs, T rhs) { +ABSL_ATTRIBUTE_CONST_FUNCTION Duration operator/(Duration lhs, T rhs) { return lhs /= rhs; } -inline int64_t operator/(Duration lhs, Duration rhs) { +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t operator/(Duration lhs, + Duration rhs) { return time_internal::IDivDuration(true, lhs, rhs, &lhs); // trunc towards zero } -inline Duration operator%(Duration lhs, Duration rhs) { return lhs %= rhs; } +ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator%(Duration lhs, + Duration rhs) { + return lhs %= rhs; +} // IDivDuration() // @@ -299,18 +329,20 @@ inline int64_t IDivDuration(Duration num, Duration den, Duration* rem) { // // double d = absl::FDivDuration(absl::Milliseconds(1500), absl::Seconds(1)); // // d == 1.5 -double FDivDuration(Duration num, Duration den); +ABSL_ATTRIBUTE_CONST_FUNCTION double FDivDuration(Duration num, Duration den); // ZeroDuration() // // Returns a zero-length duration. This function behaves just like the default // constructor, but the name helps make the semantics clear at call sites. -constexpr Duration ZeroDuration() { return Duration(); } +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration ZeroDuration() { + return Duration(); +} // AbsDuration() // // Returns the absolute value of a duration. -inline Duration AbsDuration(Duration d) { +ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration AbsDuration(Duration d) { return (d < ZeroDuration()) ? -d : d; } @@ -322,7 +354,7 @@ inline Duration AbsDuration(Duration d) { // // absl::Duration d = absl::Nanoseconds(123456789); // absl::Duration a = absl::Trunc(d, absl::Microseconds(1)); // 123456us -Duration Trunc(Duration d, Duration unit); +ABSL_ATTRIBUTE_CONST_FUNCTION Duration Trunc(Duration d, Duration unit); // Floor() // @@ -333,7 +365,7 @@ Duration Trunc(Duration d, Duration unit); // // absl::Duration d = absl::Nanoseconds(123456789); // absl::Duration b = absl::Floor(d, absl::Microseconds(1)); // 123456us -Duration Floor(Duration d, Duration unit); +ABSL_ATTRIBUTE_CONST_FUNCTION Duration Floor(Duration d, Duration unit); // Ceil() // @@ -344,7 +376,7 @@ Duration Floor(Duration d, Duration unit); // // absl::Duration d = absl::Nanoseconds(123456789); // absl::Duration c = absl::Ceil(d, absl::Microseconds(1)); // 123457us -Duration Ceil(Duration d, Duration unit); +ABSL_ATTRIBUTE_CONST_FUNCTION Duration Ceil(Duration d, Duration unit); // InfiniteDuration() // @@ -380,7 +412,7 @@ Duration Ceil(Duration d, Duration unit); // // The examples involving the `/` operator above also apply to `IDivDuration()` // and `FDivDuration()`. -constexpr Duration InfiniteDuration(); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration InfiniteDuration(); // Nanoseconds() // Microseconds() @@ -404,27 +436,27 @@ constexpr Duration InfiniteDuration(); // absl::Duration a = absl::Seconds(60); // absl::Duration b = absl::Minutes(1); // b == a template = 0> -constexpr Duration Nanoseconds(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Nanoseconds(T n) { return time_internal::FromInt64(n, std::nano{}); } template = 0> -constexpr Duration Microseconds(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Microseconds(T n) { return time_internal::FromInt64(n, std::micro{}); } template = 0> -constexpr Duration Milliseconds(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Milliseconds(T n) { return time_internal::FromInt64(n, std::milli{}); } template = 0> -constexpr Duration Seconds(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Seconds(T n) { return time_internal::FromInt64(n, std::ratio<1>{}); } template = 0> -constexpr Duration Minutes(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Minutes(T n) { return time_internal::FromInt64(n, std::ratio<60>{}); } template = 0> -constexpr Duration Hours(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration Hours(T n) { return time_internal::FromInt64(n, std::ratio<3600>{}); } @@ -438,19 +470,19 @@ constexpr Duration Hours(T n) { // auto a = absl::Seconds(1.5); // OK // auto b = absl::Milliseconds(1500); // BETTER template = 0> -Duration Nanoseconds(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION Duration Nanoseconds(T n) { return n * Nanoseconds(1); } template = 0> -Duration Microseconds(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION Duration Microseconds(T n) { return n * Microseconds(1); } template = 0> -Duration Milliseconds(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION Duration Milliseconds(T n) { return n * Milliseconds(1); } template = 0> -Duration Seconds(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION Duration Seconds(T n) { if (n >= 0) { // Note: `NaN >= 0` is false. if (n >= static_cast((std::numeric_limits::max)())) { return InfiniteDuration(); @@ -464,11 +496,11 @@ Duration Seconds(T n) { } } template = 0> -Duration Minutes(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION Duration Minutes(T n) { return n * Minutes(1); } template = 0> -Duration Hours(T n) { +ABSL_ATTRIBUTE_CONST_FUNCTION Duration Hours(T n) { return n * Hours(1); } @@ -488,12 +520,12 @@ Duration Hours(T n) { // // absl::Duration d = absl::Milliseconds(1500); // int64_t isec = absl::ToInt64Seconds(d); // isec == 1 -ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Nanoseconds(Duration d); -ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Microseconds(Duration d); -ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Milliseconds(Duration d); -ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Seconds(Duration d); -ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Minutes(Duration d); -ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Hours(Duration d); +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); // ToDoubleNanoseconds() // ToDoubleMicroseconds() @@ -510,12 +542,12 @@ ABSL_ATTRIBUTE_PURE_FUNCTION int64_t ToInt64Hours(Duration d); // // absl::Duration d = absl::Milliseconds(1500); // double dsec = absl::ToDoubleSeconds(d); // dsec == 1.5 -ABSL_ATTRIBUTE_PURE_FUNCTION double ToDoubleNanoseconds(Duration d); -ABSL_ATTRIBUTE_PURE_FUNCTION double ToDoubleMicroseconds(Duration d); -ABSL_ATTRIBUTE_PURE_FUNCTION double ToDoubleMilliseconds(Duration d); -ABSL_ATTRIBUTE_PURE_FUNCTION double ToDoubleSeconds(Duration d); -ABSL_ATTRIBUTE_PURE_FUNCTION double ToDoubleMinutes(Duration d); -ABSL_ATTRIBUTE_PURE_FUNCTION double ToDoubleHours(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleNanoseconds(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleMicroseconds(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleMilliseconds(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleSeconds(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleMinutes(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION double ToDoubleHours(Duration d); // FromChrono() // @@ -525,12 +557,18 @@ ABSL_ATTRIBUTE_PURE_FUNCTION double ToDoubleHours(Duration d); // // std::chrono::milliseconds ms(123); // absl::Duration d = absl::FromChrono(ms); -constexpr Duration FromChrono(const std::chrono::nanoseconds& d); -constexpr Duration FromChrono(const std::chrono::microseconds& d); -constexpr Duration FromChrono(const std::chrono::milliseconds& d); -constexpr Duration FromChrono(const std::chrono::seconds& d); -constexpr Duration FromChrono(const std::chrono::minutes& d); -constexpr Duration FromChrono(const std::chrono::hours& d); +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::nanoseconds& d); +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::microseconds& d); +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::milliseconds& d); +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::seconds& d); +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::minutes& d); +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::hours& d); // ToChronoNanoseconds() // ToChronoMicroseconds() @@ -550,18 +588,21 @@ constexpr Duration FromChrono(const std::chrono::hours& d); // auto y = absl::ToChronoNanoseconds(d); // x == y // auto z = absl::ToChronoSeconds(absl::InfiniteDuration()); // // z == std::chrono::seconds::max() -std::chrono::nanoseconds ToChronoNanoseconds(Duration d); -std::chrono::microseconds ToChronoMicroseconds(Duration d); -std::chrono::milliseconds ToChronoMilliseconds(Duration d); -std::chrono::seconds ToChronoSeconds(Duration d); -std::chrono::minutes ToChronoMinutes(Duration d); -std::chrono::hours ToChronoHours(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::nanoseconds ToChronoNanoseconds( + Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::microseconds ToChronoMicroseconds( + Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::milliseconds ToChronoMilliseconds( + Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::seconds ToChronoSeconds(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::minutes ToChronoMinutes(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::hours ToChronoHours(Duration d); // FormatDuration() // // Returns a string representing the duration in the form "72h3m0.5s". // Returns "inf" or "-inf" for +/- `InfiniteDuration()`. -std::string FormatDuration(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION std::string FormatDuration(Duration d); // Output stream operator. inline std::ostream& operator<<(std::ostream& os, Duration d) { @@ -725,29 +766,49 @@ class Time { }; // Relational Operators -constexpr bool operator<(Time lhs, Time rhs) { return lhs.rep_ < rhs.rep_; } -constexpr bool operator>(Time lhs, Time rhs) { return rhs < lhs; } -constexpr bool operator>=(Time lhs, Time rhs) { return !(lhs < rhs); } -constexpr bool operator<=(Time lhs, Time rhs) { return !(rhs < lhs); } -constexpr bool operator==(Time lhs, Time rhs) { return lhs.rep_ == rhs.rep_; } -constexpr bool operator!=(Time lhs, Time rhs) { return !(lhs == rhs); } +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<(Time lhs, Time rhs) { + return lhs.rep_ < rhs.rep_; +} +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>(Time lhs, Time rhs) { + return rhs < lhs; +} +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator>=(Time lhs, Time rhs) { + return !(lhs < rhs); +} +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<=(Time lhs, Time rhs) { + return !(rhs < lhs); +} +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator==(Time lhs, Time rhs) { + return lhs.rep_ == rhs.rep_; +} +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator!=(Time lhs, Time rhs) { + return !(lhs == rhs); +} // Additive Operators -inline Time operator+(Time lhs, Duration rhs) { return lhs += rhs; } -inline Time operator+(Duration lhs, Time rhs) { return rhs += lhs; } -inline Time operator-(Time lhs, Duration rhs) { return lhs -= rhs; } -inline Duration operator-(Time lhs, Time rhs) { return lhs.rep_ - rhs.rep_; } +ABSL_ATTRIBUTE_CONST_FUNCTION inline Time operator+(Time lhs, Duration rhs) { + return lhs += rhs; +} +ABSL_ATTRIBUTE_CONST_FUNCTION inline Time operator+(Duration lhs, Time rhs) { + return rhs += lhs; +} +ABSL_ATTRIBUTE_CONST_FUNCTION inline Time operator-(Time lhs, Duration rhs) { + return lhs -= rhs; +} +ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration operator-(Time lhs, Time rhs) { + return lhs.rep_ - rhs.rep_; +} // UnixEpoch() // // Returns the `absl::Time` representing "1970-01-01 00:00:00.0 +0000". -constexpr Time UnixEpoch() { return Time(); } +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time UnixEpoch() { return Time(); } // UniversalEpoch() // // Returns the `absl::Time` representing "0001-01-01 00:00:00.0 +0000", the // epoch of the ICU Universal Time Scale. -constexpr Time UniversalEpoch() { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time UniversalEpoch() { // 719162 is the number of days from 0001-01-01 to 1970-01-01, // assuming the Gregorian calendar. return Time( @@ -757,7 +818,7 @@ constexpr Time UniversalEpoch() { // InfiniteFuture() // // Returns an `absl::Time` that is infinitely far in the future. -constexpr Time InfiniteFuture() { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time InfiniteFuture() { return Time(time_internal::MakeDuration((std::numeric_limits::max)(), ~uint32_t{0})); } @@ -765,7 +826,7 @@ constexpr Time InfiniteFuture() { // InfinitePast() // // Returns an `absl::Time` that is infinitely far in the past. -constexpr Time InfinitePast() { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time InfinitePast() { return Time(time_internal::MakeDuration((std::numeric_limits::min)(), ~uint32_t{0})); } @@ -779,13 +840,13 @@ constexpr Time InfinitePast() { // FromUniversal() // // Creates an `absl::Time` from a variety of other representations. -constexpr Time FromUnixNanos(int64_t ns); -constexpr Time FromUnixMicros(int64_t us); -constexpr Time FromUnixMillis(int64_t ms); -constexpr Time FromUnixSeconds(int64_t s); -constexpr Time FromTimeT(time_t t); -Time FromUDate(double udate); -Time FromUniversal(int64_t universal); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixNanos(int64_t ns); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMicros(int64_t us); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMillis(int64_t ms); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixSeconds(int64_t s); +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromTimeT(time_t t); +ABSL_ATTRIBUTE_CONST_FUNCTION Time FromUDate(double udate); +ABSL_ATTRIBUTE_CONST_FUNCTION Time FromUniversal(int64_t universal); // ToUnixNanos() // ToUnixMicros() @@ -799,13 +860,13 @@ Time FromUniversal(int64_t universal); // these operations round down toward negative infinity where necessary to // adjust to the resolution of the result type. Beware of possible time_t // over/underflow in ToTime{T,val,spec}() on 32-bit platforms. -int64_t ToUnixNanos(Time t); -int64_t ToUnixMicros(Time t); -int64_t ToUnixMillis(Time t); -int64_t ToUnixSeconds(Time t); -time_t ToTimeT(Time t); -double ToUDate(Time t); -int64_t ToUniversal(Time t); +ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixNanos(Time t); +ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixMicros(Time t); +ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixMillis(Time t); +ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUnixSeconds(Time t); +ABSL_ATTRIBUTE_CONST_FUNCTION time_t ToTimeT(Time t); +ABSL_ATTRIBUTE_CONST_FUNCTION double ToUDate(Time t); +ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToUniversal(Time t); // DurationFromTimespec() // DurationFromTimeval() @@ -821,14 +882,14 @@ int64_t ToUniversal(Time t); // and gettimeofday(2)), so conversion functions are provided for both cases. // The "to timespec/val" direction is easily handled via overloading, but // for "from timespec/val" the desired type is part of the function name. -Duration DurationFromTimespec(timespec ts); -Duration DurationFromTimeval(timeval tv); -timespec ToTimespec(Duration d); -timeval ToTimeval(Duration d); -Time TimeFromTimespec(timespec ts); -Time TimeFromTimeval(timeval tv); -timespec ToTimespec(Time t); -timeval ToTimeval(Time t); +ABSL_ATTRIBUTE_CONST_FUNCTION Duration DurationFromTimespec(timespec ts); +ABSL_ATTRIBUTE_CONST_FUNCTION Duration DurationFromTimeval(timeval tv); +ABSL_ATTRIBUTE_CONST_FUNCTION timespec ToTimespec(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION timeval ToTimeval(Duration d); +ABSL_ATTRIBUTE_CONST_FUNCTION Time TimeFromTimespec(timespec ts); +ABSL_ATTRIBUTE_CONST_FUNCTION Time TimeFromTimeval(timeval tv); +ABSL_ATTRIBUTE_CONST_FUNCTION timespec ToTimespec(Time t); +ABSL_ATTRIBUTE_CONST_FUNCTION timeval ToTimeval(Time t); // FromChrono() // @@ -839,7 +900,8 @@ timeval ToTimeval(Time t); // auto tp = std::chrono::system_clock::from_time_t(123); // absl::Time t = absl::FromChrono(tp); // // t == absl::FromTimeT(123) -Time FromChrono(const std::chrono::system_clock::time_point& tp); +ABSL_ATTRIBUTE_PURE_FUNCTION Time +FromChrono(const std::chrono::system_clock::time_point& tp); // ToChronoTime() // @@ -852,7 +914,8 @@ Time FromChrono(const std::chrono::system_clock::time_point& tp); // absl::Time t = absl::FromTimeT(123); // auto tp = absl::ToChronoTime(t); // // tp == std::chrono::system_clock::from_time_t(123); -std::chrono::system_clock::time_point ToChronoTime(Time); +ABSL_ATTRIBUTE_CONST_FUNCTION std::chrono::system_clock::time_point + ToChronoTime(Time); // AbslParseFlag() // @@ -1124,22 +1187,25 @@ inline TimeZone LocalTimeZone() { // absl::Time t = ...; // absl::TimeZone tz = ...; // const auto cd = absl::ToCivilDay(t, tz); -inline CivilSecond ToCivilSecond(Time t, TimeZone tz) { +ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilSecond ToCivilSecond(Time t, + TimeZone tz) { return tz.At(t).cs; // already a CivilSecond } -inline CivilMinute ToCivilMinute(Time t, TimeZone tz) { +ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilMinute ToCivilMinute(Time t, + TimeZone tz) { return CivilMinute(tz.At(t).cs); } -inline CivilHour ToCivilHour(Time t, TimeZone tz) { +ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilHour ToCivilHour(Time t, TimeZone tz) { return CivilHour(tz.At(t).cs); } -inline CivilDay ToCivilDay(Time t, TimeZone tz) { +ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilDay ToCivilDay(Time t, TimeZone tz) { return CivilDay(tz.At(t).cs); } -inline CivilMonth ToCivilMonth(Time t, TimeZone tz) { +ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilMonth ToCivilMonth(Time t, + TimeZone tz) { return CivilMonth(tz.At(t).cs); } -inline CivilYear ToCivilYear(Time t, TimeZone tz) { +ABSL_ATTRIBUTE_PURE_FUNCTION inline CivilYear ToCivilYear(Time t, TimeZone tz) { return CivilYear(tz.At(t).cs); } @@ -1155,7 +1221,8 @@ inline CivilYear ToCivilYear(Time t, TimeZone tz) { // being when two non-existent civil times map to the same transition time. // // Note: Accepts civil times of any alignment. -inline Time FromCivil(CivilSecond ct, TimeZone tz) { +ABSL_ATTRIBUTE_PURE_FUNCTION inline Time FromCivil(CivilSecond ct, + TimeZone tz) { const auto ti = tz.At(ct); if (ti.kind == TimeZone::TimeInfo::SKIPPED) return ti.trans; return ti.pre; @@ -1240,13 +1307,13 @@ inline Time FromDateTime(int64_t year, int mon, int day, int hour, // instant, so `tm_isdst != 0` returns the DST instant, and `tm_isdst == 0` // returns the non-DST instant, that would have matched if the transition never // happened. -Time FromTM(const struct tm& tm, TimeZone tz); +ABSL_ATTRIBUTE_PURE_FUNCTION Time FromTM(const struct tm& tm, TimeZone tz); // ToTM() // // Converts the given `absl::Time` to a struct tm using the given time zone. // See ctime(3) for a description of the values of the tm fields. -struct tm ToTM(Time t, TimeZone tz); +ABSL_ATTRIBUTE_PURE_FUNCTION struct tm ToTM(Time t, TimeZone tz); // RFC3339_full // RFC3339_sec @@ -1305,13 +1372,14 @@ ABSL_DLL extern const char RFC1123_no_wday[]; // %d %b %E4Y %H:%M:%S %z // `absl::InfinitePast()`, the returned string will be exactly "infinite-past". // In both cases the given format string and `absl::TimeZone` are ignored. // -std::string FormatTime(absl::string_view format, Time t, TimeZone tz); +ABSL_ATTRIBUTE_PURE_FUNCTION std::string FormatTime(absl::string_view format, + Time t, TimeZone tz); // Convenience functions that format the given time using the RFC3339_full // format. The first overload uses the provided TimeZone, while the second // uses LocalTimeZone(). -std::string FormatTime(Time t, TimeZone tz); -std::string FormatTime(Time t); +ABSL_ATTRIBUTE_PURE_FUNCTION std::string FormatTime(Time t, TimeZone tz); +ABSL_ATTRIBUTE_PURE_FUNCTION std::string FormatTime(Time t); // Output stream operator. inline std::ostream& operator<<(std::ostream& os, Time t) { @@ -1389,18 +1457,20 @@ namespace time_internal { // Creates a Duration with a given representation. // REQUIRES: hi,lo is a valid representation of a Duration as specified // in time/duration.cc. -constexpr Duration MakeDuration(int64_t hi, uint32_t lo = 0) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi, + uint32_t lo = 0) { return Duration(hi, lo); } -constexpr Duration MakeDuration(int64_t hi, int64_t lo) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeDuration(int64_t hi, + int64_t lo) { return MakeDuration(hi, static_cast(lo)); } // Make a Duration value from a floating-point number, as long as that number // is in the range [ 0 .. numeric_limits::max ), that is, as long as // it's positive and can be converted to int64_t without risk of UB. -inline Duration MakePosDoubleDuration(double n) { +ABSL_ATTRIBUTE_CONST_FUNCTION inline Duration MakePosDoubleDuration(double n) { const int64_t int_secs = static_cast(n); const uint32_t ticks = static_cast( std::round((n - static_cast(int_secs)) * kTicksPerSecond)); @@ -1413,23 +1483,28 @@ inline Duration MakePosDoubleDuration(double n) { // pair. sec may be positive or negative. ticks must be in the range // -kTicksPerSecond < *ticks < kTicksPerSecond. If ticks is negative it // will be normalized to a positive value in the resulting Duration. -constexpr Duration MakeNormalizedDuration(int64_t sec, int64_t ticks) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration MakeNormalizedDuration( + int64_t sec, int64_t ticks) { return (ticks < 0) ? MakeDuration(sec - 1, ticks + kTicksPerSecond) : MakeDuration(sec, ticks); } // Provide access to the Duration representation. -constexpr int64_t GetRepHi(Duration d) { return d.rep_hi_; } -constexpr uint32_t GetRepLo(Duration d) { return d.rep_lo_; } +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t GetRepHi(Duration d) { + return d.rep_hi_; +} +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr uint32_t GetRepLo(Duration d) { + return d.rep_lo_; +} // Returns true iff d is positive or negative infinity. -constexpr bool IsInfiniteDuration(Duration d) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool IsInfiniteDuration(Duration d) { return GetRepLo(d) == ~uint32_t{0}; } // Returns an infinite Duration with the opposite sign. // REQUIRES: IsInfiniteDuration(d) -constexpr Duration OppositeInfinity(Duration d) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration OppositeInfinity(Duration d) { return GetRepHi(d) < 0 ? MakeDuration((std::numeric_limits::max)(), ~uint32_t{0}) : MakeDuration((std::numeric_limits::min)(), @@ -1437,7 +1512,8 @@ constexpr Duration OppositeInfinity(Duration d) { } // Returns (-n)-1 (equivalently -(n+1)) without avoidable overflow. -constexpr int64_t NegateAndSubtractOne(int64_t n) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr int64_t NegateAndSubtractOne( + int64_t n) { // Note: Good compilers will optimize this expression to ~n when using // a two's-complement representation (which is required for int64_t). return (n < 0) ? -(n + 1) : (-n) - 1; @@ -1447,23 +1523,30 @@ constexpr int64_t NegateAndSubtractOne(int64_t n) { // functions depend on the above mentioned choice of the Unix epoch for the // Time representation (and both need to be Time friends). Without this // knowledge, we would need to add-in/subtract-out UnixEpoch() respectively. -constexpr Time FromUnixDuration(Duration d) { return Time(d); } -constexpr Duration ToUnixDuration(Time t) { return t.rep_; } +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixDuration(Duration d) { + return Time(d); +} +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration ToUnixDuration(Time t) { + return t.rep_; +} template -constexpr Duration FromInt64(int64_t v, std::ratio<1, N>) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, + std::ratio<1, N>) { static_assert(0 < N && N <= 1000 * 1000 * 1000, "Unsupported ratio"); // Subsecond ratios cannot overflow. return MakeNormalizedDuration( v / N, v % N * kTicksPerNanosecond * 1000 * 1000 * 1000 / N); } -constexpr Duration FromInt64(int64_t v, std::ratio<60>) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, + std::ratio<60>) { return (v <= (std::numeric_limits::max)() / 60 && v >= (std::numeric_limits::min)() / 60) ? MakeDuration(v * 60) : v > 0 ? InfiniteDuration() : -InfiniteDuration(); } -constexpr Duration FromInt64(int64_t v, std::ratio<3600>) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration FromInt64(int64_t v, + std::ratio<3600>) { return (v <= (std::numeric_limits::max)() / 3600 && v >= (std::numeric_limits::min)() / 3600) ? MakeDuration(v * 3600) @@ -1483,40 +1566,44 @@ constexpr auto IsValidRep64(char) -> bool { // Converts a std::chrono::duration to an absl::Duration. template -constexpr Duration FromChrono(const std::chrono::duration& d) { +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::duration& d) { static_assert(IsValidRep64(0), "duration::rep is invalid"); return FromInt64(int64_t{d.count()}, Period{}); } template -int64_t ToInt64(Duration d, Ratio) { +ABSL_ATTRIBUTE_CONST_FUNCTION int64_t ToInt64(Duration d, Ratio) { // Note: This may be used on MSVC, which may have a system_clock period of // std::ratio<1, 10 * 1000 * 1000> return ToInt64Seconds(d * Ratio::den / Ratio::num); } // Fastpath implementations for the 6 common duration units. -inline int64_t ToInt64(Duration d, std::nano) { +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, std::nano) { return ToInt64Nanoseconds(d); } -inline int64_t ToInt64(Duration d, std::micro) { +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, std::micro) { return ToInt64Microseconds(d); } -inline int64_t ToInt64(Duration d, std::milli) { +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, std::milli) { return ToInt64Milliseconds(d); } -inline int64_t ToInt64(Duration d, std::ratio<1>) { +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, + std::ratio<1>) { return ToInt64Seconds(d); } -inline int64_t ToInt64(Duration d, std::ratio<60>) { +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, + std::ratio<60>) { return ToInt64Minutes(d); } -inline int64_t ToInt64(Duration d, std::ratio<3600>) { +ABSL_ATTRIBUTE_CONST_FUNCTION inline int64_t ToInt64(Duration d, + std::ratio<3600>) { return ToInt64Hours(d); } // Converts an absl::Duration to a chrono duration of type T. template -T ToChronoDuration(Duration d) { +ABSL_ATTRIBUTE_CONST_FUNCTION T ToChronoDuration(Duration d) { using Rep = typename T::rep; using Period = typename T::period; static_assert(IsValidRep64(0), "duration::rep is invalid"); @@ -1530,7 +1617,8 @@ T ToChronoDuration(Duration d) { } // namespace time_internal -constexpr bool operator<(Duration lhs, Duration rhs) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator<(Duration lhs, + Duration rhs) { return time_internal::GetRepHi(lhs) != time_internal::GetRepHi(rhs) ? time_internal::GetRepHi(lhs) < time_internal::GetRepHi(rhs) : time_internal::GetRepHi(lhs) == (std::numeric_limits::min)() @@ -1539,12 +1627,13 @@ constexpr bool operator<(Duration lhs, Duration rhs) { : time_internal::GetRepLo(lhs) < time_internal::GetRepLo(rhs); } -constexpr bool operator==(Duration lhs, Duration rhs) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr bool operator==(Duration lhs, + Duration rhs) { return time_internal::GetRepHi(lhs) == time_internal::GetRepHi(rhs) && time_internal::GetRepLo(lhs) == time_internal::GetRepLo(rhs); } -constexpr Duration operator-(Duration d) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration operator-(Duration d) { // This is a little interesting because of the special cases. // // If rep_lo_ is zero, we have it easy; it's safe to negate rep_hi_, we're @@ -1570,47 +1659,53 @@ constexpr Duration operator-(Duration d) { time_internal::GetRepLo(d)); } -constexpr Duration InfiniteDuration() { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Duration InfiniteDuration() { return time_internal::MakeDuration((std::numeric_limits::max)(), ~uint32_t{0}); } -constexpr Duration FromChrono(const std::chrono::nanoseconds& d) { +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::nanoseconds& d) { return time_internal::FromChrono(d); } -constexpr Duration FromChrono(const std::chrono::microseconds& d) { +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::microseconds& d) { return time_internal::FromChrono(d); } -constexpr Duration FromChrono(const std::chrono::milliseconds& d) { +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::milliseconds& d) { return time_internal::FromChrono(d); } -constexpr Duration FromChrono(const std::chrono::seconds& d) { +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::seconds& d) { return time_internal::FromChrono(d); } -constexpr Duration FromChrono(const std::chrono::minutes& d) { +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::minutes& d) { return time_internal::FromChrono(d); } -constexpr Duration FromChrono(const std::chrono::hours& d) { +ABSL_ATTRIBUTE_PURE_FUNCTION constexpr Duration FromChrono( + const std::chrono::hours& d) { return time_internal::FromChrono(d); } -constexpr Time FromUnixNanos(int64_t ns) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixNanos(int64_t ns) { return time_internal::FromUnixDuration(Nanoseconds(ns)); } -constexpr Time FromUnixMicros(int64_t us) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMicros(int64_t us) { return time_internal::FromUnixDuration(Microseconds(us)); } -constexpr Time FromUnixMillis(int64_t ms) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixMillis(int64_t ms) { return time_internal::FromUnixDuration(Milliseconds(ms)); } -constexpr Time FromUnixSeconds(int64_t s) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromUnixSeconds(int64_t s) { return time_internal::FromUnixDuration(Seconds(s)); } -constexpr Time FromTimeT(time_t t) { +ABSL_ATTRIBUTE_CONST_FUNCTION constexpr Time FromTimeT(time_t t) { return time_internal::FromUnixDuration(Seconds(t)); } diff --git a/absl/time/time_benchmark.cc b/absl/time/time_benchmark.cc index 99e62799..93a7c41b 100644 --- a/absl/time/time_benchmark.cc +++ b/absl/time/time_benchmark.cc @@ -185,9 +185,11 @@ void BM_Time_FromCivil_Absl(benchmark::State& state) { int i = 0; while (state.KeepRunning()) { if ((i & 1) == 0) { - absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz); + benchmark::DoNotOptimize( + absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz)); } else { - absl::FromCivil(absl::CivilSecond(2013, 11, 15, 18, 30, 27), tz); + benchmark::DoNotOptimize( + absl::FromCivil(absl::CivilSecond(2013, 11, 15, 18, 30, 27), tz)); } ++i; } @@ -224,7 +226,8 @@ BENCHMARK(BM_Time_FromCivil_Libc); void BM_Time_FromCivilUTC_Absl(benchmark::State& state) { const absl::TimeZone tz = absl::UTCTimeZone(); while (state.KeepRunning()) { - absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz); + benchmark::DoNotOptimize( + absl::FromCivil(absl::CivilSecond(2014, 12, 18, 20, 16, 18), tz)); } } BENCHMARK(BM_Time_FromCivilUTC_Absl); @@ -235,9 +238,11 @@ void BM_Time_FromCivilDay0_Absl(benchmark::State& state) { int i = 0; while (state.KeepRunning()) { if ((i & 1) == 0) { - absl::FromCivil(absl::CivilSecond(2014, 12, 0, 20, 16, 18), tz); + benchmark::DoNotOptimize( + absl::FromCivil(absl::CivilSecond(2014, 12, 0, 20, 16, 18), tz)); } else { - absl::FromCivil(absl::CivilSecond(2013, 11, 0, 18, 30, 27), tz); + benchmark::DoNotOptimize( + absl::FromCivil(absl::CivilSecond(2013, 11, 0, 18, 30, 27), tz)); } ++i; } -- cgit v1.2.3