blob: ffc59dadf9bdc7a349d9ff1ee895cc73017c0ccc [file] [log] [blame]
QUICHE teama6ef0a62019-03-07 20:34:33 -05001// 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>
vasilvv872e7a32019-03-12 16:42:44 -070010#include <string>
QUICHE teama6ef0a62019-03-07 20:34:33 -050011
12#include "net/third_party/quiche/src/quic/platform/api/quic_str_cat.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050013
14namespace quic {
15
zhongyi26818ca2019-05-13 17:50:37 -070016std::string QuicTime::Delta::ToDebuggingValue() const {
QUICHE teama6ef0a62019-03-07 20:34:33 -050017 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
33uint64_t QuicWallTime::ToUNIXSeconds() const {
34 return microseconds_ / 1000000;
35}
36
37uint64_t QuicWallTime::ToUNIXMicroseconds() const {
38 return microseconds_;
39}
40
41bool QuicWallTime::IsAfter(QuicWallTime other) const {
42 return microseconds_ > other.microseconds_;
43}
44
45bool QuicWallTime::IsBefore(QuicWallTime other) const {
46 return microseconds_ < other.microseconds_;
47}
48
49bool QuicWallTime::IsZero() const {
50 return microseconds_ == 0;
51}
52
53QuicTime::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
68QuicWallTime 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.
77QuicWallTime 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