]> git.proxmox.com Git - ceph.git/blob - ceph/src/jaegertracing/opentelemetry-cpp/api/include/opentelemetry/common/timestamp.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / jaegertracing / opentelemetry-cpp / api / include / opentelemetry / common / timestamp.h
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 #pragma once
5
6 #include <algorithm>
7 #include <chrono>
8 #include <cstdint>
9
10 #include "opentelemetry/version.h"
11
12 OPENTELEMETRY_BEGIN_NAMESPACE
13 namespace common
14 {
15 /**
16 * @brief A timepoint relative to the system clock epoch.
17 *
18 * This is used for marking the beginning and end of an operation.
19 */
20 class SystemTimestamp
21 {
22 public:
23 /**
24 * @brief Initializes a system timestamp pointing to the start of the epoch.
25 */
26 SystemTimestamp() noexcept : nanos_since_epoch_{0} {}
27
28 /**
29 * @brief Initializes a system timestamp from a duration.
30 *
31 * @param time_since_epoch Time elapsed since the beginning of the epoch.
32 */
33 template <class Rep, class Period>
34 explicit SystemTimestamp(const std::chrono::duration<Rep, Period> &time_since_epoch) noexcept
35 : nanos_since_epoch_{static_cast<int64_t>(
36 std::chrono::duration_cast<std::chrono::nanoseconds>(time_since_epoch).count())}
37 {}
38
39 /**
40 * @brief Initializes a system timestamp based on a point in time.
41 *
42 * @param time_point A point in time.
43 */
44 /*implicit*/ SystemTimestamp(const std::chrono::system_clock::time_point &time_point) noexcept
45 : SystemTimestamp{time_point.time_since_epoch()}
46 {}
47
48 /**
49 * @brief Returns a time point for the time stamp.
50 *
51 * @return A time point corresponding to the time stamp.
52 */
53 operator std::chrono::system_clock::time_point() const noexcept
54 {
55 return std::chrono::system_clock::time_point{
56 std::chrono::duration_cast<std::chrono::system_clock::duration>(
57 std::chrono::nanoseconds{nanos_since_epoch_})};
58 }
59
60 /**
61 * @brief Returns the nanoseconds since the beginning of the epoch.
62 *
63 * @return Elapsed nanoseconds since the beginning of the epoch for this timestamp.
64 */
65 std::chrono::nanoseconds time_since_epoch() const noexcept
66 {
67 return std::chrono::nanoseconds{nanos_since_epoch_};
68 }
69
70 /**
71 * @brief Compare two steady time stamps.
72 *
73 * @return true if the two time stamps are equal.
74 */
75 bool operator==(const SystemTimestamp &other) const noexcept
76 {
77 return nanos_since_epoch_ == other.nanos_since_epoch_;
78 }
79
80 /**
81 * @brief Compare two steady time stamps for inequality.
82 *
83 * @return true if the two time stamps are not equal.
84 */
85 bool operator!=(const SystemTimestamp &other) const noexcept
86 {
87 return nanos_since_epoch_ != other.nanos_since_epoch_;
88 }
89
90 private:
91 int64_t nanos_since_epoch_;
92 };
93
94 /**
95 * @brief A timepoint relative to the monotonic clock epoch
96 *
97 * This is used for calculating the duration of an operation.
98 */
99 class SteadyTimestamp
100 {
101 public:
102 /**
103 * @brief Initializes a monotonic timestamp pointing to the start of the epoch.
104 */
105 SteadyTimestamp() noexcept : nanos_since_epoch_{0} {}
106
107 /**
108 * @brief Initializes a monotonic timestamp from a duration.
109 *
110 * @param time_since_epoch Time elapsed since the beginning of the epoch.
111 */
112 template <class Rep, class Period>
113 explicit SteadyTimestamp(const std::chrono::duration<Rep, Period> &time_since_epoch) noexcept
114 : nanos_since_epoch_{static_cast<int64_t>(
115 std::chrono::duration_cast<std::chrono::nanoseconds>(time_since_epoch).count())}
116 {}
117
118 /**
119 * @brief Initializes a monotonic timestamp based on a point in time.
120 *
121 * @param time_point A point in time.
122 */
123 /*implicit*/ SteadyTimestamp(const std::chrono::steady_clock::time_point &time_point) noexcept
124 : SteadyTimestamp{time_point.time_since_epoch()}
125 {}
126
127 /**
128 * @brief Returns a time point for the time stamp.
129 *
130 * @return A time point corresponding to the time stamp.
131 */
132 operator std::chrono::steady_clock::time_point() const noexcept
133 {
134 return std::chrono::steady_clock::time_point{
135 std::chrono::duration_cast<std::chrono::steady_clock::duration>(
136 std::chrono::nanoseconds{nanos_since_epoch_})};
137 }
138
139 /**
140 * @brief Returns the nanoseconds since the beginning of the epoch.
141 *
142 * @return Elapsed nanoseconds since the beginning of the epoch for this timestamp.
143 */
144 std::chrono::nanoseconds time_since_epoch() const noexcept
145 {
146 return std::chrono::nanoseconds{nanos_since_epoch_};
147 }
148
149 /**
150 * @brief Compare two steady time stamps.
151 *
152 * @return true if the two time stamps are equal.
153 */
154 bool operator==(const SteadyTimestamp &other) const noexcept
155 {
156 return nanos_since_epoch_ == other.nanos_since_epoch_;
157 }
158
159 /**
160 * @brief Compare two steady time stamps for inequality.
161 *
162 * @return true if the two time stamps are not equal.
163 */
164 bool operator!=(const SteadyTimestamp &other) const noexcept
165 {
166 return nanos_since_epoch_ != other.nanos_since_epoch_;
167 }
168
169 private:
170 int64_t nanos_since_epoch_;
171 };
172 } // namespace common
173 OPENTELEMETRY_END_NAMESPACE