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.
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.
9 // This implementation wraps the classes base::TimeTicks and base::TimeDelta.
11 #ifndef NET_QUIC_QUIC_TIME_H_
12 #define NET_QUIC_QUIC_TIME_H_
14 #include "base/basictypes.h"
15 #include "base/time/time.h"
16 #include "net/base/net_export.h"
20 static const uint64 kNumMicrosPerSecond = base::Time::kMicrosecondsPerSecond;
21 static const uint64 kNumMicrosPerMilli =
22 base::Time::kMicrosecondsPerMillisecond;
24 // A QuicTime is a purely relative time. QuicTime values from different clocks
25 // cannot be compared to each other. If you need an absolute time, see
26 // QuicWallTime, below.
27 class NET_EXPORT_PRIVATE QuicTime {
29 // A QuicTime::Delta represents the signed difference between two points in
30 // time, stored in microsecond resolution.
31 class NET_EXPORT_PRIVATE Delta {
33 explicit Delta(base::TimeDelta delta);
35 // Create a object with an offset of 0.
38 // Create a object with infinite offset time.
39 static Delta Infinite();
41 // Converts a number of seconds to a time offset.
42 static Delta FromSeconds(int64 secs);
44 // Converts a number of milliseconds to a time offset.
45 static Delta FromMilliseconds(int64 ms);
47 // Converts a number of microseconds to a time offset.
48 static Delta FromMicroseconds(int64 us);
50 // Converts the time offset to a rounded number of seconds.
51 int64 ToSeconds() const;
53 // Converts the time offset to a rounded number of milliseconds.
54 int64 ToMilliseconds() const;
56 // Converts the time offset to a rounded number of microseconds.
57 int64 ToMicroseconds() const;
59 Delta Add(const Delta& delta) const;
61 Delta Subtract(const Delta& delta) const;
63 Delta Multiply(int i) const;
64 Delta Multiply(double d) const;
66 // Returns the later delta of time1 and time2.
67 static Delta Max(Delta delta1, Delta delta2);
71 bool IsInfinite() const;
74 base::TimeDelta delta_;
76 friend class QuicTime;
77 friend class QuicClock;
80 explicit QuicTime(base::TimeTicks ticks);
82 // Creates a new QuicTime with an internal value of 0. IsInitialized()
83 // will return false for these times.
84 static QuicTime Zero();
86 // Creates a new QuicTime with an infinite time.
87 static QuicTime Infinite();
89 // Returns the later time of time1 and time2.
90 static QuicTime Max(QuicTime time1, QuicTime time2);
92 // Produce the internal value to be used when logging. This value
93 // represents the number of microseconds since some epoch. It may
94 // be the UNIX epoch on some platforms. On others, it may
95 // be a CPU ticks based value.
96 int64 ToDebuggingValue() const;
98 bool IsInitialized() const;
100 QuicTime Add(const Delta& delta) const;
102 QuicTime Subtract(const Delta& delta) const;
104 Delta Subtract(const QuicTime& other) const;
107 friend bool operator==(QuicTime lhs, QuicTime rhs);
108 friend bool operator<(QuicTime lhs, QuicTime rhs);
110 friend class QuicClock;
111 friend class QuicClockTest;
113 base::TimeTicks ticks_;
116 // A QuicWallTime represents an absolute time that is globally consistent. It
117 // provides, at most, one second granularity and, in practice, clock-skew means
118 // that you shouldn't even depend on that.
119 class NET_EXPORT_PRIVATE QuicWallTime {
121 // FromUNIXSeconds constructs a QuicWallTime from a count of the seconds
122 // since the UNIX epoch.
123 static QuicWallTime FromUNIXSeconds(uint64 seconds);
125 // Zero returns a QuicWallTime set to zero. IsZero will return true for this
127 static QuicWallTime Zero();
129 // ToUNIXSeconds converts a QuicWallTime into a count of seconds since the
131 uint64 ToUNIXSeconds() const;
133 bool IsAfter(QuicWallTime other) const;
134 bool IsBefore(QuicWallTime other) const;
136 // IsZero returns true if this object is the result of calling |Zero|.
139 // AbsoluteDifference returns the absolute value of the time difference
140 // between |this| and |other|.
141 QuicTime::Delta AbsoluteDifference(QuicWallTime other) const;
143 // Add returns a new QuicWallTime that represents the time of |this| plus
145 QuicWallTime Add(QuicTime::Delta delta) const;
147 // Subtract returns a new QuicWallTime that represents the time of |this|
149 QuicWallTime Subtract(QuicTime::Delta delta) const;
152 explicit QuicWallTime(uint64 seconds);
157 // Non-member relational operators for QuicTime::Delta.
158 inline bool operator==(QuicTime::Delta lhs, QuicTime::Delta rhs) {
159 return lhs.ToMicroseconds() == rhs.ToMicroseconds();
161 inline bool operator!=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
162 return !(lhs == rhs);
164 inline bool operator<(QuicTime::Delta lhs, QuicTime::Delta rhs) {
165 return lhs.ToMicroseconds() < rhs.ToMicroseconds();
167 inline bool operator>(QuicTime::Delta lhs, QuicTime::Delta rhs) {
170 inline bool operator<=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
173 inline bool operator>=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
176 // Non-member relational operators for QuicTime.
177 inline bool operator==(QuicTime lhs, QuicTime rhs) {
178 return lhs.ticks_ == rhs.ticks_;
180 inline bool operator!=(QuicTime lhs, QuicTime rhs) {
181 return !(lhs == rhs);
183 inline bool operator<(QuicTime lhs, QuicTime rhs) {
184 return lhs.ticks_ < rhs.ticks_;
186 inline bool operator>(QuicTime lhs, QuicTime rhs) {
189 inline bool operator<=(QuicTime lhs, QuicTime rhs) {
192 inline bool operator>=(QuicTime lhs, QuicTime rhs) {
198 #endif // NET_QUIC_QUIC_TIME_H_