1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
10 #include "opentelemetry/version.h"
12 OPENTELEMETRY_BEGIN_NAMESPACE
16 * @brief A timepoint relative to the system clock epoch.
18 * This is used for marking the beginning and end of an operation.
24 * @brief Initializes a system timestamp pointing to the start of the epoch.
26 SystemTimestamp() noexcept
: nanos_since_epoch_
{0} {}
29 * @brief Initializes a system timestamp from a duration.
31 * @param time_since_epoch Time elapsed since the beginning of the epoch.
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())}
40 * @brief Initializes a system timestamp based on a point in time.
42 * @param time_point A point in time.
44 /*implicit*/ SystemTimestamp(const std::chrono::system_clock::time_point
&time_point
) noexcept
45 : SystemTimestamp
{time_point
.time_since_epoch()}
49 * @brief Returns a time point for the time stamp.
51 * @return A time point corresponding to the time stamp.
53 operator std::chrono::system_clock::time_point() const noexcept
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_
})};
61 * @brief Returns the nanoseconds since the beginning of the epoch.
63 * @return Elapsed nanoseconds since the beginning of the epoch for this timestamp.
65 std::chrono::nanoseconds
time_since_epoch() const noexcept
67 return std::chrono::nanoseconds
{nanos_since_epoch_
};
71 * @brief Compare two steady time stamps.
73 * @return true if the two time stamps are equal.
75 bool operator==(const SystemTimestamp
&other
) const noexcept
77 return nanos_since_epoch_
== other
.nanos_since_epoch_
;
81 * @brief Compare two steady time stamps for inequality.
83 * @return true if the two time stamps are not equal.
85 bool operator!=(const SystemTimestamp
&other
) const noexcept
87 return nanos_since_epoch_
!= other
.nanos_since_epoch_
;
91 int64_t nanos_since_epoch_
;
95 * @brief A timepoint relative to the monotonic clock epoch
97 * This is used for calculating the duration of an operation.
103 * @brief Initializes a monotonic timestamp pointing to the start of the epoch.
105 SteadyTimestamp() noexcept
: nanos_since_epoch_
{0} {}
108 * @brief Initializes a monotonic timestamp from a duration.
110 * @param time_since_epoch Time elapsed since the beginning of the epoch.
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())}
119 * @brief Initializes a monotonic timestamp based on a point in time.
121 * @param time_point A point in time.
123 /*implicit*/ SteadyTimestamp(const std::chrono::steady_clock::time_point
&time_point
) noexcept
124 : SteadyTimestamp
{time_point
.time_since_epoch()}
128 * @brief Returns a time point for the time stamp.
130 * @return A time point corresponding to the time stamp.
132 operator std::chrono::steady_clock::time_point() const noexcept
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_
})};
140 * @brief Returns the nanoseconds since the beginning of the epoch.
142 * @return Elapsed nanoseconds since the beginning of the epoch for this timestamp.
144 std::chrono::nanoseconds
time_since_epoch() const noexcept
146 return std::chrono::nanoseconds
{nanos_since_epoch_
};
150 * @brief Compare two steady time stamps.
152 * @return true if the two time stamps are equal.
154 bool operator==(const SteadyTimestamp
&other
) const noexcept
156 return nanos_since_epoch_
== other
.nanos_since_epoch_
;
160 * @brief Compare two steady time stamps for inequality.
162 * @return true if the two time stamps are not equal.
164 bool operator!=(const SteadyTimestamp
&other
) const noexcept
166 return nanos_since_epoch_
!= other
.nanos_since_epoch_
;
170 int64_t nanos_since_epoch_
;
172 } // namespace common
173 OPENTELEMETRY_END_NAMESPACE