• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include "net/quic/quic_time.h"
6 
7 #include "base/logging.h"
8 
9 namespace net {
10 
11 // Highest number of microseconds that DateTimeOffset can hold.
12 const int64 kQuicInfiniteTimeUs = GG_INT64_C(0x7fffffffffffffff) / 10;
13 
Delta(base::TimeDelta delta)14 QuicTime::Delta::Delta(base::TimeDelta delta)
15     : delta_(delta) {
16 }
17 
18 // static
Zero()19 QuicTime::Delta QuicTime::Delta::Zero() {
20   return QuicTime::Delta::FromMicroseconds(0);
21 }
22 
23 // static
Infinite()24 QuicTime::Delta QuicTime::Delta::Infinite() {
25   return QuicTime::Delta::FromMicroseconds(kQuicInfiniteTimeUs);
26 }
27 
28 // static
FromSeconds(int64 seconds)29 QuicTime::Delta QuicTime::Delta::FromSeconds(int64 seconds) {
30   return QuicTime::Delta(base::TimeDelta::FromSeconds(seconds));
31 }
32 
33 // static
FromMilliseconds(int64 ms)34 QuicTime::Delta QuicTime::Delta::FromMilliseconds(int64 ms) {
35   return QuicTime::Delta(base::TimeDelta::FromMilliseconds(ms));
36 }
37 
38 // static
FromMicroseconds(int64 us)39 QuicTime::Delta QuicTime::Delta::FromMicroseconds(int64 us) {
40   return QuicTime::Delta(base::TimeDelta::FromMicroseconds(us));
41 }
42 
ToSeconds() const43 int64 QuicTime::Delta::ToSeconds() const {
44   return delta_.InSeconds();
45 }
46 
ToMilliseconds() const47 int64 QuicTime::Delta::ToMilliseconds() const {
48   return delta_.InMilliseconds();
49 }
50 
ToMicroseconds() const51 int64 QuicTime::Delta::ToMicroseconds() const {
52   return delta_.InMicroseconds();
53 }
54 
Add(const Delta & delta) const55 QuicTime::Delta QuicTime::Delta::Add(const Delta& delta) const {
56   return QuicTime::Delta::FromMicroseconds(ToMicroseconds() +
57                                            delta.ToMicroseconds());
58 }
59 
Subtract(const Delta & delta) const60 QuicTime::Delta QuicTime::Delta::Subtract(const Delta& delta) const {
61   return QuicTime::Delta::FromMicroseconds(ToMicroseconds() -
62                                            delta.ToMicroseconds());
63 }
64 
Multiply(int i) const65 QuicTime::Delta QuicTime::Delta::Multiply(int i) const {
66   return QuicTime::Delta::FromMicroseconds(ToMicroseconds() * i);
67 }
68 
Multiply(double d) const69 QuicTime::Delta QuicTime::Delta::Multiply(double d) const {
70   return QuicTime::Delta::FromMicroseconds(ToMicroseconds() * d);
71 }
72 
73 // static
Max(QuicTime::Delta delta1,QuicTime::Delta delta2)74 QuicTime::Delta QuicTime::Delta::Max(QuicTime::Delta delta1,
75                                      QuicTime::Delta delta2) {
76   return delta1 < delta2 ? delta2 : delta1;
77 }
78 
IsZero() const79 bool QuicTime::Delta::IsZero() const {
80   return delta_.InMicroseconds() == 0;
81 }
82 
IsInfinite() const83 bool QuicTime::Delta::IsInfinite() const {
84   return delta_.InMicroseconds() == kQuicInfiniteTimeUs;
85 }
86 
87 // static
Zero()88 QuicTime QuicTime::Zero() {
89   return QuicTime(base::TimeTicks());
90 }
91 
92 // static
Infinite()93 QuicTime QuicTime::Infinite() {
94   return QuicTime(base::TimeTicks::FromInternalValue(kQuicInfiniteTimeUs));
95 }
96 
97 // static
Max(QuicTime time1,QuicTime time2)98 QuicTime QuicTime::Max(QuicTime time1, QuicTime time2) {
99   return time1 > time2 ? time1 : time2;
100 }
101 
QuicTime(base::TimeTicks ticks)102 QuicTime::QuicTime(base::TimeTicks ticks)
103     : ticks_(ticks) {
104 }
105 
ToDebuggingValue() const106 int64 QuicTime::ToDebuggingValue() const {
107   return (ticks_ - base::TimeTicks()).InMicroseconds();
108 }
109 
IsInitialized() const110 bool QuicTime::IsInitialized() const {
111   return ticks_ != base::TimeTicks();
112 }
113 
Add(const Delta & delta) const114 QuicTime QuicTime::Add(const Delta& delta) const {
115   return QuicTime(ticks_ + delta.delta_);
116 }
117 
Subtract(const Delta & delta) const118 QuicTime QuicTime::Subtract(const Delta& delta) const {
119   return QuicTime(ticks_ - delta.delta_);
120 }
121 
Subtract(const QuicTime & other) const122 QuicTime::Delta QuicTime::Subtract(const QuicTime& other) const {
123   return QuicTime::Delta(ticks_ - other.ticks_);
124 }
125 
126 // static
FromUNIXSeconds(uint64 seconds)127 QuicWallTime QuicWallTime::FromUNIXSeconds(uint64 seconds) {
128   return QuicWallTime(seconds);
129 }
130 
131 // static
Zero()132 QuicWallTime QuicWallTime::Zero() {
133   return QuicWallTime(0);
134 }
135 
ToUNIXSeconds() const136 uint64 QuicWallTime::ToUNIXSeconds() const {
137   return seconds_;
138 }
139 
IsAfter(QuicWallTime other) const140 bool QuicWallTime::IsAfter(QuicWallTime other) const {
141   return seconds_ > other.seconds_;
142 }
143 
IsBefore(QuicWallTime other) const144 bool QuicWallTime::IsBefore(QuicWallTime other) const {
145   return seconds_ < other.seconds_;
146 }
147 
IsZero() const148 bool QuicWallTime::IsZero() const {
149   return seconds_ == 0;
150 }
151 
AbsoluteDifference(QuicWallTime other) const152 QuicTime::Delta QuicWallTime::AbsoluteDifference(QuicWallTime other) const {
153   uint64 d;
154 
155   if (seconds_ > other.seconds_) {
156     d = seconds_ - other.seconds_;
157   } else {
158     d = other.seconds_ - seconds_;
159   }
160 
161   if (d > static_cast<uint64>(kint64max)) {
162     d = kint64max;
163   }
164   return QuicTime::Delta::FromSeconds(d);
165 }
166 
Add(QuicTime::Delta delta) const167 QuicWallTime QuicWallTime::Add(QuicTime::Delta delta) const {
168   uint64 seconds = seconds_ + delta.ToSeconds();
169   if (seconds < seconds_) {
170     seconds = kuint64max;
171   }
172   return QuicWallTime(seconds);
173 }
174 
Subtract(QuicTime::Delta delta) const175 QuicWallTime QuicWallTime::Subtract(QuicTime::Delta delta) const {
176   uint64 seconds = seconds_ - delta.ToSeconds();
177   if (seconds > seconds_) {
178     seconds = 0;
179   }
180   return QuicWallTime(seconds);
181 }
182 
QuicWallTime(uint64 seconds)183 QuicWallTime::QuicWallTime(uint64 seconds)
184     : seconds_(seconds) {
185 }
186 
187 }  // namespace net
188