tetl 0.1.0
Embedded Template Library
Loading...
Searching...
No Matches
time_point.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: BSL-1.0
2// SPDX-FileCopyrightText: Copyright (C) 2021 Tobias Hienzsch
3
4#ifndef TETL_CHRONO_TIME_POINT_HPP
5#define TETL_CHRONO_TIME_POINT_HPP
6
7#include <etl/_type_traits/common_type.hpp>
8#include <etl/_type_traits/is_convertible.hpp>
9
10namespace etl::chrono {
11
12/// Class template time_point represents a point in time. It is
13/// implemented as if it stores a value of type Duration indicating the time
14/// interval from the start of the Clock's epoch.
15///
16/// \tparam Clock Must meet the requirements for Clock
17///
18/// https://en.cppreference.com/w/cpp/named_req/Clock
19///
20/// \ingroup chrono
21template <typename Clock, typename Duration = typename Clock::duration>
22struct time_point {
23 /// Clock, the clock on which this time point is measured.
24 using clock = Clock;
25
26 /// Duration, a duration type used to measure the time since epoch.
27 using duration = Duration;
28
29 /// Rep, an arithmetic type representing the number of ticks of the
30 /// duration.
31 using rep = typename duration::rep;
32
33 /// Period, a ratio type representing the tick period of the
34 /// duration.
35 using period = typename duration::period;
36
37 /// Constructs a new time_point from one of several optional data
38 /// sources. Default constructor, creates a time_point representing the
39 /// Clock's epoch (i.e., time_since_epoch() is zero).
40 constexpr time_point() noexcept = default;
41
42 /// Constructs a new time_point from one of several optional data
43 /// sources. Constructs a time_point at Clock's epoch plus d.
44 constexpr explicit time_point(duration const& d) noexcept
45 : _d{d}
46 {
47 }
48
49 /// Constructs a new time_point from one of several optional data
50 /// sources. Constructs a time_point by converting t to duration. This
51 /// constructor only participates in overload resolution if Duration2 is
52 /// implicitly convertible to duration.
53 template <typename Dur2>
54 requires(is_convertible_v<Dur2, duration>)
55 constexpr time_point(time_point<clock, Dur2> const& t)
56 : _d{t.time_since_epch()}
57 {
58 }
59
60 /// Returns a duration representing the amount of time between *this
61 /// and the clock's epoch.
62 [[nodiscard]] constexpr auto time_since_epoch() const noexcept -> duration
63 {
64 return _d;
65 }
66
67 /// \brief Modifies the time point by the given duration. Applies the offset
68 /// d to pt. Effectively, d is added to the internally stored duration d_ as
69 /// d_
70 /// += d.
71 constexpr auto operator+=(duration const& d) noexcept -> time_point&
72 {
73 _d += d;
74 return *this;
75 }
76
77 /// \brief Modifies the time point by the given duration. Applies the offset
78 /// d to pt in negative direction. Effectively, d is subtracted from
79 /// internally stored duration d_ as d_ -= d.
80 constexpr auto operator-=(duration const& d) noexcept -> time_point&
81 {
82 _d -= d;
83 return *this;
84 }
85
86 /// \brief Modifies the point in time *this represents by one tick of the
87 /// duration.
88 constexpr auto operator++() noexcept -> time_point&
89 {
90 ++_d;
91 return *this;
92 }
93
94 /// \brief Modifies the point in time *this represents by one tick of the
95 /// duration.
96 constexpr auto operator++(int) noexcept -> time_point
97 {
98 return time_point(_d++);
99 }
100
101 /// \brief Modifies the point in time *this represents by one tick of the
102 /// duration.
103 constexpr auto operator--() noexcept -> time_point&
104 {
105 --_d;
106 return *this;
107 }
108
109 /// \brief Modifies the point in time *this represents by one tick of the
110 /// duration.
111 constexpr auto operator--(int) noexcept -> time_point
112 {
113 return time_point(_d--);
114 }
115
116 /// \brief Returns a time_point with the smallest possible duration,
117 [[nodiscard]] static constexpr auto min() noexcept -> time_point
118 {
119 return time_point(duration::min());
120 }
121
122 /// \brief Returns a time_point with the largest possible duration,
123 [[nodiscard]] static constexpr auto max() noexcept -> time_point
124 {
125 return time_point(duration::max());
126 }
127
128private:
129 duration _d{};
130};
131
132/// \brief Compares two time points. The comparison is done by comparing the
133/// results time_since_epoch() for the time points.
134template <typename Clock, typename Dur1, typename Dur2>
135[[nodiscard]] constexpr auto operator==(time_point<Clock, Dur1> const& lhs, time_point<Clock, Dur2> const& rhs) noexcept
136 -> bool
137{
138 return lhs.time_since_epoch() == rhs.time_since_epoch();
139}
140
141/// \brief Compares two time points. The comparison is done by comparing the
142/// results time_since_epoch() for the time points.
143template <typename Clock, typename Dur1, typename Dur2>
144[[nodiscard]] constexpr auto operator<(time_point<Clock, Dur1> const& lhs, time_point<Clock, Dur2> const& rhs) noexcept
145 -> bool
146{
147 return lhs.time_since_epoch() < rhs.time_since_epoch();
148}
149
150/// \brief Compares two time points. The comparison is done by comparing the
151/// results time_since_epoch() for the time points.
152template <typename Clock, typename Dur1, typename Dur2>
153[[nodiscard]] constexpr auto operator<=(time_point<Clock, Dur1> const& lhs, time_point<Clock, Dur2> const& rhs) noexcept
154 -> bool
155{
156 return lhs.time_since_epoch() <= rhs.time_since_epoch();
157}
158
159/// \brief Compares two time points. The comparison is done by comparing the
160/// results time_since_epoch() for the time points.
161template <typename Clock, typename Dur1, typename Dur2>
162[[nodiscard]] constexpr auto operator>(time_point<Clock, Dur1> const& lhs, time_point<Clock, Dur2> const& rhs) noexcept
163 -> bool
164{
165 return lhs.time_since_epoch() > rhs.time_since_epoch();
166}
167
168/// \brief Compares two time points. The comparison is done by comparing the
169/// results time_since_epoch() for the time points.
170template <typename Clock, typename Dur1, typename Dur2>
171[[nodiscard]] constexpr auto operator>=(time_point<Clock, Dur1> const& lhs, time_point<Clock, Dur2> const& rhs) noexcept
172 -> bool
173{
174 return lhs.time_since_epoch() >= rhs.time_since_epoch();
175}
176
177} // namespace etl::chrono
178
179namespace etl {
180
181/// \brief Exposes the type named type, which is the common type of two
182/// chrono::time_points.
183template <typename Clock, typename Duration1, typename Duration2>
184struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
185 using type = chrono::time_point<Clock, common_type_t<Duration1, Duration2>>;
186};
187
188} // namespace etl
189
190#endif // TETL_CHRONO_TIME_POINT_HPP
Definition abs.hpp:12
constexpr auto operator>(time_point< Clock, Dur1 > const &lhs, time_point< Clock, Dur2 > const &rhs) noexcept -> bool
Compares two time points. The comparison is done by comparing the results time_since_epoch() for the ...
Definition time_point.hpp:162
constexpr auto operator<(time_point< Clock, Dur1 > const &lhs, time_point< Clock, Dur2 > const &rhs) noexcept -> bool
Compares two time points. The comparison is done by comparing the results time_since_epoch() for the ...
Definition time_point.hpp:144
constexpr auto operator<=(time_point< Clock, Dur1 > const &lhs, time_point< Clock, Dur2 > const &rhs) noexcept -> bool
Compares two time points. The comparison is done by comparing the results time_since_epoch() for the ...
Definition time_point.hpp:153
constexpr auto operator==(time_point< Clock, Dur1 > const &lhs, time_point< Clock, Dur2 > const &rhs) noexcept -> bool
Compares two time points. The comparison is done by comparing the results time_since_epoch() for the ...
Definition time_point.hpp:135
constexpr auto operator>=(time_point< Clock, Dur1 > const &lhs, time_point< Clock, Dur2 > const &rhs) noexcept -> bool
Compares two time points. The comparison is done by comparing the results time_since_epoch() for the ...
Definition time_point.hpp:171
Definition adjacent_find.hpp:9
Class template time_point represents a point in time. It is implemented as if it stores a value of ty...
Definition time_point.hpp:22
constexpr time_point(time_point< clock, Dur2 > const &t)
Constructs a new time_point from one of several optional data sources. Constructs a time_point by con...
Definition time_point.hpp:55
constexpr auto time_since_epoch() const noexcept -> duration
Returns a duration representing the amount of time between *this and the clock's epoch.
Definition time_point.hpp:62
static constexpr auto max() noexcept -> time_point
Returns a time_point with the largest possible duration,.
Definition time_point.hpp:123
constexpr time_point() noexcept=default
Constructs a new time_point from one of several optional data sources. Default constructor,...
static constexpr auto min() noexcept -> time_point
Returns a time_point with the smallest possible duration,.
Definition time_point.hpp:117
constexpr auto operator++(int) noexcept -> time_point
Modifies the point in time *this represents by one tick of the duration.
Definition time_point.hpp:96
constexpr auto operator++() noexcept -> time_point &
Modifies the point in time *this represents by one tick of the duration.
Definition time_point.hpp:88
constexpr auto operator+=(duration const &d) noexcept -> time_point &
Modifies the time point by the given duration. Applies the offset d to pt. Effectively,...
Definition time_point.hpp:71
constexpr auto operator--() noexcept -> time_point &
Modifies the point in time *this represents by one tick of the duration.
Definition time_point.hpp:103
constexpr time_point(duration const &d) noexcept
Constructs a new time_point from one of several optional data sources. Constructs a time_point at Clo...
Definition time_point.hpp:44
constexpr auto operator--(int) noexcept -> time_point
Modifies the point in time *this represents by one tick of the duration.
Definition time_point.hpp:111
constexpr auto operator-=(duration const &d) noexcept -> time_point &
Modifies the time point by the given duration. Applies the offset d to pt in negative direction....
Definition time_point.hpp:80