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