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::Max(QuicTime time1
, QuicTime time2
) {
94 return time1
> time2
? time1
: time2
;
97 QuicTime::QuicTime(base::TimeTicks ticks
)
101 int64
QuicTime::ToDebuggingValue() const {
102 return (ticks_
- base::TimeTicks()).InMicroseconds();
105 bool QuicTime::IsInitialized() const {
106 return ticks_
!= base::TimeTicks();
109 QuicTime
QuicTime::Add(const Delta
& delta
) const {
110 return QuicTime(ticks_
+ delta
.delta_
);
113 QuicTime
QuicTime::Subtract(const Delta
& delta
) const {
114 return QuicTime(ticks_
- delta
.delta_
);
117 QuicTime::Delta
QuicTime::Subtract(const QuicTime
& other
) const {
118 return QuicTime::Delta(ticks_
- other
.ticks_
);
122 QuicWallTime
QuicWallTime::FromUNIXSeconds(uint64 seconds
) {
123 return QuicWallTime(seconds
);
127 QuicWallTime
QuicWallTime::Zero() {
128 return QuicWallTime(0);
131 uint64
QuicWallTime::ToUNIXSeconds() const {
135 bool QuicWallTime::IsAfter(QuicWallTime other
) const {
136 return seconds_
> other
.seconds_
;
139 bool QuicWallTime::IsBefore(QuicWallTime other
) const {
140 return seconds_
< other
.seconds_
;
143 bool QuicWallTime::IsZero() const {
144 return seconds_
== 0;
147 QuicTime::Delta
QuicWallTime::AbsoluteDifference(QuicWallTime other
) const {
150 if (seconds_
> other
.seconds_
) {
151 d
= seconds_
- other
.seconds_
;
153 d
= other
.seconds_
- seconds_
;
156 if (d
> static_cast<uint64
>(kint64max
)) {
159 return QuicTime::Delta::FromSeconds(d
);
162 QuicWallTime
QuicWallTime::Add(QuicTime::Delta delta
) const {
163 uint64 seconds
= seconds_
+ delta
.ToSeconds();
164 if (seconds
< seconds_
) {
165 seconds
= kuint64max
;
167 return QuicWallTime(seconds
);
170 QuicWallTime
QuicWallTime::Subtract(QuicTime::Delta delta
) const {
171 uint64 seconds
= seconds_
- delta
.ToSeconds();
172 if (seconds
> seconds_
) {
175 return QuicWallTime(seconds
);
178 QuicWallTime::QuicWallTime(uint64 seconds
)
179 : seconds_(seconds
) {