[ServiceWorker] Add unittests for ServiceWorkerWriteToCacheJob.
[chromium-blink-merge.git] / net / quic / quic_time.h
blob5fbe30cce9890ee5055369203791af9803e0613b
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.
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"
18 namespace net {
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 {
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);
33 // Create a object with an offset of 0.
34 static Delta Zero();
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);
67 bool IsZero() const;
69 bool IsInfinite() const;
71 private:
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 // Creates a new QuicTime with an infinite time.
85 static QuicTime Infinite();
87 // Returns the later time of time1 and time2.
88 static QuicTime Max(QuicTime time1, QuicTime time2);
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;
96 bool IsInitialized() const;
98 QuicTime Add(const Delta& delta) const;
100 QuicTime Subtract(const Delta& delta) const;
102 Delta Subtract(const QuicTime& other) const;
104 private:
105 friend bool operator==(QuicTime lhs, QuicTime rhs);
106 friend bool operator<(QuicTime lhs, QuicTime rhs);
108 friend class QuicClock;
109 friend class QuicClockTest;
111 base::TimeTicks ticks_;
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);
123 // Zero returns a QuicWallTime set to zero. IsZero will return true for this
124 // value.
125 static QuicWallTime Zero();
127 // ToUNIXSeconds converts a QuicWallTime into a count of seconds since the
128 // UNIX epoch.
129 uint64 ToUNIXSeconds() const;
131 bool IsAfter(QuicWallTime other) const;
132 bool IsBefore(QuicWallTime other) const;
134 // IsZero returns true if this object is the result of calling |Zero|.
135 bool IsZero() const;
137 // AbsoluteDifference returns the absolute value of the time difference
138 // between |this| and |other|.
139 QuicTime::Delta AbsoluteDifference(QuicWallTime other) const;
141 // Add returns a new QuicWallTime that represents the time of |this| plus
142 // |delta|.
143 QuicWallTime Add(QuicTime::Delta delta) const;
145 // Subtract returns a new QuicWallTime that represents the time of |this|
146 // minus |delta|.
147 QuicWallTime Subtract(QuicTime::Delta delta) const;
149 private:
150 explicit QuicWallTime(uint64 seconds);
152 uint64 seconds_;
155 // Non-member relational operators for QuicTime::Delta.
156 inline bool operator==(QuicTime::Delta lhs, QuicTime::Delta rhs) {
157 return lhs.ToMicroseconds() == rhs.ToMicroseconds();
159 inline bool operator!=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
160 return !(lhs == rhs);
162 inline bool operator<(QuicTime::Delta lhs, QuicTime::Delta rhs) {
163 return lhs.ToMicroseconds() < rhs.ToMicroseconds();
165 inline bool operator>(QuicTime::Delta lhs, QuicTime::Delta rhs) {
166 return rhs < lhs;
168 inline bool operator<=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
169 return !(rhs < lhs);
171 inline bool operator>=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
172 return !(lhs < rhs);
174 // Non-member relational operators for QuicTime.
175 inline bool operator==(QuicTime lhs, QuicTime rhs) {
176 return lhs.ticks_ == rhs.ticks_;
178 inline bool operator!=(QuicTime lhs, QuicTime rhs) {
179 return !(lhs == rhs);
181 inline bool operator<(QuicTime lhs, QuicTime rhs) {
182 return lhs.ticks_ < rhs.ticks_;
184 inline bool operator>(QuicTime lhs, QuicTime rhs) {
185 return rhs < lhs;
187 inline bool operator<=(QuicTime lhs, QuicTime rhs) {
188 return !(rhs < lhs);
190 inline bool operator>=(QuicTime lhs, QuicTime rhs) {
191 return !(lhs < rhs);
194 } // namespace net
196 #endif // NET_QUIC_QUIC_TIME_H_