93 lines · c
1// Mimic the implementation of absl::Duration2namespace absl {3 4using int64_t = long long int;5 6class Duration {7public:8 Duration &operator*=(int64_t r);9 Duration &operator*=(float r);10 Duration &operator*=(double r);11 template <typename T> Duration &operator*=(T r);12 13 Duration &operator/=(int64_t r);14 Duration &operator/=(float r);15 Duration &operator/=(double r);16 template <typename T> Duration &operator/=(T r);17 18 Duration &operator+(Duration d);19};20 21template <typename T> Duration operator*(Duration lhs, T rhs);22template <typename T> Duration operator*(T lhs, Duration rhs);23template <typename T> Duration operator/(Duration lhs, T rhs);24int64_t operator/(Duration lhs, Duration rhs);25 26class Time{};27 28constexpr Duration Nanoseconds(long long);29constexpr Duration Microseconds(long long);30constexpr Duration Milliseconds(long long);31constexpr Duration Seconds(long long);32constexpr Duration Minutes(long long);33constexpr Duration Hours(long long);34 35template <typename T> struct EnableIfFloatImpl {};36template <> struct EnableIfFloatImpl<float> { typedef int Type; };37template <> struct EnableIfFloatImpl<double> { typedef int Type; };38template <> struct EnableIfFloatImpl<long double> { typedef int Type; };39template <typename T> using EnableIfFloat = typename EnableIfFloatImpl<T>::Type;40 41template <typename T, EnableIfFloat<T> = 0> Duration Nanoseconds(T n);42template <typename T, EnableIfFloat<T> = 0> Duration Microseconds(T n);43template <typename T, EnableIfFloat<T> = 0> Duration Milliseconds(T n);44template <typename T, EnableIfFloat<T> = 0> Duration Seconds(T n);45template <typename T, EnableIfFloat<T> = 0> Duration Minutes(T n);46template <typename T, EnableIfFloat<T> = 0> Duration Hours(T n);47 48double ToDoubleHours(Duration d);49double ToDoubleMinutes(Duration d);50double ToDoubleSeconds(Duration d);51double ToDoubleMilliseconds(Duration d);52double ToDoubleMicroseconds(Duration d);53double ToDoubleNanoseconds(Duration d);54int64_t ToInt64Hours(Duration d);55int64_t ToInt64Minutes(Duration d);56int64_t ToInt64Seconds(Duration d);57int64_t ToInt64Milliseconds(Duration d);58int64_t ToInt64Microseconds(Duration d);59int64_t ToInt64Nanoseconds(Duration d);60 61int64_t ToUnixHours(Time t);62int64_t ToUnixMinutes(Time t);63int64_t ToUnixSeconds(Time t);64int64_t ToUnixMillis(Time t);65int64_t ToUnixMicros(Time t);66int64_t ToUnixNanos(Time t);67Time FromUnixHours(int64_t);68Time FromUnixMinutes(int64_t);69Time FromUnixSeconds(int64_t);70Time FromUnixMillis(int64_t);71Time FromUnixMicros(int64_t);72Time FromUnixNanos(int64_t);73 74Time Now();75 76// Relational Operators77constexpr bool operator<(Duration lhs, Duration rhs);78constexpr bool operator>(Duration lhs, Duration rhs);79constexpr bool operator>=(Duration lhs, Duration rhs);80constexpr bool operator<=(Duration lhs, Duration rhs);81constexpr bool operator==(Duration lhs, Duration rhs);82constexpr bool operator!=(Duration lhs, Duration rhs);83 84// Additive Operators85inline Time operator+(Time lhs, Duration rhs);86inline Time operator+(Duration lhs, Time rhs);87inline Time operator-(Time lhs, Duration rhs);88inline Duration operator-(Time lhs, Time rhs);89 90double FDivDuration(Duration num, Duration den);91 92} // namespace absl93