| 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 "base/basictypes.h" | |
| 15 #include "base/time/time.h" | |
| 16 #include "net/base/net_export.h" | |
| 17 | |
| 18 namespace net { | |
| 19 | |
| 20 static const int kNumSecondsPerMinute = 60; | |
| 21 static const int kNumSecondsPerHour = kNumSecondsPerMinute * 60; | |
| 22 static const uint64 kNumMicrosPerSecond = base::Time::kMicrosecondsPerSecond; | |
| 23 static const uint64 kNumMicrosPerMilli = | |
| 24 base::Time::kMicrosecondsPerMillisecond; | |
| 25 | |
| 26 // A QuicTime is a purely relative time. QuicTime values from different clocks | |
| 27 // cannot be compared to each other. If you need an absolute time, see | |
| 28 // QuicWallTime, below. | |
| 29 class NET_EXPORT_PRIVATE QuicTime { | |
| 30 public: | |
| 31 // A QuicTime::Delta represents the signed difference between two points in | |
| 32 // time, stored in microsecond resolution. | |
| 33 class NET_EXPORT_PRIVATE Delta { | |
| 34 public: | |
| 35 explicit Delta(base::TimeDelta delta); | |
| 36 | |
| 37 // Create a object with an offset of 0. | |
| 38 static Delta Zero(); | |
| 39 | |
| 40 // Create a object with infinite offset time. | |
| 41 static Delta Infinite(); | |
| 42 | |
| 43 // Converts a number of seconds to a time offset. | |
| 44 static Delta FromSeconds(int64 secs); | |
| 45 | |
| 46 // Converts a number of milliseconds to a time offset. | |
| 47 static Delta FromMilliseconds(int64 ms); | |
| 48 | |
| 49 // Converts a number of microseconds to a time offset. | |
| 50 static Delta FromMicroseconds(int64 us); | |
| 51 | |
| 52 // Converts the time offset to a rounded number of seconds. | |
| 53 int64 ToSeconds() const; | |
| 54 | |
| 55 // Converts the time offset to a rounded number of milliseconds. | |
| 56 int64 ToMilliseconds() const; | |
| 57 | |
| 58 // Converts the time offset to a rounded number of microseconds. | |
| 59 int64 ToMicroseconds() const; | |
| 60 | |
| 61 Delta Add(const Delta& delta) const; | |
| 62 | |
| 63 Delta Subtract(const Delta& delta) const; | |
| 64 | |
| 65 Delta Multiply(int i) const; | |
| 66 Delta Multiply(double d) const; | |
| 67 | |
| 68 // Returns the later delta of time1 and time2. | |
| 69 static Delta Max(Delta delta1, Delta delta2); | |
| 70 | |
| 71 bool IsZero() const; | |
| 72 | |
| 73 bool IsInfinite() const; | |
| 74 | |
| 75 private: | |
| 76 base::TimeDelta delta_; | |
| 77 | |
| 78 friend class QuicTime; | |
| 79 friend class QuicClock; | |
| 80 }; | |
| 81 | |
| 82 explicit QuicTime(base::TimeTicks ticks); | |
| 83 | |
| 84 // Creates a new QuicTime with an internal value of 0. IsInitialized() | |
| 85 // will return false for these times. | |
| 86 static QuicTime Zero(); | |
| 87 | |
| 88 // Creates a new QuicTime with an infinite time. | |
| 89 static QuicTime Infinite(); | |
| 90 | |
| 91 // Returns the later time of time1 and time2. | |
| 92 static QuicTime Max(QuicTime time1, QuicTime time2); | |
| 93 | |
| 94 // Produce the internal value to be used when logging. This value | |
| 95 // represents the number of microseconds since some epoch. It may | |
| 96 // be the UNIX epoch on some platforms. On others, it may | |
| 97 // be a CPU ticks based value. | |
| 98 int64 ToDebuggingValue() const; | |
| 99 | |
| 100 bool IsInitialized() const; | |
| 101 | |
| 102 QuicTime Add(const Delta& delta) const; | |
| 103 | |
| 104 QuicTime Subtract(const Delta& delta) const; | |
| 105 | |
| 106 Delta Subtract(const QuicTime& other) const; | |
| 107 | |
| 108 private: | |
| 109 friend bool operator==(QuicTime lhs, QuicTime rhs); | |
| 110 friend bool operator<(QuicTime lhs, QuicTime rhs); | |
| 111 | |
| 112 friend class QuicClock; | |
| 113 friend class QuicClockTest; | |
| 114 | |
| 115 base::TimeTicks ticks_; | |
| 116 }; | |
| 117 | |
| 118 // A QuicWallTime represents an absolute time that is globally consistent. It | |
| 119 // provides, at most, one second granularity and, in practice, clock-skew means | |
| 120 // that you shouldn't even depend on that. | |
| 121 class NET_EXPORT_PRIVATE QuicWallTime { | |
| 122 public: | |
| 123 // FromUNIXSeconds constructs a QuicWallTime from a count of the seconds | |
| 124 // since the UNIX epoch. | |
| 125 static QuicWallTime FromUNIXSeconds(uint64 seconds); | |
| 126 | |
| 127 // Zero returns a QuicWallTime set to zero. IsZero will return true for this | |
| 128 // value. | |
| 129 static QuicWallTime Zero(); | |
| 130 | |
| 131 // ToUNIXSeconds converts a QuicWallTime into a count of seconds since the | |
| 132 // UNIX epoch. | |
| 133 uint64 ToUNIXSeconds() const; | |
| 134 | |
| 135 bool IsAfter(QuicWallTime other) const; | |
| 136 bool IsBefore(QuicWallTime other) const; | |
| 137 | |
| 138 // IsZero returns true if this object is the result of calling |Zero|. | |
| 139 bool IsZero() const; | |
| 140 | |
| 141 // AbsoluteDifference returns the absolute value of the time difference | |
| 142 // between |this| and |other|. | |
| 143 QuicTime::Delta AbsoluteDifference(QuicWallTime other) const; | |
| 144 | |
| 145 // Add returns a new QuicWallTime that represents the time of |this| plus | |
| 146 // |delta|. | |
| 147 QuicWallTime Add(QuicTime::Delta delta) const; | |
| 148 | |
| 149 // Subtract returns a new QuicWallTime that represents the time of |this| | |
| 150 // minus |delta|. | |
| 151 QuicWallTime Subtract(QuicTime::Delta delta) const; | |
| 152 | |
| 153 private: | |
| 154 explicit QuicWallTime(uint64 seconds); | |
| 155 | |
| 156 uint64 seconds_; | |
| 157 }; | |
| 158 | |
| 159 // Non-member relational operators for QuicTime::Delta. | |
| 160 inline bool operator==(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 161 return lhs.ToMicroseconds() == rhs.ToMicroseconds(); | |
| 162 } | |
| 163 inline bool operator!=(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 164 return !(lhs == rhs); | |
| 165 } | |
| 166 inline bool operator<(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 167 return lhs.ToMicroseconds() < rhs.ToMicroseconds(); | |
| 168 } | |
| 169 inline bool operator>(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 170 return rhs < lhs; | |
| 171 } | |
| 172 inline bool operator<=(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 173 return !(rhs < lhs); | |
| 174 } | |
| 175 inline bool operator>=(QuicTime::Delta lhs, QuicTime::Delta rhs) { | |
| 176 return !(lhs < rhs); | |
| 177 } | |
| 178 // Non-member relational operators for QuicTime. | |
| 179 inline bool operator==(QuicTime lhs, QuicTime rhs) { | |
| 180 return lhs.ticks_ == rhs.ticks_; | |
| 181 } | |
| 182 inline bool operator!=(QuicTime lhs, QuicTime rhs) { | |
| 183 return !(lhs == rhs); | |
| 184 } | |
| 185 inline bool operator<(QuicTime lhs, QuicTime rhs) { | |
| 186 return lhs.ticks_ < rhs.ticks_; | |
| 187 } | |
| 188 inline bool operator>(QuicTime lhs, QuicTime rhs) { | |
| 189 return rhs < lhs; | |
| 190 } | |
| 191 inline bool operator<=(QuicTime lhs, QuicTime rhs) { | |
| 192 return !(rhs < lhs); | |
| 193 } | |
| 194 inline bool operator>=(QuicTime lhs, QuicTime rhs) { | |
| 195 return !(lhs < rhs); | |
| 196 } | |
| 197 | |
| 198 } // namespace net | |
| 199 | |
| 200 #endif // NET_QUIC_QUIC_TIME_H_ | |
| OLD | NEW |