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