Commit 1f8da02d by Abseil Team Committed by Copybara-Service

Tagged most functions in absl/time/time.h as ABSL_ATTRIBUTE_CONST_FUNCTION or…

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