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(ToMicroseconds() * d
);
74 QuicTime::Delta
QuicTime::Delta::Max(QuicTime::Delta delta1
,
75 QuicTime::Delta delta2
) {
76 return delta1
< delta2
? delta2
: delta1
;
79 bool QuicTime::Delta::IsZero() const {
80 return delta_
.InMicroseconds() == 0;
83 bool QuicTime::Delta::IsInfinite() const {
84 return delta_
.InMicroseconds() == kQuicInfiniteTimeUs
;
88 QuicTime
QuicTime::Zero() {
89 return QuicTime(base::TimeTicks());
93 QuicTime
QuicTime::Infinite() {
94 return QuicTime(base::TimeTicks::FromInternalValue(kQuicInfiniteTimeUs
));
98 QuicTime
QuicTime::Max(QuicTime time1
, QuicTime time2
) {
99 return time1
> time2
? time1
: time2
;
102 QuicTime::QuicTime(base::TimeTicks ticks
)
106 int64
QuicTime::ToDebuggingValue() const {
107 return (ticks_
- base::TimeTicks()).InMicroseconds();
110 bool QuicTime::IsInitialized() const {
111 return ticks_
!= base::TimeTicks();
114 QuicTime
QuicTime::Add(const Delta
& delta
) const {
115 return QuicTime(ticks_
+ delta
.delta_
);
118 QuicTime
QuicTime::Subtract(const Delta
& delta
) const {
119 return QuicTime(ticks_
- delta
.delta_
);
122 QuicTime::Delta
QuicTime::Subtract(const QuicTime
& other
) const {
123 return QuicTime::Delta(ticks_
- other
.ticks_
);
127 QuicWallTime
QuicWallTime::FromUNIXSeconds(uint64 seconds
) {
128 return QuicWallTime(seconds
);
132 QuicWallTime
QuicWallTime::Zero() {
133 return QuicWallTime(0);
136 uint64
QuicWallTime::ToUNIXSeconds() const {
140 bool QuicWallTime::IsAfter(QuicWallTime other
) const {
141 return seconds_
> other
.seconds_
;
144 bool QuicWallTime::IsBefore(QuicWallTime other
) const {
145 return seconds_
< other
.seconds_
;
148 bool QuicWallTime::IsZero() const {
149 return seconds_
== 0;
152 QuicTime::Delta
QuicWallTime::AbsoluteDifference(QuicWallTime other
) const {
155 if (seconds_
> other
.seconds_
) {
156 d
= seconds_
- other
.seconds_
;
158 d
= other
.seconds_
- seconds_
;
161 if (d
> static_cast<uint64
>(kint64max
)) {
164 return QuicTime::Delta::FromSeconds(d
);
167 QuicWallTime
QuicWallTime::Add(QuicTime::Delta delta
) const {
168 uint64 seconds
= seconds_
+ delta
.ToSeconds();
169 if (seconds
< seconds_
) {
170 seconds
= kuint64max
;
172 return QuicWallTime(seconds
);
175 QuicWallTime
QuicWallTime::Subtract(QuicTime::Delta delta
) const {
176 uint64 seconds
= seconds_
- delta
.ToSeconds();
177 if (seconds
> seconds_
) {
180 return QuicWallTime(seconds
);
183 QuicWallTime::QuicWallTime(uint64 seconds
)
184 : seconds_(seconds
) {