QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 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/third_party/quiche/src/quic/core/quic_time.h" |
| 6 | |
| 7 | #include <cinttypes> |
| 8 | #include <cstdlib> |
| 9 | #include <limits> |
vasilvv | 872e7a3 | 2019-03-12 16:42:44 -0700 | [diff] [blame^] | 10 | #include <string> |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 11 | |
| 12 | #include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h" |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 13 | |
| 14 | namespace quic { |
| 15 | |
vasilvv | c48c871 | 2019-03-11 13:38:16 -0700 | [diff] [blame] | 16 | std::string QuicTime::Delta::ToDebugValue() const { |
QUICHE team | a6ef0a6 | 2019-03-07 20:34:33 -0500 | [diff] [blame] | 17 | const int64_t one_ms = 1000; |
| 18 | const int64_t one_s = 1000 * one_ms; |
| 19 | |
| 20 | int64_t absolute_value = std::abs(time_offset_); |
| 21 | |
| 22 | // For debugging purposes, always display the value with the highest precision |
| 23 | // available. |
| 24 | if (absolute_value > one_s && absolute_value % one_s == 0) { |
| 25 | return QuicStringPrintf("%" PRId64 "s", time_offset_ / one_s); |
| 26 | } |
| 27 | if (absolute_value > one_ms && absolute_value % one_ms == 0) { |
| 28 | return QuicStringPrintf("%" PRId64 "ms", time_offset_ / one_ms); |
| 29 | } |
| 30 | return QuicStringPrintf("%" PRId64 "us", time_offset_); |
| 31 | } |
| 32 | |
| 33 | uint64_t QuicWallTime::ToUNIXSeconds() const { |
| 34 | return microseconds_ / 1000000; |
| 35 | } |
| 36 | |
| 37 | uint64_t QuicWallTime::ToUNIXMicroseconds() const { |
| 38 | return microseconds_; |
| 39 | } |
| 40 | |
| 41 | bool QuicWallTime::IsAfter(QuicWallTime other) const { |
| 42 | return microseconds_ > other.microseconds_; |
| 43 | } |
| 44 | |
| 45 | bool QuicWallTime::IsBefore(QuicWallTime other) const { |
| 46 | return microseconds_ < other.microseconds_; |
| 47 | } |
| 48 | |
| 49 | bool QuicWallTime::IsZero() const { |
| 50 | return microseconds_ == 0; |
| 51 | } |
| 52 | |
| 53 | QuicTime::Delta QuicWallTime::AbsoluteDifference(QuicWallTime other) const { |
| 54 | uint64_t d; |
| 55 | |
| 56 | if (microseconds_ > other.microseconds_) { |
| 57 | d = microseconds_ - other.microseconds_; |
| 58 | } else { |
| 59 | d = other.microseconds_ - microseconds_; |
| 60 | } |
| 61 | |
| 62 | if (d > static_cast<uint64_t>(std::numeric_limits<int64_t>::max())) { |
| 63 | d = std::numeric_limits<int64_t>::max(); |
| 64 | } |
| 65 | return QuicTime::Delta::FromMicroseconds(d); |
| 66 | } |
| 67 | |
| 68 | QuicWallTime QuicWallTime::Add(QuicTime::Delta delta) const { |
| 69 | uint64_t microseconds = microseconds_ + delta.ToMicroseconds(); |
| 70 | if (microseconds < microseconds_) { |
| 71 | microseconds = std::numeric_limits<uint64_t>::max(); |
| 72 | } |
| 73 | return QuicWallTime(microseconds); |
| 74 | } |
| 75 | |
| 76 | // TODO(ianswett) Test this. |
| 77 | QuicWallTime QuicWallTime::Subtract(QuicTime::Delta delta) const { |
| 78 | uint64_t microseconds = microseconds_ - delta.ToMicroseconds(); |
| 79 | if (microseconds > microseconds_) { |
| 80 | microseconds = 0; |
| 81 | } |
| 82 | return QuicWallTime(microseconds); |
| 83 | } |
| 84 | |
| 85 | } // namespace quic |