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// QuicTime represents one point in time, stored in microsecond resolution.
6// QuicTime is monotonically increasing, even across system clock adjustments.
7// The epoch (time 0) of QuicTime is unspecified.
8//
9// This implementation wraps the classes base::TimeTicks and base::TimeDelta.
10
11#ifndef NET_QUIC_QUIC_TIME_H_
12#define NET_QUIC_QUIC_TIME_H_
13
14#include "base/basictypes.h"
15#include "base/time/time.h"
16#include "net/base/net_export.h"
17
18namespace net {
19
20static const uint64 kNumMicrosPerSecond = base::Time::kMicrosecondsPerSecond;
21
22// A QuicTime is a purely relative time. QuicTime values from different clocks
23// cannot be compared to each other. If you need an absolute time, see
24// QuicWallTime, below.
25class NET_EXPORT_PRIVATE QuicTime {
26 public:
27  // A QuicTime::Delta represents the signed difference between two points in
28  // time, stored in microsecond resolution.
29  class NET_EXPORT_PRIVATE Delta {
30   public:
31    explicit Delta(base::TimeDelta delta);
32
33    // Create a object with an offset of 0.
34    static Delta Zero();
35
36    // Create a object with infinite offset time.
37    static Delta Infinite();
38
39    // Converts a number of seconds to a time offset.
40    static Delta FromSeconds(int64 secs);
41
42    // Converts a number of milliseconds to a time offset.
43    static Delta FromMilliseconds(int64 ms);
44
45    // Converts a number of microseconds to a time offset.
46    static Delta FromMicroseconds(int64 us);
47
48    // Converts the time offset to a rounded number of seconds.
49    int64 ToSeconds() const;
50
51    // Converts the time offset to a rounded number of milliseconds.
52    int64 ToMilliseconds() const;
53
54    // Converts the time offset to a rounded number of microseconds.
55    int64 ToMicroseconds() const;
56
57    Delta Add(const Delta& delta) const;
58
59    Delta Subtract(const Delta& delta) const;
60
61    bool IsZero() const;
62
63    bool IsInfinite() const;
64
65   private:
66    base::TimeDelta delta_;
67
68    friend class QuicTime;
69    friend class QuicClock;
70  };
71
72  explicit QuicTime(base::TimeTicks ticks);
73
74  // Creates a new QuicTime with an internal value of 0.  IsInitialized()
75  // will return false for these times.
76  static QuicTime Zero();
77
78  // Produce the internal value to be used when logging.  This value
79  // represents the number of microseconds since some epoch.  It may
80  // be the UNIX epoch on some platforms.  On others, it may
81  // be a CPU ticks based value.
82  int64 ToDebuggingValue() const;
83
84  bool IsInitialized() const;
85
86  QuicTime Add(const Delta& delta) const;
87
88  QuicTime Subtract(const Delta& delta) const;
89
90  Delta Subtract(const QuicTime& other) const;
91
92 private:
93  friend bool operator==(QuicTime lhs, QuicTime rhs);
94  friend bool operator<(QuicTime lhs, QuicTime rhs);
95
96  friend class QuicClock;
97  friend class QuicClockTest;
98
99  base::TimeTicks ticks_;
100};
101
102// A QuicWallTime represents an absolute time that is globally consistent. It
103// provides, at most, one second granularity and, in practice, clock-skew means
104// that you shouldn't even depend on that.
105class NET_EXPORT_PRIVATE QuicWallTime {
106 public:
107  // FromUNIXSeconds constructs a QuicWallTime from a count of the seconds
108  // since the UNIX epoch.
109  static QuicWallTime FromUNIXSeconds(uint64 seconds);
110
111  // Zero returns a QuicWallTime set to zero. IsZero will return true for this
112  // value.
113  static QuicWallTime Zero();
114
115  // ToUNIXSeconds converts a QuicWallTime into a count of seconds since the
116  // UNIX epoch.
117  uint64 ToUNIXSeconds() const;
118
119  bool IsAfter(QuicWallTime other) const;
120  bool IsBefore(QuicWallTime other) const;
121
122  // IsZero returns true if this object is the result of calling |Zero|.
123  bool IsZero() const;
124
125  // AbsoluteDifference returns the absolute value of the time difference
126  // between |this| and |other|.
127  QuicTime::Delta AbsoluteDifference(QuicWallTime other) const;
128
129  // Add returns a new QuicWallTime that represents the time of |this| plus
130  // |delta|.
131  QuicWallTime Add(QuicTime::Delta delta) const;
132
133  // Subtract returns a new QuicWallTime that represents the time of |this|
134  // minus |delta|.
135  QuicWallTime Subtract(QuicTime::Delta delta) const;
136
137 private:
138  explicit QuicWallTime(uint64 seconds);
139
140  uint64 seconds_;
141};
142
143// Non-member relational operators for QuicTime::Delta.
144inline bool operator==(QuicTime::Delta lhs, QuicTime::Delta rhs) {
145  return lhs.ToMicroseconds() == rhs.ToMicroseconds();
146}
147inline bool operator!=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
148  return !(lhs == rhs);
149}
150inline bool operator<(QuicTime::Delta lhs, QuicTime::Delta rhs) {
151  return lhs.ToMicroseconds() < rhs.ToMicroseconds();
152}
153inline bool operator>(QuicTime::Delta lhs, QuicTime::Delta rhs) {
154  return rhs < lhs;
155}
156inline bool operator<=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
157  return !(rhs < lhs);
158}
159inline bool operator>=(QuicTime::Delta lhs, QuicTime::Delta rhs) {
160  return !(lhs < rhs);
161}
162// Non-member relational operators for QuicTime.
163inline bool operator==(QuicTime lhs, QuicTime rhs) {
164  return lhs.ticks_ == rhs.ticks_;
165}
166inline bool operator!=(QuicTime lhs, QuicTime rhs) {
167  return !(lhs == rhs);
168}
169inline bool operator<(QuicTime lhs, QuicTime rhs) {
170  return lhs.ticks_ < rhs.ticks_;
171}
172inline bool operator>(QuicTime lhs, QuicTime rhs) {
173  return rhs < lhs;
174}
175inline bool operator<=(QuicTime lhs, QuicTime rhs) {
176  return !(rhs < lhs);
177}
178inline bool operator>=(QuicTime lhs, QuicTime rhs) {
179  return !(lhs < rhs);
180}
181
182}  // namespace net
183
184#endif  // NET_QUIC_QUIC_TIME_H_
185