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
3#ifndef TETL_CHRONO_TIME_POINT_HPP
4#define TETL_CHRONO_TIME_POINT_HPP
5
8
9namespace etl::chrono {
10
20template <typename Clock, typename Duration = typename Clock::duration>
21struct time_point {
23 using clock = Clock;
24
26 using duration = Duration;
27
30 using rep = typename duration::rep;
31
34 using period = typename duration::period;
35
39 constexpr time_point() noexcept = default;
40
43 constexpr explicit time_point(duration const& d) noexcept
44 : _d{d}
45 {
46 }
47
52 template <typename Dur2>
55 : _d{t.time_since_epch()}
56 {
57 }
58
61 [[nodiscard]] constexpr auto time_since_epoch() const noexcept -> duration { return _d; }
62
67 constexpr auto operator+=(duration const& d) noexcept -> time_point&
68 {
69 _d += d;
70 return *this;
71 }
72
76 constexpr auto operator-=(duration const& d) noexcept -> time_point&
77 {
78 _d -= d;
79 return *this;
80 }
81
84 constexpr auto operator++() noexcept -> time_point&
85 {
86 ++_d;
87 return *this;
88 }
89
92 constexpr auto operator++(int) noexcept -> time_point { return time_point(_d++); }
93
96 constexpr auto operator--() noexcept -> time_point&
97 {
98 --_d;
99 return *this;
100 }
101
104 constexpr auto operator--(int) noexcept -> time_point { return time_point(_d--); }
105
107 [[nodiscard]] static constexpr auto min() noexcept -> time_point { return time_point(duration::min()); }
108
110 [[nodiscard]] static constexpr auto max() noexcept -> time_point { return time_point(duration::max()); }
111
112private:
113 duration _d{};
114};
115
118template <typename Clock, typename Dur1, typename Dur2>
119[[nodiscard]] constexpr auto operator==(time_point<Clock, Dur1> const& lhs, time_point<Clock, Dur2> const& rhs) noexcept
120 -> bool
121{
122 return lhs.time_since_epoch() == rhs.time_since_epoch();
123}
124
127template <typename Clock, typename Dur1, typename Dur2>
128[[nodiscard]] constexpr auto operator<(time_point<Clock, Dur1> const& lhs, time_point<Clock, Dur2> const& rhs) noexcept
129 -> bool
130{
131 return lhs.time_since_epoch() < rhs.time_since_epoch();
132}
133
136template <typename Clock, typename Dur1, typename Dur2>
137[[nodiscard]] constexpr auto operator<=(time_point<Clock, Dur1> const& lhs, time_point<Clock, Dur2> const& rhs) noexcept
138 -> bool
139{
140 return lhs.time_since_epoch() <= rhs.time_since_epoch();
141}
142
145template <typename Clock, typename Dur1, typename Dur2>
146[[nodiscard]] constexpr auto operator>(time_point<Clock, Dur1> const& lhs, time_point<Clock, Dur2> const& rhs) noexcept
147 -> bool
148{
149 return lhs.time_since_epoch() > rhs.time_since_epoch();
150}
151
154template <typename Clock, typename Dur1, typename Dur2>
155[[nodiscard]] constexpr auto operator>=(time_point<Clock, Dur1> const& lhs, time_point<Clock, Dur2> const& rhs) noexcept
156 -> bool
157{
158 return lhs.time_since_epoch() >= rhs.time_since_epoch();
159}
160
161} // namespace etl::chrono
162
163namespace etl {
164
167template <typename Clock, typename Duration1, typename Duration2>
168struct common_type<chrono::time_point<Clock, Duration1>, chrono::time_point<Clock, Duration2>> {
170};
171
172} // namespace etl
173
174#endif // TETL_CHRONO_TIME_POINT_HPP
Definition abs.hpp:11
constexpr auto operator<(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:316
constexpr auto operator>(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:333
constexpr auto operator==(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Checks if lhs and rhs are equal, i.e. the number of ticks for the type common...
Definition duration.hpp:299
constexpr auto operator<=(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:325
constexpr auto operator>=(duration< Rep1, Period1 > const &lhs, duration< Rep2, Period2 > const &rhs) -> bool
Compares two durations. Compares lhs to rhs, i.e. compares the number of ticks for the type common to...
Definition duration.hpp:341
Definition adjacent_find.hpp:8
constexpr bool is_convertible_v
Definition is_convertible.hpp:46
Class template etl::chrono::duration represents a time interval.
Definition duration.hpp:31
static constexpr auto min() noexcept -> duration
Returns a duration with the lowest possible value.
Definition duration.hpp:106
typename Period::type period
A etl::ratio representing the tick period (i.e. the number of seconds per tick).
Definition duration.hpp:37
Rep rep
Rep, an arithmetic type representing the number of ticks.
Definition duration.hpp:33
static constexpr auto max() noexcept -> duration
Returns a duration with the largest possible value.
Definition duration.hpp:112
Class template time_point represents a point in time. It is implemented as if it stores a value of ty...
Definition time_point.hpp:21
constexpr time_point(time_point< clock, Dur2 > const &t)
Definition time_point.hpp:54
constexpr auto time_since_epoch() const noexcept -> duration
Definition time_point.hpp:61
static constexpr auto max() noexcept -> time_point
Definition time_point.hpp:110
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
Definition time_point.hpp:107
constexpr auto operator++(int) noexcept -> time_point
Definition time_point.hpp:92
constexpr auto operator++() noexcept -> time_point &
Definition time_point.hpp:84
typename duration::period period
Definition time_point.hpp:34
constexpr auto operator+=(duration const &d) noexcept -> time_point &
Definition time_point.hpp:67
constexpr auto operator--() noexcept -> time_point &
Definition time_point.hpp:96
constexpr auto operator--(int) noexcept -> time_point
Definition time_point.hpp:104
constexpr auto operator-=(duration const &d) noexcept -> time_point &
Definition time_point.hpp:76
chrono::time_point< Clock, common_type_t< Duration1, Duration2 > > type
Definition time_point.hpp:169
Determines the common type among all types T..., that is the type all T... can be implicitly converte...
Definition common_type.hpp:18