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 // Returns the later time of time1 and time2. 85 static QuicTime Max(QuicTime time1, QuicTime time2); 86 87 // Produce the internal value to be used when logging. This value 88 // represents the number of microseconds since some epoch. It may 89 // be the UNIX epoch on some platforms. On others, it may 90 // be a CPU ticks based value. 91 int64 ToDebuggingValue() const; 92 93 bool IsInitialized() const; 94 95 QuicTime Add(const Delta& delta) const; 96 97 QuicTime Subtract(const Delta& delta) const; 98 99 Delta Subtract(const QuicTime& other) const; 100 101 private: 102 friend bool operator==(QuicTime lhs, QuicTime rhs); 103 friend bool operator<(QuicTime lhs, QuicTime rhs); 104 105 friend class QuicClock; 106 friend class QuicClockTest; 107 108 base::TimeTicks ticks_; 109 }; 110 111 // A QuicWallTime represents an absolute time that is globally consistent. It 112 // provides, at most, one second granularity and, in practice, clock-skew means 113 // that you shouldn't even depend on that. 114 class NET_EXPORT_PRIVATE QuicWallTime { 115 public: 116 // FromUNIXSeconds constructs a QuicWallTime from a count of the seconds 117 // since the UNIX epoch. 118 static QuicWallTime FromUNIXSeconds(uint64 seconds); 119 120 // Zero returns a QuicWallTime set to zero. IsZero will return true for this 121 // value. 122 static QuicWallTime Zero(); 123 124 // ToUNIXSeconds converts a QuicWallTime into a count of seconds since the 125 // UNIX epoch. 126 uint64 ToUNIXSeconds() const; 127 128 bool IsAfter(QuicWallTime other) const; 129 bool IsBefore(QuicWallTime other) const; 130 131 // IsZero returns true if this object is the result of calling |Zero|. 132 bool IsZero() const; 133 134 // AbsoluteDifference returns the absolute value of the time difference 135 // between |this| and |other|. 136 QuicTime::Delta AbsoluteDifference(QuicWallTime other) const; 137 138 // Add returns a new QuicWallTime that represents the time of |this| plus 139 // |delta|. 140 QuicWallTime Add(QuicTime::Delta delta) const; 141 142 // Subtract returns a new QuicWallTime that represents the time of |this| 143 // minus |delta|. 144 QuicWallTime Subtract(QuicTime::Delta delta) const; 145 146 private: 147 explicit QuicWallTime(uint64 seconds); 148 149 uint64 seconds_; 150 }; 151 152 // Non-member relational operators for QuicTime::Delta. 153 inline bool operator==(QuicTime::Delta lhs, QuicTime::Delta rhs) { 154 return lhs.ToMicroseconds() == rhs.ToMicroseconds(); 155 } 156 inline bool operator!=(QuicTime::Delta lhs, QuicTime::Delta rhs) { 157 return !(lhs == rhs); 158 } 159 inline bool operator<(QuicTime::Delta lhs, QuicTime::Delta rhs) { 160 return lhs.ToMicroseconds() < rhs.ToMicroseconds(); 161 } 162 inline bool operator>(QuicTime::Delta lhs, QuicTime::Delta rhs) { 163 return rhs < lhs; 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 !(lhs < rhs); 170 } 171 // Non-member relational operators for QuicTime. 172 inline bool operator==(QuicTime lhs, QuicTime rhs) { 173 return lhs.ticks_ == rhs.ticks_; 174 } 175 inline bool operator!=(QuicTime lhs, QuicTime rhs) { 176 return !(lhs == rhs); 177 } 178 inline bool operator<(QuicTime lhs, QuicTime rhs) { 179 return lhs.ticks_ < rhs.ticks_; 180 } 181 inline bool operator>(QuicTime lhs, QuicTime rhs) { 182 return rhs < lhs; 183 } 184 inline bool operator<=(QuicTime lhs, QuicTime rhs) { 185 return !(rhs < lhs); 186 } 187 inline bool operator>=(QuicTime lhs, QuicTime rhs) { 188 return !(lhs < rhs); 189 } 190 191 } // namespace net 192 193 #endif // NET_QUIC_QUIC_TIME_H_ 194