Updating trunk VERSION from 2139.0 to 2140.0
[chromium-blink-merge.git] / net / quic / quic_time.cc
blobe20a338ce390d9e2418878752f99ac1f9dda308b
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"
9 namespace net {
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)
15 : delta_(delta) {
18 // static
19 QuicTime::Delta QuicTime::Delta::Zero() {
20 return QuicTime::Delta::FromMicroseconds(0);
23 // static
24 QuicTime::Delta QuicTime::Delta::Infinite() {
25 return QuicTime::Delta::FromMicroseconds(kQuicInfiniteTimeUs);
28 // static
29 QuicTime::Delta QuicTime::Delta::FromSeconds(int64 seconds) {
30 return QuicTime::Delta(base::TimeDelta::FromSeconds(seconds));
33 // static
34 QuicTime::Delta QuicTime::Delta::FromMilliseconds(int64 ms) {
35 return QuicTime::Delta(base::TimeDelta::FromMilliseconds(ms));
38 // static
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);
73 // static
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;
87 // static
88 QuicTime QuicTime::Zero() {
89 return QuicTime(base::TimeTicks());
92 // static
93 QuicTime QuicTime::Infinite() {
94 return QuicTime(base::TimeTicks::FromInternalValue(kQuicInfiniteTimeUs));
97 // static
98 QuicTime QuicTime::Max(QuicTime time1, QuicTime time2) {
99 return time1 > time2 ? time1 : time2;
102 QuicTime::QuicTime(base::TimeTicks ticks)
103 : ticks_(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_);
126 // static
127 QuicWallTime QuicWallTime::FromUNIXSeconds(uint64 seconds) {
128 return QuicWallTime(seconds);
131 // static
132 QuicWallTime QuicWallTime::Zero() {
133 return QuicWallTime(0);
136 uint64 QuicWallTime::ToUNIXSeconds() const {
137 return seconds_;
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 {
153 uint64 d;
155 if (seconds_ > other.seconds_) {
156 d = seconds_ - other.seconds_;
157 } else {
158 d = other.seconds_ - seconds_;
161 if (d > static_cast<uint64>(kint64max)) {
162 d = 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_) {
178 seconds = 0;
180 return QuicWallTime(seconds);
183 QuicWallTime::QuicWallTime(uint64 seconds)
184 : seconds_(seconds) {
187 } // namespace net