[Sync] Test Android passphrase creation UI
[chromium-blink-merge.git] / net / quic / quic_time.h
blob62bf582af49abb9e93d00e3a3d569f6d8e91b867
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 int kNumSecondsPerMinute = 60;
21 static const int kNumSecondsPerHour = kNumSecondsPerMinute * 60;
22 static const uint64 kNumMicrosPerSecond = base::Time::kMicrosecondsPerSecond;
23 static const uint64 kNumMicrosPerMilli =
24 base::Time::kMicrosecondsPerMillisecond;
26 // A QuicTime is a purely relative time. QuicTime values from different clocks
27 // cannot be compared to each other. If you need an absolute time, see
28 // QuicWallTime, below.
29 class NET_EXPORT_PRIVATE QuicTime {
30 public:
31 // A QuicTime::Delta represents the signed difference between two points in
32 // time, stored in microsecond resolution.
33 class NET_EXPORT_PRIVATE Delta {
34 public:
35 explicit Delta(base::TimeDelta delta);
37 // Create a object with an offset of 0.
38 static Delta Zero();
40 // Create a object with infinite offset time.
41 static Delta Infinite();
43 // Converts a number of seconds to a time offset.
44 static Delta FromSeconds(int64 secs);
46 // Converts a number of milliseconds to a time offset.
47 static Delta FromMilliseconds(int64 ms);
49 // Converts a number of microseconds to a time offset.
50 static Delta FromMicroseconds(int64 us);
52 // Converts the time offset to a rounded number of seconds.
53 int64 ToSeconds() const;
55 // Converts the time offset to a rounded number of milliseconds.
56 int64 ToMilliseconds() const;
58 // Converts the time offset to a rounded number of microseconds.
59 int64 ToMicroseconds() const;
61 Delta Add(const Delta& delta) const;
63 Delta Subtract(const Delta& delta) const;
65 Delta Multiply(int i) const;
66 Delta Multiply(double d) const;
68 // Returns the later delta of time1 and time2.
69 static Delta Max(Delta delta1, Delta delta2);
71 bool IsZero() const;
73 bool IsInfinite() const;
75 private:
76 base::TimeDelta delta_;
78 friend class QuicTime;
79 friend class QuicClock;
82 explicit QuicTime(base::TimeTicks ticks);
84 // Creates a new QuicTime with an internal value of 0. IsInitialized()
85 // will return false for these times.
86 static QuicTime Zero();
88 // Creates a new QuicTime with an infinite time.
89 static QuicTime Infinite();
91 // Returns the later time of time1 and time2.
92 static QuicTime Max(QuicTime time1, QuicTime time2);
94 // Produce the internal value to be used when logging. This value
95 // represents the number of microseconds since some epoch. It may
96 // be the UNIX epoch on some platforms. On others, it may
97 // be a CPU ticks based value.
98 int64 ToDebuggingValue() const;
100 bool IsInitialized() const;
102 QuicTime Add(const Delta& delta) const;
104 QuicTime Subtract(const Delta& delta) const;
106 Delta Subtract(const QuicTime& other) const;
108 private:
109 friend bool operator==(QuicTime lhs, QuicTime rhs);
110 friend bool operator<(QuicTime lhs, QuicTime rhs);
112 friend class QuicClock;
113 friend class QuicClockTest;
115 base::TimeTicks ticks_;
118 // A QuicWallTime represents an absolute time that is globally consistent. It
119 // provides, at most, one second granularity and, in practice, clock-skew means
120 // that you shouldn't even depend on that.
121 class NET_EXPORT_PRIVATE QuicWallTime {
122 public:
123 // FromUNIXSeconds constructs a QuicWallTime from a count of the seconds
124 // since the UNIX epoch.
125 static QuicWallTime FromUNIXSeconds(uint64 seconds);
127 // Zero returns a QuicWallTime set to zero. IsZero will return true for this
128 // value.
129 static QuicWallTime Zero();
131 // ToUNIXSeconds converts a QuicWallTime into a count of seconds since the
132 // UNIX epoch.
133 uint64 ToUNIXSeconds() const;
135 bool IsAfter(QuicWallTime other) const;
136 bool IsBefore(QuicWallTime other) const;
138 // IsZero returns true if this object is the result of calling |Zero|.
139 bool IsZero() const;
141 // AbsoluteDifference returns the absolute value of the time difference
142 // between |this| and |other|.
143 QuicTime::Delta AbsoluteDifference(QuicWallTime other) const;
145 // Add returns a new QuicWallTime that represents the time of |this| plus
146 // |delta|.
147 QuicWallTime Add(QuicTime::Delta delta) const;
149 // Subtract returns a new QuicWallTime that represents the time of |this|
150 // minus |delta|.
151 QuicWallTime Subtract(QuicTime::Delta delta) const;
153 private:
154 explicit QuicWallTime(uint64 seconds);
156 uint64 seconds_;
159 // Non-member relational operators for QuicTime::Delta.
160 inline bool operator==(QuicTime::Delta lhs, QuicTime::Delta rhs) {
161 return lhs.ToMicroseconds() == rhs.ToMicroseconds();
163 inline bool operator!=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
164 return !(lhs == rhs);
166 inline bool operator<(QuicTime::Delta lhs, QuicTime::Delta rhs) {
167 return lhs.ToMicroseconds() < rhs.ToMicroseconds();
169 inline bool operator>(QuicTime::Delta lhs, QuicTime::Delta rhs) {
170 return rhs < lhs;
172 inline bool operator<=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
173 return !(rhs < lhs);
175 inline bool operator>=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
176 return !(lhs < rhs);
178 // Non-member relational operators for QuicTime.
179 inline bool operator==(QuicTime lhs, QuicTime rhs) {
180 return lhs.ticks_ == rhs.ticks_;
182 inline bool operator!=(QuicTime lhs, QuicTime rhs) {
183 return !(lhs == rhs);
185 inline bool operator<(QuicTime lhs, QuicTime rhs) {
186 return lhs.ticks_ < rhs.ticks_;
188 inline bool operator>(QuicTime lhs, QuicTime rhs) {
189 return rhs < lhs;
191 inline bool operator<=(QuicTime lhs, QuicTime rhs) {
192 return !(rhs < lhs);
194 inline bool operator>=(QuicTime lhs, QuicTime rhs) {
195 return !(lhs < rhs);
198 } // namespace net
200 #endif // NET_QUIC_QUIC_TIME_H_