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;
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 {
27 // A QuicTime::Delta represents the signed difference between two points in
28 // time, stored in microsecond resolution.
29 class NET_EXPORT_PRIVATE Delta {
31 explicit Delta(base::TimeDelta delta);
33 // Create a object with an offset of 0.
36 // Create a object with infinite offset time.
37 static Delta Infinite();
39 // Converts a number of seconds to a time offset.
40 static Delta FromSeconds(int64 secs);
42 // Converts a number of milliseconds to a time offset.
43 static Delta FromMilliseconds(int64 ms);
45 // Converts a number of microseconds to a time offset.
46 static Delta FromMicroseconds(int64 us);
48 // Converts the time offset to a rounded number of seconds.
49 int64 ToSeconds() const;
51 // Converts the time offset to a rounded number of milliseconds.
52 int64 ToMilliseconds() const;
54 // Converts the time offset to a rounded number of microseconds.
55 int64 ToMicroseconds() const;
57 Delta Add(const Delta& delta) const;
59 Delta Subtract(const Delta& delta) const;
63 bool IsInfinite() const;
66 base::TimeDelta delta_;
68 friend class QuicTime;
69 friend class QuicClock;
72 explicit QuicTime(base::TimeTicks ticks);
74 // Creates a new QuicTime with an internal value of 0. IsInitialized()
75 // will return false for these times.
76 static QuicTime Zero();
78 // Produce the internal value to be used when logging. This value
79 // represents the number of microseconds since some epoch. It may
80 // be the UNIX epoch on some platforms. On others, it may
81 // be a CPU ticks based value.
82 int64 ToDebuggingValue() const;
84 bool IsInitialized() const;
86 QuicTime Add(const Delta& delta) const;
88 QuicTime Subtract(const Delta& delta) const;
90 Delta Subtract(const QuicTime& other) const;
93 friend bool operator==(QuicTime lhs, QuicTime rhs);
94 friend bool operator<(QuicTime lhs, QuicTime rhs);
96 friend class QuicClock;
97 friend class QuicClockTest;
99 base::TimeTicks ticks_;
102 // A QuicWallTime represents an absolute time that is globally consistent. It
103 // provides, at most, one second granularity and, in practice, clock-skew means
104 // that you shouldn't even depend on that.
105 class NET_EXPORT_PRIVATE QuicWallTime {
107 // FromUNIXSeconds constructs a QuicWallTime from a count of the seconds
108 // since the UNIX epoch.
109 static QuicWallTime FromUNIXSeconds(uint64 seconds);
111 // Zero returns a QuicWallTime set to zero. IsZero will return true for this
113 static QuicWallTime Zero();
115 // ToUNIXSeconds converts a QuicWallTime into a count of seconds since the
117 uint64 ToUNIXSeconds() const;
119 bool IsAfter(QuicWallTime other) const;
120 bool IsBefore(QuicWallTime other) const;
122 // IsZero returns true if this object is the result of calling |Zero|.
125 // AbsoluteDifference returns the absolute value of the time difference
126 // between |this| and |other|.
127 QuicTime::Delta AbsoluteDifference(QuicWallTime other) const;
129 // Add returns a new QuicWallTime that represents the time of |this| plus
131 QuicWallTime Add(QuicTime::Delta delta) const;
133 // Subtract returns a new QuicWallTime that represents the time of |this|
135 QuicWallTime Subtract(QuicTime::Delta delta) const;
138 explicit QuicWallTime(uint64 seconds);
143 // Non-member relational operators for QuicTime::Delta.
144 inline bool operator==(QuicTime::Delta lhs, QuicTime::Delta rhs) {
145 return lhs.ToMicroseconds() == rhs.ToMicroseconds();
147 inline bool operator!=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
148 return !(lhs == rhs);
150 inline bool operator<(QuicTime::Delta lhs, QuicTime::Delta rhs) {
151 return lhs.ToMicroseconds() < rhs.ToMicroseconds();
153 inline bool operator>(QuicTime::Delta lhs, QuicTime::Delta rhs) {
156 inline bool operator<=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
159 inline bool operator>=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
162 // Non-member relational operators for QuicTime.
163 inline bool operator==(QuicTime lhs, QuicTime rhs) {
164 return lhs.ticks_ == rhs.ticks_;
166 inline bool operator!=(QuicTime lhs, QuicTime rhs) {
167 return !(lhs == rhs);
169 inline bool operator<(QuicTime lhs, QuicTime rhs) {
170 return lhs.ticks_ < rhs.ticks_;
172 inline bool operator>(QuicTime lhs, QuicTime rhs) {
175 inline bool operator<=(QuicTime lhs, QuicTime rhs) {
178 inline bool operator>=(QuicTime lhs, QuicTime rhs) {
184 #endif // NET_QUIC_QUIC_TIME_H_