| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 // | |
| 5 // QuicTime represents one point in time, stored in microsecond resolution. | |
| 6 // QuicTime is monotonically increasing, even across system clock adjustments. | |
| 7 // The epoch (time 0) of QuicTime is unspecified. | |
| 8 // | |
| 9 // This implementation wraps the classes base::TimeTicks and base::TimeDelta. | |
| 10 | |
| 11 #ifndef NET_QUIC_QUIC_TIME_H_ | |
| 12 #define NET_QUIC_QUIC_TIME_H_ | |
| 13 | |
| 14 #include <stdint.h> | |
| 15 | |
| 16 #include "base/compiler_specific.h" | |
| 17 #include "base/time/time.h" | |
| 18 #include "net/base/net_export.h" | |
| 19 | |
| 20 #define QUICTIME_CONSTEXPR inline | |
| 21 | |
| 22 namespace net { | |
| 23 | |
| 24 static const int kNumSecondsPerMinute = 60; | |
| 25 static const int kNumSecondsPerHour = kNumSecondsPerMinute * 60; | |
| 26 static const uint64_t kNumMicrosPerSecond = base::Time::kMicrosecondsPerSecond; | |
| 27 static const uint64_t kNumMicrosPerMilli = | |
| 28 base::Time::kMicrosecondsPerMillisecond; | |
| 29 | |
| 30 // A QuicTime is a purely relative time. QuicTime values from different clocks | |
| 31 // cannot be compared to each other. If you need an absolute time, see | |
| 32 // QuicWallTime, below. | |
| 33 class NET_EXPORT_PRIVATE QuicTime { | |
| 34 public: | |
| 35 // A QuicTime::Delta represents the signed difference between two points in | |
| 36 // time, stored in microsecond resolution. | |
| 37 class NET_EXPORT_PRIVATE Delta { | |
| 38 public: | |
| 39 explicit Delta(base::TimeDelta delta); | |
| 40 | |
| 41 // Create a object with an offset of 0. | |
| 42 static QUICTIME_CONSTEXPR Delta Zero() { return Delta(0); } | |
| 43 | |
| 44 // Create a object with infinite offset time. | |
| 45 static QUICTIME_CONSTEXPR Delta Infinite() { | |
| 46 return Delta(kQuicInfiniteTimeUs); | |
| 47 } | |
| 48 | |
| 49 // Converts a number of seconds to a time offset. | |
| 50 static QUICTIME_CONSTEXPR Delta FromSeconds(int64_t secs) { | |
| 51 return Delta(secs * 1000 * 1000); | |
| 52 } | |
| 53 | |
| 54 // Converts a number of milliseconds to a time offset. | |
| 55 static QUICTIME_CONSTEXPR Delta FromMilliseconds(int64_t ms) { | |
| 56 return Delta(ms * 1000); | |
| 57 } | |
| 58 | |
| 59 // Converts a number of microseconds to a time offset. | |
| 60 static QUICTIME_CONSTEXPR Delta FromMicroseconds(int64_t us) { | |
| 61 return Delta(us); | |
| 62 } | |
| 63 | |
| 64 // Converts the time offset to a rounded number of seconds. | |
| 65 inline int64_t ToSeconds() const { return time_offset_ / 1000 / 1000; } | |
| 66 | |
| 67 // Converts the time offset to a rounded number of milliseconds. | |
| 68 inline int64_t ToMilliseconds() const { return time_offset_ / 1000; } | |
| 69 | |
| 70 // Converts the time offset to a rounded number of microseconds. | |
| 71 inline int64_t ToMicroseconds() const { return time_offset_; } | |
| 72 | |
| 73 inline bool IsZero() const { return time_offset_ == 0; } | |
| 74 | |
| 75 inline bool IsInfinite() const { | |
| 76 return time_offset_ == kQuicInfiniteTimeUs; | |
| 77 } | |
| 78 | |
| 79 private: | |
| 80 base::TimeDelta delta_; | |
| 81 friend inline bool operator==(QuicTime::Delta lhs, QuicTime::Delta rhs); | |
| 82 friend inline bool operator<(QuicTime::Delta lhs, QuicTime::Delta rhs); | |
| 83 friend inline QuicTime::Delta operator<<(QuicTime::Delta lhs, size_t rhs); | |
| 84 friend inline QuicTime::Delta operator>>(QuicTime::Delta lhs, size_t rhs); | |
| 85 | |
| 86 friend inline QuicTime::Delta operator+(QuicTime::Delta lhs, | |
| 87 QuicTime::Delta rhs); | |
| 88 friend inline QuicTime::Delta operator-(QuicTime::Delta lhs, | |
| 89 QuicTime::Delta rhs); | |
| 90 friend inline QuicTime::Delta operator*(QuicTime::Delta lhs, int rhs); | |
| 91 friend inline QuicTime::Delta operator*(QuicTime::Delta lhs, double rhs); | |
| 92 | |
| 93 friend inline QuicTime operator+(QuicTime lhs, QuicTime::Delta rhs); | |
| 94 friend inline QuicTime operator-(QuicTime lhs, QuicTime::Delta rhs); | |
| 95 friend inline QuicTime::Delta operator-(QuicTime lhs, QuicTime rhs); | |
| 96 | |
| 97 static const int64_t kQuicInfiniteTimeUs = | |
| 98 std::numeric_limits<int64_t>::max(); | |
| 99 | |
| 100 explicit QUICTIME_CONSTEXPR Delta(int64_t time_offset) | |
| 101 : time_offset_(time_offset) {} | |
| 102 | |
| 103 int64_t time_offset_; | |
| 104 friend class QuicTime; | |
| 105 friend class QuicClock; | |
| 106 }; | |
| 107 | |
| 108 explicit QuicTime(base::TimeTicks ticks) : time_(ticks.ToInternalValue()) {} | |
| 109 | |
| 110 // Creates a new QuicTime with an internal value of 0. IsInitialized() | |
| 111 // will return false for these times. | |
| 112 static QUICTIME_CONSTEXPR QuicTime Zero() { return QuicTime(0); } | |
| 113 | |
| 114 // Creates a new QuicTime with an infinite time. | |
| 115 static QUICTIME_CONSTEXPR QuicTime Infinite() { | |
| 116 return QuicTime(Delta::kQuicInfiniteTimeUs); | |
| 117 } | |
| 118 | |
| 119 // Produce the internal value to be used when logging. This value | |
| 120 // represents the number of microseconds since some epoch. It may | |
| 121 // be the UNIX epoch on some platforms. On others, it may | |
| 122 // be a CPU ticks based value. | |
| 123 inline int64_t ToDebuggingValue() const { return time_; } | |
| 124 | |
| 125 inline bool IsInitialized() const { return 0 != time_; } | |
| 126 | |
| 127 private: | |
| 128 friend inline bool operator==(QuicTime lhs, QuicTime rhs); | |
| 129 friend inline bool operator<(QuicTime lhs, QuicTime rhs); | |
| 130 friend inline QuicTime operator+(QuicTime lhs, QuicTime::Delta rhs); | |
| 131 friend inline QuicTime operator-(QuicTime lhs, QuicTime::Delta rhs); | |
| 132 friend inline QuicTime::Delta operator-(QuicTime lhs, QuicTime rhs); | |
| 133 | |
| 134 explicit QUICTIME_CONSTEXPR QuicTime(int64_t time) : time_(time) {} | |
| 135 | |
| 136 int64_t time_; | |
| 137 }; | |
| 138 | |
| 139 // A QuicWallTime represents an absolute time that is globally consistent. In | |
| 140 // practice, clock-skew means that comparing values from different machines | |
| 141 // requires some flexibility. | |
| 142 class NET_EXPORT_PRIVATE QuicWallTime { | |
| 143 public: | |
| 144 // FromUNIXSeconds constructs a QuicWallTime from a count of the seconds | |
| 145 // since the UNIX epoch. | |
| 146 static QUICTIME_CONSTEXPR QuicWallTime FromUNIXSeconds(uint64_t seconds) { | |
| 147 return QuicWallTime(seconds * 1000000); | |
| 148 } | |
| 149 | |
| 150 static QUICTIME_CONSTEXPR QuicWallTime | |
| 151 FromUNIXMicroseconds(uint64_t microseconds) { | |
| 152 return QuicWallTime(microseconds); | |
| 153 } | |
| 154 | |
| 155 // Zero returns a QuicWallTime set to zero. IsZero will return true for this | |
| 156 // value. | |
| 157 static QUICTIME_CONSTEXPR QuicWallTime Zero() { return QuicWallTime(0); } | |
| 158 | |
| 159 // Returns the number of seconds since the UNIX epoch. | |
| 160 uint64_t ToUNIXSeconds() const; | |
| 161 // Returns the number of microseconds since the UNIX epoch. | |
| 162 uint64_t ToUNIXMicroseconds() const; | |
| 163 | |
| 164 bool IsAfter(QuicWallTime other) const; | |
| 165 bool IsBefore(QuicWallTime other) const; | |
| 166 | |
| 167 // IsZero returns true if this object is the result of calling |Zero|. | |
| 168 bool IsZero() const; | |
| 169 | |
| 170 // AbsoluteDifference returns the absolute value of the time difference | |
| 171 // between |this| and |other|. | |
| 172 QuicTime::Delta AbsoluteDifference(QuicWallTime other) const; | |
| 173 | |
| 174 // Add returns a new QuicWallTime that represents the time of |this| plus | |
| 175 // |delta|. | |
| 176 QuicWallTime Add(QuicTime::Delta delta) const WARN_UNUSED_RESULT; | |
| 177 | |
| 178 // Subtract returns a new QuicWallTime that represents the time of |this| | |
| 179 // minus |delta|. | |
| 180 QuicWallTime Subtract(QuicTime::Delta delta) const WARN_UNUSED_RESULT; | |
| 181 | |
| 182 private: | |
| 183 explicit QUICTIME_CONSTEXPR QuicWallTime(uint64_t microseconds) | |
| 184 : microseconds_(microseconds) {} | |
| 185 | |
| 186 uint64_t microseconds_; | |
| 187 }; | |
| 188 | |
| 189 // Non-member relational operators for QuicTime::Delta. | |
| 190 inline bool operator==(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 191 return lhs.time_offset_ == rhs.time_offset_; | |
| 192 } | |
| 193 inline bool operator!=(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 194 return !(lhs == rhs); | |
| 195 } | |
| 196 inline bool operator<(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 197 return lhs.time_offset_ < rhs.time_offset_; | |
| 198 } | |
| 199 inline bool operator>(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 200 return rhs < lhs; | |
| 201 } | |
| 202 inline bool operator<=(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 203 return !(rhs < lhs); | |
| 204 } | |
| 205 inline bool operator>=(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 206 return !(lhs < rhs); | |
| 207 } | |
| 208 inline QuicTime::Delta operator<<(QuicTime::Delta lhs, size_t rhs) { | |
| 209 return QuicTime::Delta(lhs.time_offset_ << rhs); | |
| 210 } | |
| 211 inline QuicTime::Delta operator>>(QuicTime::Delta lhs, size_t rhs) { | |
| 212 return QuicTime::Delta(lhs.time_offset_ >> rhs); | |
| 213 } | |
| 214 | |
| 215 // Non-member relational operators for QuicTime. | |
| 216 inline bool operator==(QuicTime lhs, QuicTime rhs) { | |
| 217 return lhs.time_ == rhs.time_; | |
| 218 } | |
| 219 inline bool operator!=(QuicTime lhs, QuicTime rhs) { | |
| 220 return !(lhs == rhs); | |
| 221 } | |
| 222 inline bool operator<(QuicTime lhs, QuicTime rhs) { | |
| 223 return lhs.time_ < rhs.time_; | |
| 224 } | |
| 225 inline bool operator>(QuicTime lhs, QuicTime rhs) { | |
| 226 return rhs < lhs; | |
| 227 } | |
| 228 inline bool operator<=(QuicTime lhs, QuicTime rhs) { | |
| 229 return !(rhs < lhs); | |
| 230 } | |
| 231 inline bool operator>=(QuicTime lhs, QuicTime rhs) { | |
| 232 return !(lhs < rhs); | |
| 233 } | |
| 234 | |
| 235 // Non-member arithmetic operators for QuicTime::Delta. | |
| 236 inline QuicTime::Delta operator+(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 237 return QuicTime::Delta(lhs.time_offset_ + rhs.time_offset_); | |
| 238 } | |
| 239 inline QuicTime::Delta operator-(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 240 return QuicTime::Delta(lhs.time_offset_ - rhs.time_offset_); | |
| 241 } | |
| 242 inline QuicTime::Delta operator*(QuicTime::Delta lhs, int rhs) { | |
| 243 return QuicTime::Delta(lhs.time_offset_ * rhs); | |
| 244 } | |
| 245 inline QuicTime::Delta operator*(QuicTime::Delta lhs, double rhs) { | |
| 246 return QuicTime::Delta(lhs.time_offset_ * rhs); | |
| 247 } | |
| 248 inline QuicTime::Delta operator*(int lhs, QuicTime::Delta rhs) { | |
| 249 return rhs * lhs; | |
| 250 } | |
| 251 inline QuicTime::Delta operator*(double lhs, QuicTime::Delta rhs) { | |
| 252 return rhs * lhs; | |
| 253 } | |
| 254 | |
| 255 // Non-member arithmetic operators for QuicTime and QuicTime::Delta. | |
| 256 inline QuicTime operator+(QuicTime lhs, QuicTime::Delta rhs) { | |
| 257 return QuicTime(lhs.time_ + rhs.time_offset_); | |
| 258 } | |
| 259 inline QuicTime operator-(QuicTime lhs, QuicTime::Delta rhs) { | |
| 260 return QuicTime(lhs.time_ - rhs.time_offset_); | |
| 261 } | |
| 262 inline QuicTime::Delta operator-(QuicTime lhs, QuicTime rhs) { | |
| 263 return QuicTime::Delta(lhs.time_ - rhs.time_); | |
| 264 } | |
| 265 | |
| 266 } // namespace net | |
| 267 | |
| 268 #endif // NET_QUIC_QUIC_TIME_H_ | |
| OLD | NEW |