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;
61 Delta
Multiply(int i
) const;
62 Delta
Multiply(double d
) const;
64 // Returns the later delta of time1 and time2.
65 static Delta
Max(Delta delta1
, Delta delta2
);
69 bool IsInfinite() const;
72 base::TimeDelta delta_
;
74 friend class QuicTime
;
75 friend class QuicClock
;
78 explicit QuicTime(base::TimeTicks ticks
);
80 // Creates a new QuicTime with an internal value of 0. IsInitialized()
81 // will return false for these times.
82 static QuicTime
Zero();
84 // Returns the later time of time1 and time2.
85 static QuicTime
Max(QuicTime time1
, QuicTime time2
);
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;
93 bool IsInitialized() const;
95 QuicTime
Add(const Delta
& delta
) const;
97 QuicTime
Subtract(const Delta
& delta
) const;
99 Delta
Subtract(const QuicTime
& other
) const;
102 friend bool operator==(QuicTime lhs
, QuicTime rhs
);
103 friend bool operator<(QuicTime lhs
, QuicTime rhs
);
105 friend class QuicClock
;
106 friend class QuicClockTest
;
108 base::TimeTicks ticks_
;
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
{
116 // FromUNIXSeconds constructs a QuicWallTime from a count of the seconds
117 // since the UNIX epoch.
118 static QuicWallTime
FromUNIXSeconds(uint64 seconds
);
120 // Zero returns a QuicWallTime set to zero. IsZero will return true for this
122 static QuicWallTime
Zero();
124 // ToUNIXSeconds converts a QuicWallTime into a count of seconds since the
126 uint64
ToUNIXSeconds() const;
128 bool IsAfter(QuicWallTime other
) const;
129 bool IsBefore(QuicWallTime other
) const;
131 // IsZero returns true if this object is the result of calling |Zero|.
134 // AbsoluteDifference returns the absolute value of the time difference
135 // between |this| and |other|.
136 QuicTime::Delta
AbsoluteDifference(QuicWallTime other
) const;
138 // Add returns a new QuicWallTime that represents the time of |this| plus
140 QuicWallTime
Add(QuicTime::Delta delta
) const;
142 // Subtract returns a new QuicWallTime that represents the time of |this|
144 QuicWallTime
Subtract(QuicTime::Delta delta
) const;
147 explicit QuicWallTime(uint64 seconds
);
152 // Non-member relational operators for QuicTime::Delta.
153 inline bool operator==(QuicTime::Delta lhs
, QuicTime::Delta rhs
) {
154 return lhs
.ToMicroseconds() == rhs
.ToMicroseconds();
156 inline bool operator!=(QuicTime::Delta lhs
, QuicTime::Delta rhs
) {
157 return !(lhs
== rhs
);
159 inline bool operator<(QuicTime::Delta lhs
, QuicTime::Delta rhs
) {
160 return lhs
.ToMicroseconds() < rhs
.ToMicroseconds();
162 inline bool operator>(QuicTime::Delta lhs
, QuicTime::Delta rhs
) {
165 inline bool operator<=(QuicTime::Delta lhs
, QuicTime::Delta rhs
) {
168 inline bool operator>=(QuicTime::Delta lhs
, QuicTime::Delta rhs
) {
171 // Non-member relational operators for QuicTime.
172 inline bool operator==(QuicTime lhs
, QuicTime rhs
) {
173 return lhs
.ticks_
== rhs
.ticks_
;
175 inline bool operator!=(QuicTime lhs
, QuicTime rhs
) {
176 return !(lhs
== rhs
);
178 inline bool operator<(QuicTime lhs
, QuicTime rhs
) {
179 return lhs
.ticks_
< rhs
.ticks_
;
181 inline bool operator>(QuicTime lhs
, QuicTime rhs
) {
184 inline bool operator<=(QuicTime lhs
, QuicTime rhs
) {
187 inline bool operator>=(QuicTime lhs
, QuicTime rhs
) {
193 #endif // NET_QUIC_QUIC_TIME_H_