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 #include "net/quic/quic_time.h"
7 #include "base/logging.h"
11 // Highest number of microseconds that DateTimeOffset can hold.
12 const int64 kQuicInfiniteTimeUs
= GG_INT64_C(0x7fffffffffffffff) / 10;
14 QuicTime::Delta::Delta(base::TimeDelta delta
)
19 QuicTime::Delta
QuicTime::Delta::Zero() {
20 return QuicTime::Delta::FromMicroseconds(0);
24 QuicTime::Delta
QuicTime::Delta::Infinite() {
25 return QuicTime::Delta::FromMicroseconds(kQuicInfiniteTimeUs
);
29 QuicTime::Delta
QuicTime::Delta::FromSeconds(int64 seconds
) {
30 return QuicTime::Delta(base::TimeDelta::FromSeconds(seconds
));
34 QuicTime::Delta
QuicTime::Delta::FromMilliseconds(int64 ms
) {
35 return QuicTime::Delta(base::TimeDelta::FromMilliseconds(ms
));
39 QuicTime::Delta
QuicTime::Delta::FromMicroseconds(int64 us
) {
40 return QuicTime::Delta(base::TimeDelta::FromMicroseconds(us
));
43 int64
QuicTime::Delta::ToSeconds() const {
44 return delta_
.InSeconds();
47 int64
QuicTime::Delta::ToMilliseconds() const {
48 return delta_
.InMilliseconds();
51 int64
QuicTime::Delta::ToMicroseconds() const {
52 return delta_
.InMicroseconds();
55 QuicTime::Delta
QuicTime::Delta::Add(const Delta
& delta
) const {
56 return QuicTime::Delta::FromMicroseconds(ToMicroseconds() +
57 delta
.ToMicroseconds());
60 QuicTime::Delta
QuicTime::Delta::Subtract(const Delta
& delta
) const {
61 return QuicTime::Delta::FromMicroseconds(ToMicroseconds() -
62 delta
.ToMicroseconds());
65 QuicTime::Delta
QuicTime::Delta::Multiply(int i
) const {
66 return QuicTime::Delta::FromMicroseconds(ToMicroseconds() * i
);
69 QuicTime::Delta
QuicTime::Delta::Multiply(double d
) const {
70 return QuicTime::Delta::FromMicroseconds(
71 static_cast<int64
>(ToMicroseconds() * d
));
75 QuicTime::Delta
QuicTime::Delta::Max(QuicTime::Delta delta1
,
76 QuicTime::Delta delta2
) {
77 return delta1
< delta2
? delta2
: delta1
;
80 bool QuicTime::Delta::IsZero() const {
81 return delta_
.InMicroseconds() == 0;
84 bool QuicTime::Delta::IsInfinite() const {
85 return delta_
.InMicroseconds() == kQuicInfiniteTimeUs
;
89 QuicTime
QuicTime::Zero() {
90 return QuicTime(base::TimeTicks());
94 QuicTime
QuicTime::Infinite() {
95 return QuicTime(base::TimeTicks::FromInternalValue(kQuicInfiniteTimeUs
));
99 QuicTime
QuicTime::Max(QuicTime time1
, QuicTime time2
) {
100 return time1
> time2
? time1
: time2
;
103 QuicTime::QuicTime(base::TimeTicks ticks
)
107 int64
QuicTime::ToDebuggingValue() const {
108 return (ticks_
- base::TimeTicks()).InMicroseconds();
111 bool QuicTime::IsInitialized() const {
112 return ticks_
!= base::TimeTicks();
115 QuicTime
QuicTime::Add(const Delta
& delta
) const {
116 return QuicTime(ticks_
+ delta
.delta_
);
119 QuicTime
QuicTime::Subtract(const Delta
& delta
) const {
120 return QuicTime(ticks_
- delta
.delta_
);
123 QuicTime::Delta
QuicTime::Subtract(const QuicTime
& other
) const {
124 return QuicTime::Delta(ticks_
- other
.ticks_
);
128 QuicWallTime
QuicWallTime::FromUNIXSeconds(uint64 seconds
) {
129 return QuicWallTime(seconds
);
133 QuicWallTime
QuicWallTime::Zero() {
134 return QuicWallTime(0);
137 uint64
QuicWallTime::ToUNIXSeconds() const {
141 bool QuicWallTime::IsAfter(QuicWallTime other
) const {
142 return seconds_
> other
.seconds_
;
145 bool QuicWallTime::IsBefore(QuicWallTime other
) const {
146 return seconds_
< other
.seconds_
;
149 bool QuicWallTime::IsZero() const {
150 return seconds_
== 0;
153 QuicTime::Delta
QuicWallTime::AbsoluteDifference(QuicWallTime other
) const {
156 if (seconds_
> other
.seconds_
) {
157 d
= seconds_
- other
.seconds_
;
159 d
= other
.seconds_
- seconds_
;
162 if (d
> static_cast<uint64
>(kint64max
)) {
165 return QuicTime::Delta::FromSeconds(d
);
168 QuicWallTime
QuicWallTime::Add(QuicTime::Delta delta
) const {
169 uint64 seconds
= seconds_
+ delta
.ToSeconds();
170 if (seconds
< seconds_
) {
171 seconds
= kuint64max
;
173 return QuicWallTime(seconds
);
176 // TODO(ianswett) Test this.
177 QuicWallTime
QuicWallTime::Subtract(QuicTime::Delta delta
) const {
178 uint64 seconds
= seconds_
- delta
.ToSeconds();
179 if (seconds
> seconds_
) {
182 return QuicWallTime(seconds
);
185 QuicWallTime::QuicWallTime(uint64 seconds
)
186 : seconds_(seconds
) {