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)14QuicTime::Delta::Delta(base::TimeDelta delta) 15 : delta_(delta) { 16 } 17 18 // static Zero()19QuicTime::Delta QuicTime::Delta::Zero() { 20 return QuicTime::Delta::FromMicroseconds(0); 21 } 22 23 // static Infinite()24QuicTime::Delta QuicTime::Delta::Infinite() { 25 return QuicTime::Delta::FromMicroseconds(kQuicInfiniteTimeUs); 26 } 27 28 // static FromSeconds(int64 seconds)29QuicTime::Delta QuicTime::Delta::FromSeconds(int64 seconds) { 30 return QuicTime::Delta(base::TimeDelta::FromSeconds(seconds)); 31 } 32 33 // static FromMilliseconds(int64 ms)34QuicTime::Delta QuicTime::Delta::FromMilliseconds(int64 ms) { 35 return QuicTime::Delta(base::TimeDelta::FromMilliseconds(ms)); 36 } 37 38 // static FromMicroseconds(int64 us)39QuicTime::Delta QuicTime::Delta::FromMicroseconds(int64 us) { 40 return QuicTime::Delta(base::TimeDelta::FromMicroseconds(us)); 41 } 42 ToSeconds() const43int64 QuicTime::Delta::ToSeconds() const { 44 return delta_.InSeconds(); 45 } 46 ToMilliseconds() const47int64 QuicTime::Delta::ToMilliseconds() const { 48 return delta_.InMilliseconds(); 49 } 50 ToMicroseconds() const51int64 QuicTime::Delta::ToMicroseconds() const { 52 return delta_.InMicroseconds(); 53 } 54 Add(const Delta & delta) const55QuicTime::Delta QuicTime::Delta::Add(const Delta& delta) const { 56 return QuicTime::Delta::FromMicroseconds(ToMicroseconds() + 57 delta.ToMicroseconds()); 58 } 59 Subtract(const Delta & delta) const60QuicTime::Delta QuicTime::Delta::Subtract(const Delta& delta) const { 61 return QuicTime::Delta::FromMicroseconds(ToMicroseconds() - 62 delta.ToMicroseconds()); 63 } 64 IsZero() const65bool QuicTime::Delta::IsZero() const { 66 return delta_.InMicroseconds() == 0; 67 } 68 IsInfinite() const69bool QuicTime::Delta::IsInfinite() const { 70 return delta_.InMicroseconds() == kQuicInfiniteTimeUs; 71 } 72 73 // static Zero()74QuicTime QuicTime::Zero() { 75 return QuicTime(base::TimeTicks()); 76 } 77 QuicTime(base::TimeTicks ticks)78QuicTime::QuicTime(base::TimeTicks ticks) 79 : ticks_(ticks) { 80 } 81 ToDebuggingValue() const82int64 QuicTime::ToDebuggingValue() const { 83 return (ticks_ - base::TimeTicks()).InMicroseconds(); 84 } 85 IsInitialized() const86bool QuicTime::IsInitialized() const { 87 return ticks_ != base::TimeTicks(); 88 } 89 Add(const Delta & delta) const90QuicTime QuicTime::Add(const Delta& delta) const { 91 return QuicTime(ticks_ + delta.delta_); 92 } 93 Subtract(const Delta & delta) const94QuicTime QuicTime::Subtract(const Delta& delta) const { 95 return QuicTime(ticks_ - delta.delta_); 96 } 97 Subtract(const QuicTime & other) const98QuicTime::Delta QuicTime::Subtract(const QuicTime& other) const { 99 return QuicTime::Delta(ticks_ - other.ticks_); 100 } 101 102 // static FromUNIXSeconds(uint64 seconds)103QuicWallTime QuicWallTime::FromUNIXSeconds(uint64 seconds) { 104 return QuicWallTime(seconds); 105 } 106 107 // static Zero()108QuicWallTime QuicWallTime::Zero() { 109 return QuicWallTime(0); 110 } 111 ToUNIXSeconds() const112uint64 QuicWallTime::ToUNIXSeconds() const { 113 return seconds_; 114 } 115 IsAfter(QuicWallTime other) const116bool QuicWallTime::IsAfter(QuicWallTime other) const { 117 return seconds_ > other.seconds_; 118 } 119 IsBefore(QuicWallTime other) const120bool QuicWallTime::IsBefore(QuicWallTime other) const { 121 return seconds_ < other.seconds_; 122 } 123 IsZero() const124bool QuicWallTime::IsZero() const { 125 return seconds_ == 0; 126 } 127 AbsoluteDifference(QuicWallTime other) const128QuicTime::Delta QuicWallTime::AbsoluteDifference(QuicWallTime other) const { 129 uint64 d; 130 131 if (seconds_ > other.seconds_) { 132 d = seconds_ - other.seconds_; 133 } else { 134 d = other.seconds_ - seconds_; 135 } 136 137 if (d > static_cast<uint64>(kint64max)) { 138 d = kint64max; 139 } 140 return QuicTime::Delta::FromSeconds(d); 141 } 142 Add(QuicTime::Delta delta) const143QuicWallTime QuicWallTime::Add(QuicTime::Delta delta) const { 144 uint64 seconds = seconds_ + delta.ToSeconds(); 145 if (seconds < seconds_) { 146 seconds = kuint64max; 147 } 148 return QuicWallTime(seconds); 149 } 150 Subtract(QuicTime::Delta delta) const151QuicWallTime QuicWallTime::Subtract(QuicTime::Delta delta) const { 152 uint64 seconds = seconds_ - delta.ToSeconds(); 153 if (seconds > seconds_) { 154 seconds = 0; 155 } 156 return QuicWallTime(seconds); 157 } 158 QuicWallTime(uint64 seconds)159QuicWallTime::QuicWallTime(uint64 seconds) 160 : seconds_(seconds) { 161 } 162 163 } // namespace net 164