⏱️ golxzn::os::chrono ⏱️ 1.2.4
Chrono for golxzn's projects.
Loading...
Searching...
No Matches
time.hpp
Go to the documentation of this file.
1/**
2 * @file golxzn/os/chrono/time.hpp
3 * @author Ruslan Golovinskii (golxzn@gmail.com)
4 * @brief Class that represents time.
5 * @date 2023-10-24
6 *
7 * @copyright Copyright (c) 2023
8 */
9
10#pragma once
11
12#include <chrono>
13#include <type_traits>
14#include <golxzn/os/aliases.hpp>
15
17
18namespace golxzn::os::chrono {
19
20/**
21 * @brief Class that represents time.
22 * @ingroup Chrono time
23 * @details It's a wrapper around std::chrono::microseconds.
24 * This time representation is based on std::chrono::microseconds and is used in golxzn::os::chrono.
25 * @see golxzn::os::chrono::clock
26 * @see golxzn::os::chrono::fast_clock
27 * @see golxzn::os::chrono::timer
28 * @see golxzn::os::chrono::fast_timer
29 */
30class time {
31public:
32 constexpr time() = default;
33 constexpr time(const time &) = default;
34 constexpr time(time &&) noexcept = default;
35 constexpr time &operator=(const time &) = default;
36 constexpr time &operator=(time &&) noexcept = default;
37
38 /**
39 * @brief Constructor that creates time from std::chrono::microseconds.
40 * @ingroup Chrono time construction
41 * @param duration - duration in microseconds.
42 * @see time(const std::chrono::duration<Rep, Period> duration)
43 * @see time(const std::chrono::time_point<Clock, Duration> time_point)
44 * @see time(const utils::floating_point_t<T> seconds)
45 * @see seconds(const utils::floating_point_t<T> value)
46 * @see milliseconds(const i32 value)
47 * @see microseconds(const i64 value)
48 */
49 explicit constexpr time(const std::chrono::microseconds duration) noexcept;
50
51 /**
52 * @brief Constructor that creates time from std::chrono::duration.
53 * @ingroup Chrono time construction
54 * @tparam Rep Duration rep
55 * @tparam Period Duration period
56 * @see time(const std::chrono::microseconds duration)
57 * @see time(const std::chrono::time_point<Clock, Duration> time_point)
58 * @see time(const utils::floating_point_t<T> seconds)
59 * @see seconds(const utils::floating_point_t<T> value)
60 * @see milliseconds(const i32 value)
61 * @see microseconds(const i64 value)
62 */
63 template<class Rep, class Period>
64 explicit constexpr time(const std::chrono::duration<Rep, Period> duration) noexcept;
65
66 /**
67 * @brief Constructor that creates time from std::chrono::time_point.
68 * @ingroup Chrono time construction
69 * @tparam Clock time_point clock
70 * @tparam Clock::duration time_point duration
71 * @see time(const std::chrono::microseconds duration)
72 * @see time(const std::chrono::duration<Rep, Period> duration)
73 * @see time(const utils::floating_point_t<T> seconds)
74 * @see seconds(const utils::floating_point_t<T> value)
75 * @see milliseconds(const i32 value)
76 * @see microseconds(const i64 value)
77 */
78 template<class Clock, class Duration = typename Clock::duration>
79 explicit constexpr time(const std::chrono::time_point<Clock, Duration> time_point) noexcept;
80
81 /**
82 * @brief Constructor that creates time from seconds floating point value
83 * @ingroup Chrono time construction
84 * @tparam T floating point type (e.g. float, double)
85 * @see time(const i32 milliseconds)
86 * @see time(const i64 microseconds)
87 * @see seconds(const utils::floating_point_t<T> value)
88 * @see milliseconds(const i32 value)
89 * @see microseconds(const i64 value)
90 */
91 template<class T>
92 explicit constexpr time(const utils::floating_point_t<T> seconds) noexcept;
93
94 /**
95 * @brief Constructor that creates time from milliseconds value
96 * @ingroup Chrono time construction
97 * @see time(const utils::floating_point_t<T> seconds)
98 * @see time(const i64 microseconds)
99 * @see seconds(const utils::floating_point_t<T> value)
100 * @see milliseconds(const i32 value)
101 * @see microseconds(const i64 value)
102 */
103 explicit constexpr time(const i32 milliseconds) noexcept;
104
105 /**
106 * @brief Constructor that creates time from microseconds value
107 * @ingroup Chrono time construction
108 * @see time(const utils::floating_point_t<T> seconds)
109 * @see time(const i32 milliseconds)
110 * @see seconds(const utils::floating_point_t<T> value)
111 * @see milliseconds(const i32 value)
112 * @see microseconds(const i64 value)
113 */
114 explicit constexpr time(const i64 microseconds) noexcept;
115
116 /**
117 * @brief Returns duration in seconds
118 * @ingroup Chrono time conversion
119 * @tparam T floating point type (e.g. float, double). Default is f64 aka double.
120 * @return Seconds as floating point type T
121 * @see milliseconds()
122 * @see microseconds()
123 */
124 template<class T = f64>
125 [[nodiscard]] constexpr utils::floating_point_t<T> seconds() const noexcept;
126
127 /**
128 * @brief Returns duration in milliseconds
129 * @ingroup Chrono time conversion
130 * @return Milliseconds as i32
131 * @see seconds()
132 * @see microseconds()
133 */
134 [[nodiscard]] constexpr i32 milliseconds() const noexcept;
135
136 /**
137 * @brief Returns duration in microseconds
138 * @ingroup Chrono time conversion
139 * @return Microseconds as i64
140 * @see seconds()
141 * @see milliseconds()
142 */
143 [[nodiscard]] constexpr i64 microseconds() const noexcept;
144
145 /**
146 * @brief Converts time to std::chrono::duration
147 * @ingroup Chrono time conversion
148 * @tparam Rep Duration rep
149 * @tparam Period Duration period
150 * @return std::chrono::duration
151 * @see operator std::chrono::duration<Rep, Period>()
152 */
153 [[nodiscard]] constexpr std::chrono::microseconds duration() const noexcept;
154
155 /**
156 * @brief Converts time to std::chrono::duration
157 * @ingroup Chrono time conversion
158 * @tparam Rep Duration rep
159 * @tparam Period Duration period
160 * @return std::chrono::duration<Rep, Period> with casted microseconds
161 * @see duration()
162 */
163 template<class Rep, class Period>
164 [[nodiscard]] explicit constexpr operator std::chrono::duration<Rep, Period>() const noexcept;
165
166 [[nodiscard]] constexpr time &operator+=(const time &rhs) noexcept;
167 template<class T>
168 [[nodiscard]] constexpr time &operator+=(const utils::floating_point_t<T> seconds) noexcept;
169 [[nodiscard]] constexpr time &operator+=(const i32 milliseconds) noexcept;
170 [[nodiscard]] constexpr time &operator+=(const i64 microseconds) noexcept;
171
172 [[nodiscard]] constexpr time &operator-=(const time &rhs) noexcept;
173 template<class T>
174 [[nodiscard]] constexpr time &operator-=(const utils::floating_point_t<T> seconds) noexcept;
175 [[nodiscard]] constexpr time &operator-=(const i32 milliseconds) noexcept;
176 [[nodiscard]] constexpr time &operator-=(const i64 microseconds) noexcept;
177
178
179 [[nodiscard]] constexpr bool operator==(const time &rhs) const noexcept;
180 [[nodiscard]] constexpr bool operator!=(const time &rhs) const noexcept;
181 [[nodiscard]] constexpr bool operator>=(const time &rhs) const noexcept;
182 [[nodiscard]] constexpr bool operator<=(const time &rhs) const noexcept;
183 [[nodiscard]] constexpr bool operator> (const time &rhs) const noexcept;
184 [[nodiscard]] constexpr bool operator< (const time &rhs) const noexcept;
185
186 /**
187 * @brief Zero time.
188 * @ingroup Chrono time constants
189 * @return Well, zero, I guess.
190 */
191 [[nodiscard]] static constexpr time zero() noexcept;
192
193 /**
194 * @brief Now time.
195 * @return Current time.
196 */
197 [[nodiscard]] static time now() noexcept;
198
199private:
200 std::chrono::microseconds m_microseconds{};
201};
202
203/**
204 * @brief Make time from floating point value (seconds).
205 * @ingroup Chrono time construction
206 * @tparam T floating point type
207 * @param value floating point value
208 * @return time
209 * @see milliseconds(const i32 value)
210 * @see microseconds(const i64 value)
211 */
212template<class T>
213[[nodiscard]] constexpr time seconds(const utils::floating_point_t<T> value) noexcept;
214
215/**
216 * @brief Make time from integer value (milliseconds).
217 * @ingroup Chrono time construction
218 * @param value Integer value (milliseconds)
219 * @return time
220 * @see seconds(const utils::floating_point_t<T> value)
221 * @see microseconds(const i64 value)
222 */
223[[nodiscard]] constexpr time milliseconds(const i32 value) noexcept;
224
225/**
226 * @brief Make time from integer value (microseconds).
227 * @ingroup Chrono time construction
228 * @param value Integer value (microseconds)
229 * @return time
230 * @see seconds(const utils::floating_point_t<T> value)
231 * @see milliseconds(const i32 value)
232 */
233[[nodiscard]] constexpr time microseconds(const i64 value) noexcept;
234
235#include "golxzn/os/chrono/impl/time.inl"
236
237} // namespace golxzn::os::chrono
Class that represents time.
Definition time.hpp:30
constexpr i32 milliseconds() const noexcept
Returns duration in milliseconds.
constexpr time(const i64 microseconds) noexcept
Constructor that creates time from microseconds value.
constexpr time(const utils::floating_point_t< T > seconds) noexcept
Constructor that creates time from seconds floating point value.
constexpr i64 microseconds() const noexcept
Returns duration in microseconds.
constexpr time(const std::chrono::time_point< Clock, Duration > time_point) noexcept
Constructor that creates time from std::chrono::time_point.
static time now() noexcept
Now time.
constexpr utils::floating_point_t< T > seconds() const noexcept
Returns duration in seconds.
static constexpr time zero() noexcept
Zero time.
constexpr time(const i32 milliseconds) noexcept
Constructor that creates time from milliseconds value.
constexpr time(const std::chrono::microseconds duration) noexcept
Constructor that creates time from std::chrono::microseconds.
constexpr std::chrono::microseconds duration() const noexcept
Converts time to std::chrono::duration.
constexpr time(const std::chrono::duration< Rep, Period > duration) noexcept
Constructor that creates time from std::chrono::duration.
constexpr time milliseconds(const i32 value) noexcept
Make time from integer value (milliseconds).
constexpr time seconds(const utils::floating_point_t< T > value) noexcept
Make time from floating point value (seconds).
constexpr time microseconds(const i64 value) noexcept
Make time from integer value (microseconds).
Useful functions and aliases for golxzn::os::chrono.