blob: 6164cafaa5af5fb26c57839735ebeddcf9ab90e0 [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
vasilvv89713d02020-02-11 14:33:26 -08005#ifndef QUICHE_QUIC_CORE_QUIC_CLOCK_H_
6#define QUICHE_QUIC_CORE_QUIC_CLOCK_H_
QUICHE teama6ef0a62019-03-07 20:34:33 -05007
QUICHE team5be974e2020-12-29 18:35:24 -05008#include "quic/core/quic_time.h"
9#include "quic/platform/api/quic_export.h"
QUICHE teama6ef0a62019-03-07 20:34:33 -050010
danzh48ba61a2019-04-30 12:05:42 -070011/* API_DESCRIPTION
12 QuicClock is used by QUIC core to get current time. Its instance is created by
13 applications and passed into QuicDispatcher and QuicConnectionHelperInterface.
14 API-DESCRIPTION */
15
QUICHE teama6ef0a62019-03-07 20:34:33 -050016namespace quic {
17
18// Interface for retrieving the current time.
19class QUIC_EXPORT_PRIVATE QuicClock {
20 public:
21 QuicClock();
22 virtual ~QuicClock();
23
24 QuicClock(const QuicClock&) = delete;
25 QuicClock& operator=(const QuicClock&) = delete;
26
27 // Compute the offset between this clock with the Unix Epoch clock.
28 // Return the calibrated offset between WallNow() and Now(), in the form of
29 // (wallnow_in_us - now_in_us).
30 // The return value can be used by SetCalibrationOffset() to actually
31 // calibrate the clock, or all instances of this clock type.
32 QuicTime::Delta ComputeCalibrationOffset() const;
33
34 // Calibrate this clock. A calibrated clock guarantees that the
35 // ConvertWallTimeToQuicTime() function always return the same result for the
36 // same walltime.
37 // Should not be called more than once for each QuicClock.
38 void SetCalibrationOffset(QuicTime::Delta offset);
39
40 // Returns the approximate current time as a QuicTime object.
41 virtual QuicTime ApproximateNow() const = 0;
42
43 // Returns the current time as a QuicTime object.
44 // Note: this use significant resources please use only if needed.
45 virtual QuicTime Now() const = 0;
46
47 // WallNow returns the current wall-time - a time that is consistent across
48 // different clocks.
49 virtual QuicWallTime WallNow() const = 0;
50
51 // Converts |walltime| to a QuicTime relative to this clock's epoch.
52 virtual QuicTime ConvertWallTimeToQuicTime(
53 const QuicWallTime& walltime) const;
54
55 protected:
56 // Creates a new QuicTime using |time_us| as the internal value.
57 QuicTime CreateTimeFromMicroseconds(uint64_t time_us) const {
58 return QuicTime(time_us);
59 }
60
61 private:
62 // True if |calibration_offset_| is valid.
63 bool is_calibrated_;
64 // If |is_calibrated_|, |calibration_offset_| is the (fixed) offset between
65 // the Unix Epoch clock and this clock.
66 // In other words, the offset between WallNow() and Now().
67 QuicTime::Delta calibration_offset_;
68};
69
70} // namespace quic
71
vasilvv89713d02020-02-11 14:33:26 -080072#endif // QUICHE_QUIC_CORE_QUIC_CLOCK_H_