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
14#include "arene/base/constraints.hpp"
15#include "arene/base/type_traits/is_instantiation_of.hpp"
16#include "stdlib/include/stdlib_detail/common_type.hpp"
17#include "stdlib/include/stdlib_detail/duration.hpp"
18#include "stdlib/include/stdlib_detail/is_constructible.hpp"
19#include "stdlib/include/stdlib_detail/is_copy_assignable.hpp"
20#include "stdlib/include/stdlib_detail/is_copy_constructible.hpp"
21#include "stdlib/include/stdlib_detail/is_move_assignable.hpp"
22#include "stdlib/include/stdlib_detail/is_move_constructible.hpp"
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
74 constexpr time_point(time_point<clock, Duration2> const& other
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
80 constexpr auto time_since_epoch() const noexcept(is_nothrow_copy_constructible_v<duration>) -> duration {
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(
131 using return_type = time_point<Clock, common_type_t<Duration1, duration<Rep2, Period2>>>;
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)
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))
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(
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(
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(
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,
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_
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
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
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
Definition duration.hpp:37
constexpr auto operator==(time_point< Clock, Duration1 > const &left, time_point< Clock, Duration2 > const &right) noexcept(noexcept(left.time_since_epoch()==right.time_since_epoch())) -> bool
Compare two time points for equality if they're both from the same clock.
Definition time_point.hpp:192
constexpr auto operator+(duration< Rep1, Period1 > const &left, time_point< Clock, Duration2 > const &right) noexcept(noexcept(right+left)) -> time_point< Clock, common_type_t< duration< Rep1, Period1 >, Duration2 > >
Add a time_point to a duration when the two are compatible.
Definition time_point.hpp:145
constexpr auto operator>(time_point< Clock, Duration1 > const &left, time_point< Clock, Duration2 > const &right) noexcept(noexcept(right< left)) -> bool
Compare two time points for ordering if they're both from the same clock.
Definition time_point.hpp:248
constexpr auto operator<=(time_point< Clock, Duration1 > const &left, time_point< Clock, Duration2 > const &right) noexcept(noexcept(right< left)) -> bool
Compare two time points for ordering if they're both from the same clock.
Definition time_point.hpp:234
constexpr auto operator-(time_point< Clock, Duration1 > const &left, time_point< Clock, Duration2 > const &right) noexcept(noexcept(common_type_t< Duration1, Duration2 >(left.time_since_epoch() - right.time_since_epoch()))) -> common_type_t< Duration1, Duration2 >
Get the duration between two time_points when the two are compatible.
Definition time_point.hpp:176
constexpr auto operator>=(time_point< Clock, Duration1 > const &left, time_point< Clock, Duration2 > const &right) noexcept(noexcept(left< right)) -> bool
Compare two time points for ordering if they're both from the same clock.
Definition time_point.hpp:262
constexpr auto operator<(time_point< Clock, Duration1 > const &left, time_point< Clock, Duration2 > const &right) noexcept(noexcept(left.time_since_epoch()< right.time_since_epoch())) -> bool
Compare two time points for ordering if they're both from the same clock.
Definition time_point.hpp:220
constexpr auto operator-(time_point< Clock, Duration1 > const &left, duration< Rep2, Period2 > const &right) noexcept(noexcept(left+(-right))) -> time_point< Clock, common_type_t< Duration1, duration< Rep2, Period2 > > >
Subtract a duration from a time_point when the two are compatible.
Definition time_point.hpp:161
constexpr auto operator+(time_point< Clock, Duration1 > const &left, duration< Rep2, Period2 > const &right) noexcept(noexcept(time_point< Clock, common_type_t< Duration1, duration< Rep2, Period2 > > >(left.time_since_epoch()+right))) -> time_point< Clock, common_type_t< Duration1, duration< Rep2, Period2 > > >
Add a time_point to a duration when the two are compatible.
Definition time_point.hpp:128
constexpr auto operator!=(time_point< Clock, Duration1 > const &left, time_point< Clock, Duration2 > const &right) noexcept(noexcept(left==right)) -> bool
Compare two time points for inequality if they're both from the same clock.
Definition time_point.hpp:206
constexpr auto operator()(::arene::base::result< void, E > const &value) const noexcept(noexcept(hash< E >{}(std::declval< E const & >()))) -> std::size_t
Calculate the hash of a result.
Definition result.hpp:1827