Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / net / quic / quic_time.h
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.
10
11 #ifndef NET_QUIC_QUIC_TIME_H_
12 #define NET_QUIC_QUIC_TIME_H_
13
14 #include "base/basictypes.h"
15 #include "base/time/time.h"
16 #include "net/base/net_export.h"
17
18 namespace net {
19
20 static const uint64 kNumMicrosPerSecond = base::Time::kMicrosecondsPerSecond;
21 static const uint64 kNumMicrosPerMilli =
22     base::Time::kMicrosecondsPerMillisecond;
23
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 {
28  public:
29   // A QuicTime::Delta represents the signed difference between two points in
30   // time, stored in microsecond resolution.
31   class NET_EXPORT_PRIVATE Delta {
32    public:
33     explicit Delta(base::TimeDelta delta);
34
35     // Create a object with an offset of 0.
36     static Delta Zero();
37
38     // Create a object with infinite offset time.
39     static Delta Infinite();
40
41     // Converts a number of seconds to a time offset.
42     static Delta FromSeconds(int64 secs);
43
44     // Converts a number of milliseconds to a time offset.
45     static Delta FromMilliseconds(int64 ms);
46
47     // Converts a number of microseconds to a time offset.
48     static Delta FromMicroseconds(int64 us);
49
50     // Converts the time offset to a rounded number of seconds.
51     int64 ToSeconds() const;
52
53     // Converts the time offset to a rounded number of milliseconds.
54     int64 ToMilliseconds() const;
55
56     // Converts the time offset to a rounded number of microseconds.
57     int64 ToMicroseconds() const;
58
59     Delta Add(const Delta& delta) const;
60
61     Delta Subtract(const Delta& delta) const;
62
63     Delta Multiply(int i) const;
64     Delta Multiply(double d) const;
65
66     // Returns the later delta of time1 and time2.
67     static Delta Max(Delta delta1, Delta delta2);
68
69     bool IsZero() const;
70
71     bool IsInfinite() const;
72
73    private:
74     base::TimeDelta delta_;
75
76     friend class QuicTime;
77     friend class QuicClock;
78   };
79
80   explicit QuicTime(base::TimeTicks ticks);
81
82   // Creates a new QuicTime with an internal value of 0.  IsInitialized()
83   // will return false for these times.
84   static QuicTime Zero();
85
86   // Creates a new QuicTime with an infinite time.
87   static QuicTime Infinite();
88
89   // Returns the later time of time1 and time2.
90   static QuicTime Max(QuicTime time1, QuicTime time2);
91
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;
97
98   bool IsInitialized() const;
99
100   QuicTime Add(const Delta& delta) const;
101
102   QuicTime Subtract(const Delta& delta) const;
103
104   Delta Subtract(const QuicTime& other) const;
105
106  private:
107   friend bool operator==(QuicTime lhs, QuicTime rhs);
108   friend bool operator<(QuicTime lhs, QuicTime rhs);
109
110   friend class QuicClock;
111   friend class QuicClockTest;
112
113   base::TimeTicks ticks_;
114 };
115
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 {
120  public:
121   // FromUNIXSeconds constructs a QuicWallTime from a count of the seconds
122   // since the UNIX epoch.
123   static QuicWallTime FromUNIXSeconds(uint64 seconds);
124
125   // Zero returns a QuicWallTime set to zero. IsZero will return true for this
126   // value.
127   static QuicWallTime Zero();
128
129   // ToUNIXSeconds converts a QuicWallTime into a count of seconds since the
130   // UNIX epoch.
131   uint64 ToUNIXSeconds() const;
132
133   bool IsAfter(QuicWallTime other) const;
134   bool IsBefore(QuicWallTime other) const;
135
136   // IsZero returns true if this object is the result of calling |Zero|.
137   bool IsZero() const;
138
139   // AbsoluteDifference returns the absolute value of the time difference
140   // between |this| and |other|.
141   QuicTime::Delta AbsoluteDifference(QuicWallTime other) const;
142
143   // Add returns a new QuicWallTime that represents the time of |this| plus
144   // |delta|.
145   QuicWallTime Add(QuicTime::Delta delta) const;
146
147   // Subtract returns a new QuicWallTime that represents the time of |this|
148   // minus |delta|.
149   QuicWallTime Subtract(QuicTime::Delta delta) const;
150
151  private:
152   explicit QuicWallTime(uint64 seconds);
153
154   uint64 seconds_;
155 };
156
157 // Non-member relational operators for QuicTime::Delta.
158 inline bool operator==(QuicTime::Delta lhs, QuicTime::Delta rhs) {
159   return lhs.ToMicroseconds() == rhs.ToMicroseconds();
160 }
161 inline bool operator!=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
162   return !(lhs == rhs);
163 }
164 inline bool operator<(QuicTime::Delta lhs, QuicTime::Delta rhs) {
165   return lhs.ToMicroseconds() < rhs.ToMicroseconds();
166 }
167 inline bool operator>(QuicTime::Delta lhs, QuicTime::Delta rhs) {
168   return rhs < lhs;
169 }
170 inline bool operator<=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
171   return !(rhs < lhs);
172 }
173 inline bool operator>=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
174   return !(lhs < rhs);
175 }
176 // Non-member relational operators for QuicTime.
177 inline bool operator==(QuicTime lhs, QuicTime rhs) {
178   return lhs.ticks_ == rhs.ticks_;
179 }
180 inline bool operator!=(QuicTime lhs, QuicTime rhs) {
181   return !(lhs == rhs);
182 }
183 inline bool operator<(QuicTime lhs, QuicTime rhs) {
184   return lhs.ticks_ < rhs.ticks_;
185 }
186 inline bool operator>(QuicTime lhs, QuicTime rhs) {
187   return rhs < lhs;
188 }
189 inline bool operator<=(QuicTime lhs, QuicTime rhs) {
190   return !(rhs < lhs);
191 }
192 inline bool operator>=(QuicTime lhs, QuicTime rhs) {
193   return !(lhs < rhs);
194 }
195
196 }  // namespace net
197
198 #endif  // NET_QUIC_QUIC_TIME_H_