Arene Base
Fundamental Utilities For Safety Critical C++
Loading...
Searching...
No Matches
time_point.hpp
Go to the documentation of this file.
1// Copyright 2026, Toyota Motor Corporation
2//
3// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4
5#ifndef INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_TIME_POINT_HPP_
6#define INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_TIME_POINT_HPP_
7
8// IWYU pragma: private, include <chrono>
9// IWYU pragma: friend "stdlib_detail/.*"
10
11// parasoft-begin-suppress CERT_CPP-DCL58-a-2 "Part of a standard library implementation"
12// parasoft-begin-suppress AUTOSAR-A17_6_1-a-2 "Part of a standard library implementation"
13
23
24namespace std {
25namespace chrono {
26
27// parasoft-begin-suppress AUTOSAR-A12_1_5-a "False positive: delegating constructors would not reduce duplication"
28// parasoft-begin-suppress AUTOSAR-M2_10_1-a "Similar names permitted by M2-10-1 Permit #1"
29// parasoft-begin-suppress AUTOSAR-A14_5_1-a "Template constructor is sufficiently constrained to not hide default ones"
30
31/// @brief A class representing a single point in time for the given clock
32/// @tparam Clock The clock on which this is a time point
33/// @tparam Duration The type representing time since the clock's epoch; must be an instance of @c std::chrono::duration
34template <class Clock, class Duration = typename Clock::duration>
36 // The standard requires that Clock be a clock in the sense of [time.clock], but none of the major implementations
37 // actually enforce this requirement, and it was removed in C++23, so we will not enforce it either for compatibility.
38 static_assert(
40 "The Duration of a std::time_point must be an instance of std::chrono::duration"
41 );
42
43 public:
44 /// @brief The clock on which this is a time point
45 // parasoft-begin-suppress CERT_CPP-DCL51-f "This name is required by the C++14 standard"
46 using clock = Clock;
47 // parasoft-end-suppress CERT_CPP-DCL51-f
48 /// @brief A @c std::chrono::duration specialization to be used to represent the time since the clock's epoch
49 using duration = Duration;
50 /// @brief The underlying arithmetic type used to represent the time since the clock's epoch
51 using rep = typename duration::rep;
52 /// @brief The period (length of one tick) used by <c>duration</c>, relative to 1 second
53 using period = typename duration::period;
54
55 private:
56 /// @brief The time since the clock's epoch represented by a particular @c time_point instance
57 duration time_since_epoch_;
58
59 public:
60 /// @brief Default-construct a @c time_point referring to <c>clock</c>'s epoch
61 constexpr time_point() noexcept(noexcept(duration(duration::zero())))
62 : time_since_epoch_{duration::zero()} {}
63
64 /// @brief Construct a @c time_point referring to the point a certain length of time after <c>clock</c>'s epoch
65 /// @param epoch_time The amount of time after <c>clock</c>'s epoch which the new @c time_point will represent
66 constexpr explicit time_point(duration const& epoch_time) noexcept(is_nothrow_copy_constructible_v<duration>)
67 : time_since_epoch_{epoch_time} {}
68
69 /// @brief Implicitly convert a @c time_point for the same clock, potentially even if it has a different @c duration
70 /// @tparam Duration2 The duration representation for the other time point; must be convertible to @c duration
71 /// @param other The time point to convert
72 template <class Duration2, arene::base::constraints<enable_if_t<is_convertible_v<Duration2, duration>>> = nullptr>
73 // NOLINTNEXTLINE(hicpp-explicit-conversions) C++14 specifies this constructor as implicit
76 : time_since_epoch_{other.time_since_epoch()} {}
77
78 /// @brief Return the time of a particular @c time_point as the duration since its clock's epoch
79 /// @return The time between this @c time_point and <c>clock</c>'s epoch
81 return time_since_epoch_;
82 }
83
84 /// @brief Move this @c time_point forward by a certain duration
85 /// @param dur The duration to move forward by
86 /// @note May overflow if @c duration::operator+= can overflow
87 auto operator+=(duration const& dur) noexcept(noexcept(time_since_epoch_ += dur)) -> time_point& {
88 time_since_epoch_ += dur;
89 return *this;
90 }
91
92 /// @brief Move this @c time_point backward by a certain duration
93 /// @param dur The duration to move backward by
94 /// @note May underflow if @c duration::operator-= can underflow
95 auto operator-=(duration const& dur) noexcept(noexcept(time_since_epoch_ -= dur)) -> time_point& {
96 time_since_epoch_ -= dur;
97 return *this;
98 }
99
100 /// @brief Get the minimum @c time_point representable by this class
101 /// @return The @c time_point corresponding to the earliest representable time
102 static constexpr auto min() noexcept(noexcept(time_point{duration::min()})) -> time_point {
103 return time_point{duration::min()};
104 }
105 /// @brief Get the maximum @c time_point representable by this class
106 /// @return The @c time_point corresponding to the latest representable time
107 static constexpr auto max() noexcept(noexcept(time_point{duration::max()})) -> time_point {
108 return time_point{duration::max()};
109 }
110};
111
112// parasoft-end-suppress AUTOSAR-A14_5_1-a
113// parasoft-end-suppress AUTOSAR-M2_10_1-a
114// parasoft-end-suppress AUTOSAR-A12_1_5-a
115
116// parasoft-begin-suppress AUTOSAR-M3_3_2-a "False positive: these functions have external linkage"
117
118/// @brief Add a @c time_point to a @c duration when the two are compatible
119/// @tparam Clock The clock of the <c>time_point</c>, deduced from @c left
120/// @tparam Duration1 The @c duration type of the <c>time_point</c>, deduced from @c left
121/// @tparam Rep2 The arithmetic representation of the <c>duration</c>, deduced from @c right
122/// @tparam Period2 The tick period of the <c>duration</c>, deduced from @c right
123/// @param left The @c time_point to be added
124/// @param right The @c duration to be added
125/// @return A time point representing @c left shifted by @c right
126// parasoft-begin-suppress AUTOSAR-M5_17_1-a "This behavior is mandated explicitly by the C++14 standard"
127template <class Clock, class Duration1, class Rep2, class Period2>
128constexpr auto operator+(time_point<Clock, Duration1> const& left, duration<Rep2, Period2> const& right) noexcept(
129 noexcept(time_point<Clock, common_type_t<Duration1, duration<Rep2, Period2>>>(left.time_since_epoch() + right))
132 return return_type(left.time_since_epoch() + right);
133}
134// parasoft-end-suppress AUTOSAR-M5_17_1-a
135
136/// @brief Add a @c time_point to a @c duration when the two are compatible
137/// @tparam Rep1 The arithmetic representation of the <c>duration</c>, deduced from @c left
138/// @tparam Period1 The tick period of the <c>duration</c>, deduced from @c left
139/// @tparam Clock The clock of the <c>time_point</c>, deduced from @c right
140/// @tparam Duration2 The @c duration type of the <c>time_point</c>, deduced from @c right
141/// @param left The @c duration to be added
142/// @param right The @c time_point to be added
143/// @return A time point representing @c right shifted by @c left
144template <class Rep1, class Period1, class Clock, class Duration2>
145constexpr auto operator+(duration<Rep1, Period1> const& left, time_point<Clock, Duration2> const& right) noexcept(
146 noexcept(right + left)
147) -> time_point<Clock, common_type_t<duration<Rep1, Period1>, Duration2>> {
148 return right + left;
149}
150
151/// @brief Subtract a @c duration from a @c time_point when the two are compatible
152/// @tparam Clock The clock of the <c>time_point</c>, deduced from @c left
153/// @tparam Duration1 The @c duration type of the <c>time_point</c>, deduced from @c left
154/// @tparam Rep2 The arithmetic representation of the <c>duration</c>, deduced from @c right
155/// @tparam Period2 The tick period of the <c>duration</c>, deduced from @c right
156/// @param left The @c time_point to be subtracted from
157/// @param right The @c duration to be subtracted
158/// @return A time point representing @c left shifted by the inverse of @c right
159// parasoft-begin-suppress AUTOSAR-M5_17_1-a "This behavior is mandated explicitly by the C++14 standard"
160template <class Clock, class Duration1, class Rep2, class Period2>
161constexpr auto operator-(time_point<Clock, Duration1> const& left, duration<Rep2, Period2> const& right) noexcept(
162 noexcept(left + (-right))
163) -> time_point<Clock, common_type_t<Duration1, duration<Rep2, Period2>>> {
164 return left + (-right);
165}
166// parasoft-end-suppress AUTOSAR-M5_17_1-a
167
168/// @brief Get the @c duration between two <c>time_point</c>s when the two are compatible
169/// @tparam Clock The clock shared by both <c>time_point</c>s, deduced from @c left and @c right
170/// @tparam Duration1 The @c duration type of the first <c>time_point</c>, deduced from @c left
171/// @tparam Duration2 The @c duration type of the second <c>time_point</c>, deduced from @c right
172/// @param left The first <c>time_point</c>, where the return value is "duration from here"
173/// @param right The second <c>time_point</c>, where the return value is "duration to here"
174/// @return A @c duration representing the time from @c left to @c right
175template <class Clock, class Duration1, class Duration2>
176constexpr auto operator-(time_point<Clock, Duration1> const& left, time_point<Clock, Duration2> const& right) noexcept(
177 noexcept(common_type_t<Duration1, Duration2>(left.time_since_epoch() - right.time_since_epoch()))
179 return left.time_since_epoch() - right.time_since_epoch();
180}
181
182// parasoft-begin-suppress AUTOSAR-A13_5_5-b "Comparisons are conditionally noexcept based on underlying comparison"
183
184/// @brief Compare two time points for equality if they're both from the same clock
185/// @tparam Clock The clock shared by both <c>time_point</c>s, deduced from @c left and @c right
186/// @tparam Duration1 The @c duration type of the first <c>time_point</c>, deduced from @c left
187/// @tparam Duration2 The @c duration type of the second <c>time_point</c>, deduced from @c right
188/// @param left The first <c>time_point</c>
189/// @param right The second <c>time_point</c>
190/// @return @c true if @c left and @c right refer to the same time point, otherwise @c false
191template <class Clock, class Duration1, class Duration2>
192constexpr auto operator==(time_point<Clock, Duration1> const& left, time_point<Clock, Duration2> const& right) noexcept(
193 noexcept(left.time_since_epoch() == right.time_since_epoch())
194) -> bool {
195 return left.time_since_epoch() == right.time_since_epoch();
196}
197
198/// @brief Compare two time points for inequality if they're both from the same clock
199/// @tparam Clock The clock shared by both <c>time_point</c>s, deduced from @c left and @c right
200/// @tparam Duration1 The @c duration type of the first <c>time_point</c>, deduced from @c left
201/// @tparam Duration2 The @c duration type of the second <c>time_point</c>, deduced from @c right
202/// @param left The first <c>time_point</c>
203/// @param right The second <c>time_point</c>
204/// @return @c true if @c left and @c right refer to different time points, otherwise @c false
205template <class Clock, class Duration1, class Duration2>
206constexpr auto operator!=(time_point<Clock, Duration1> const& left, time_point<Clock, Duration2> const& right) noexcept(
207 noexcept(left == right)
208) -> bool {
209 return !(left == right);
210}
211
212/// @brief Compare two time points for ordering if they're both from the same clock
213/// @tparam Clock The clock shared by both <c>time_point</c>s, deduced from @c left and @c right
214/// @tparam Duration1 The @c duration type of the first <c>time_point</c>, deduced from @c left
215/// @tparam Duration2 The @c duration type of the second <c>time_point</c>, deduced from @c right
216/// @param left The first <c>time_point</c>
217/// @param right The second <c>time_point</c>
218/// @return @c true if @c left refers to a time point earlier than <c>right</c>, otherwise @c false
219template <class Clock, class Duration1, class Duration2>
220constexpr auto operator<(time_point<Clock, Duration1> const& left, time_point<Clock, Duration2> const& right) noexcept(
221 noexcept(left.time_since_epoch() < right.time_since_epoch())
222) -> bool {
223 return left.time_since_epoch() < right.time_since_epoch();
224}
225
226/// @brief Compare two time points for ordering if they're both from the same clock
227/// @tparam Clock The clock shared by both <c>time_point</c>s, deduced from @c left and @c right
228/// @tparam Duration1 The @c duration type of the first <c>time_point</c>, deduced from @c left
229/// @tparam Duration2 The @c duration type of the second <c>time_point</c>, deduced from @c right
230/// @param left The first <c>time_point</c>
231/// @param right The second <c>time_point</c>
232/// @return @c true if @c left refers to a time point no later than <c>right</c>, otherwise @c false
233template <class Clock, class Duration1, class Duration2>
234constexpr auto operator<=(time_point<Clock, Duration1> const& left, time_point<Clock, Duration2> const& right) noexcept(
235 noexcept(right < left)
236) -> bool {
237 return !(right < left);
238}
239
240/// @brief Compare two time points for ordering if they're both from the same clock
241/// @tparam Clock The clock shared by both <c>time_point</c>s, deduced from @c left and @c right
242/// @tparam Duration1 The @c duration type of the first <c>time_point</c>, deduced from @c left
243/// @tparam Duration2 The @c duration type of the second <c>time_point</c>, deduced from @c right
244/// @param left The first <c>time_point</c>
245/// @param right The second <c>time_point</c>
246/// @return @c true if @c left refers to a time point later than <c>right</c>, otherwise @c false
247template <class Clock, class Duration1, class Duration2>
248constexpr auto operator>(time_point<Clock, Duration1> const& left, time_point<Clock, Duration2> const& right) noexcept(
249 noexcept(right < left)
250) -> bool {
251 return right < left;
252}
253
254/// @brief Compare two time points for ordering if they're both from the same clock
255/// @tparam Clock The clock shared by both <c>time_point</c>s, deduced from @c left and @c right
256/// @tparam Duration1 The @c duration type of the first <c>time_point</c>, deduced from @c left
257/// @tparam Duration2 The @c duration type of the second <c>time_point</c>, deduced from @c right
258/// @param left The first <c>time_point</c>
259/// @param right The second <c>time_point</c>
260/// @return @c true if @c left refers to a time point no earlier than <c>right</c>, otherwise @c false
261template <class Clock, class Duration1, class Duration2>
262constexpr auto operator>=(time_point<Clock, Duration1> const& left, time_point<Clock, Duration2> const& right) noexcept(
263 noexcept(left < right)
264) -> bool {
265 return !(left < right);
266}
267
268// parasoft-end-suppress AUTOSAR-A13_5_5-b
269
270/// @brief Cast a @c time_point into another type with the same clock but a different duration type
271/// @tparam ToDuration The @c duration type to be used after the cast
272/// @tparam Clock The clock of the <c>time_point</c>, deduced from @c point
273/// @tparam Duration The @c duration type of the existing <c>time_point</c>, deduced from @c point
274/// @param point The time point to cast to the new duration @c ToDuration
275/// @return A representation of the same time point represented by <c>point</c>, but using duration type @c ToDuration
276template <
277 class ToDuration,
278 class Clock,
279 class Duration,
282) noexcept(noexcept(time_point<Clock, ToDuration>(duration_cast<ToDuration>(point.time_since_epoch()))))
284 return time_point<Clock, ToDuration>(duration_cast<ToDuration>(point.time_since_epoch()));
285}
286
287// parasoft-end-suppress AUTOSAR-M3_3_2-a
288
289} // namespace chrono
290} // namespace std
291
292#endif // INCLUDE_GUARD_ARENE_BASE_STDLIB_INCLUDE_STDLIB_DETAIL_TIME_POINT_HPP_
Forward declaration of duration class template.
Definition duration.hpp:308
static constexpr auto zero() noexcept(noexcept(duration(duration_values< rep >::zero()))) -> duration
Get a duration of length zero, corresponding to no time passing.
Definition duration.hpp:468
static constexpr auto max() noexcept(noexcept(duration(duration_values< rep >::max()))) -> duration
Get a maximally positive duration.
Definition duration.hpp:481
Period period
The period type representing the tick period in seconds.
Definition duration.hpp:321
Rep rep
The arithmetic type representing the number of ticks.
Definition duration.hpp:319
static constexpr auto min() noexcept(noexcept(duration(duration_values< rep >::min()))) -> duration
Get a minimally positive duration (for signed representations, a maximally negative duration)
Definition duration.hpp:475
A class representing a single point in time for the given clock.
Definition time_point.hpp:35
static constexpr auto min() noexcept(noexcept(time_point{duration::min()})) -> time_point
Get the minimum time_point representable by this class.
Definition time_point.hpp:102
constexpr time_point() noexcept(noexcept(duration(duration::zero())))
Default-construct a time_point referring to clock's epoch.
Definition time_point.hpp:61
auto operator+=(duration const &dur) noexcept(noexcept(time_since_epoch_+=dur)) -> time_point &
Move this time_point forward by a certain duration.
Definition time_point.hpp:87
typename duration::period period
The period (length of one tick) used by duration, relative to 1 second.
Definition time_point.hpp:53
typename duration::rep rep
The underlying arithmetic type used to represent the time since the clock's epoch.
Definition time_point.hpp:51
Clock clock
The clock on which this is a time point.
Definition time_point.hpp:46
constexpr auto time_since_epoch() const noexcept(is_nothrow_copy_constructible_v< duration >) -> duration
Return the time of a particular time_point as the duration since its clock's epoch.
Definition time_point.hpp:80
Duration duration
A std::chrono::duration specialization to be used to represent the time since the clock's epoch.
Definition time_point.hpp:49
auto operator-=(duration const &dur) noexcept(noexcept(time_since_epoch_ -=dur)) -> time_point &
Move this time_point backward by a certain duration.
Definition time_point.hpp:95
constexpr time_point(time_point< clock, Duration2 > const &other) noexcept(is_nothrow_constructible_v< duration, Duration2 >)
Implicitly convert a time_point for the same clock, potentially even if it has a different duration.
Definition time_point.hpp:74
constexpr time_point(duration const &epoch_time) noexcept(is_nothrow_copy_constructible_v< duration >)
Construct a time_point referring to the point a certain length of time after clock's epoch.
Definition time_point.hpp:66
static constexpr auto max() noexcept(noexcept(time_point{duration::max()})) -> time_point
Get the maximum time_point representable by this class.
Definition time_point.hpp:107
Public export header for the constraints subpackage.
constexpr bool is_instantiation_of_v
Definition is_instantiation_of.hpp:16
typename constraints_impl::constraints< EnableIfs... >::type constraints
Helper used to express template constraints in a consistent way using std::enable_if_t.
Definition constraints.hpp:51
Definition duration.hpp:37
constexpr auto operator!=(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) noexcept(noexcept(lhs==rhs)) -> bool
Inequality comparison operator for duration types.
Definition duration.hpp:528
constexpr auto operator>=(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) noexcept(noexcept(lhs< rhs)) -> bool
Greater-than-or-equal comparison operator for duration types.
Definition duration.hpp:590
constexpr auto duration_cast(duration< Rep, Period > const &duration) noexcept(noexcept(duration.count()) &&noexcept(chrono_detail::duration_cast_impl< ToDuration, ConversionFactor, CommonRep >(duration.count(), std::bool_constant< ConversionFactor::num==1 >{}, std::bool_constant< ConversionFactor::den==1 >{}))) -> ToDuration
Convert a duration to a different duration type.
Definition duration.hpp:277
constexpr auto operator<=(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) noexcept(noexcept(rhs< lhs)) -> bool
Less-than-or-equal comparison operator for duration types.
Definition duration.hpp:560
constexpr auto operator==(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) noexcept(chrono_detail::duration_op_noexcept_v< equal_to<>, duration< Rep1, Period1 >, duration< Rep2, Period2 > >) -> bool
Equality comparison operator for duration types.
Definition duration.hpp:508
constexpr auto operator>(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) noexcept(noexcept(rhs< lhs)) -> bool
Greater-than comparison operator for duration types.
Definition duration.hpp:575
constexpr auto operator<(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) noexcept(chrono_detail::duration_op_noexcept_v< less<>, duration< Rep1, Period1 >, duration< Rep2, Period2 > >) -> bool
Less-than comparison operator for duration types.
Definition duration.hpp:543
constexpr auto operator-(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) noexcept(chrono_detail::duration_op_noexcept_v< minus<>, duration< Rep1, Period1 >, duration< Rep2, Period2 > >) -> std::common_type_t< duration< Rep1, Period1 >, duration< Rep2, Period2 > >
Subtraction operator for duration types.
Definition duration.hpp:628
constexpr auto time_point_cast(time_point< Clock, Duration > const &point) noexcept(noexcept(time_point< Clock, ToDuration >(duration_cast< ToDuration >(point.time_since_epoch())))) -> time_point< Clock, ToDuration >
Cast a time_point into another type with the same clock but a different duration type.
Definition time_point.hpp:281
constexpr auto operator+(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) noexcept(chrono_detail::duration_op_noexcept_v< plus<>, duration< Rep1, Period1 >, duration< Rep2, Period2 > >) -> std::common_type_t< duration< Rep1, Period1 >, duration< Rep2, Period2 > >
Addition operator for duration types.
Definition duration.hpp:610
STL namespace.
typename common_type< Ts... >::type common_type_t
Helper alias template for common_type.
Definition common_type.hpp:99
constexpr bool is_nothrow_constructible_v
Type trait to detect if a type is constructible from arguments of the specified types and is noexcept...
constexpr bool is_nothrow_copy_constructible_v
Type trait to detect if an instance of T is copy constructible without throwing.