Home | History | Annotate | Download | only in quic
      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 
     14 QuicTime::Delta::Delta(base::TimeDelta delta)
     15     : delta_(delta) {
     16 }
     17 
     18 // static
     19 QuicTime::Delta QuicTime::Delta::Zero() {
     20   return QuicTime::Delta::FromMicroseconds(0);
     21 }
     22 
     23 // static
     24 QuicTime::Delta QuicTime::Delta::Infinite() {
     25   return QuicTime::Delta::FromMicroseconds(kQuicInfiniteTimeUs);
     26 }
     27 
     28 // static
     29 QuicTime::Delta QuicTime::Delta::FromSeconds(int64 seconds) {
     30   return QuicTime::Delta(base::TimeDelta::FromSeconds(seconds));
     31 }
     32 
     33 // static
     34 QuicTime::Delta QuicTime::Delta::FromMilliseconds(int64 ms) {
     35   return QuicTime::Delta(base::TimeDelta::FromMilliseconds(ms));
     36 }
     37 
     38 // static
     39 QuicTime::Delta QuicTime::Delta::FromMicroseconds(int64 us) {
     40   return QuicTime::Delta(base::TimeDelta::FromMicroseconds(us));
     41 }
     42 
     43 int64 QuicTime::Delta::ToSeconds() const {
     44   return delta_.InSeconds();
     45 }
     46 
     47 int64 QuicTime::Delta::ToMilliseconds() const {
     48   return delta_.InMilliseconds();
     49 }
     50 
     51 int64 QuicTime::Delta::ToMicroseconds() const {
     52   return delta_.InMicroseconds();
     53 }
     54 
     55 QuicTime::Delta QuicTime::Delta::Add(const Delta& delta) const {
     56   return QuicTime::Delta::FromMicroseconds(ToMicroseconds() +
     57                                            delta.ToMicroseconds());
     58 }
     59 
     60 QuicTime::Delta QuicTime::Delta::Subtract(const Delta& delta) const {
     61   return QuicTime::Delta::FromMicroseconds(ToMicroseconds() -
     62                                            delta.ToMicroseconds());
     63 }
     64 
     65 bool QuicTime::Delta::IsZero() const {
     66   return delta_.InMicroseconds() == 0;
     67 }
     68 
     69 bool QuicTime::Delta::IsInfinite() const {
     70   return delta_.InMicroseconds() == kQuicInfiniteTimeUs;
     71 }
     72 
     73 // static
     74 QuicTime QuicTime::Zero() {
     75   return QuicTime(base::TimeTicks());
     76 }
     77 
     78 QuicTime::QuicTime(base::TimeTicks ticks)
     79     : ticks_(ticks) {
     80 }
     81 
     82 int64 QuicTime::ToDebuggingValue() const {
     83   return (ticks_ - base::TimeTicks()).InMicroseconds();
     84 }
     85 
     86 bool QuicTime::IsInitialized() const {
     87   return ticks_ != base::TimeTicks();
     88 }
     89 
     90 QuicTime QuicTime::Add(const Delta& delta) const {
     91   return QuicTime(ticks_ + delta.delta_);
     92 }
     93 
     94 QuicTime QuicTime::Subtract(const Delta& delta) const {
     95   return QuicTime(ticks_ - delta.delta_);
     96 }
     97 
     98 QuicTime::Delta QuicTime::Subtract(const QuicTime& other) const {
     99   return QuicTime::Delta(ticks_ - other.ticks_);
    100 }
    101 
    102 // static
    103 QuicWallTime QuicWallTime::FromUNIXSeconds(uint64 seconds) {
    104   return QuicWallTime(seconds);
    105 }
    106 
    107 // static
    108 QuicWallTime QuicWallTime::Zero() {
    109   return QuicWallTime(0);
    110 }
    111 
    112 uint64 QuicWallTime::ToUNIXSeconds() const {
    113   return seconds_;
    114 }
    115 
    116 bool QuicWallTime::IsAfter(QuicWallTime other) const {
    117   return seconds_ > other.seconds_;
    118 }
    119 
    120 bool QuicWallTime::IsBefore(QuicWallTime other) const {
    121   return seconds_ < other.seconds_;
    122 }
    123 
    124 bool QuicWallTime::IsZero() const {
    125   return seconds_ == 0;
    126 }
    127 
    128 QuicTime::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 
    143 QuicWallTime 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 
    151 QuicWallTime 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 
    159 QuicWallTime::QuicWallTime(uint64 seconds)
    160     : seconds_(seconds) {
    161 }
    162 
    163 }  // namespace net
    164