GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
time.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_TIME_HPP
5#define GREM_CORE_TIME_HPP
6
8
12
13#include <chrono> // std::chrono::...
14#include <ratio> // std::ratio
15
16namespace grem {
17
18template <intmax_t Num, intmax_t Den = 1>
19using Ratio = std::ratio<Num, Den>;
20
21using std::ratio_add; // NOLINT(misc-unused-alias-decls)
22using std::ratio_divide; // NOLINT(misc-unused-alias-decls)
23using std::ratio_equal; // NOLINT(misc-unused-alias-decls)
24using std::ratio_equal_v;
25using std::ratio_greater; // NOLINT(misc-unused-alias-decls)
26using std::ratio_greater_equal; // NOLINT(misc-unused-alias-decls)
27using std::ratio_greater_equal_v;
28using std::ratio_greater_v;
29using std::ratio_less; // NOLINT(misc-unused-alias-decls)
30using std::ratio_less_equal; // NOLINT(misc-unused-alias-decls)
31using std::ratio_less_equal_v;
32using std::ratio_less_v;
33using std::ratio_multiply; // NOLINT(misc-unused-alias-decls)
34using std::ratio_not_equal; // NOLINT(misc-unused-alias-decls)
35using std::ratio_not_equal_v;
36using std::ratio_subtract; // NOLINT(misc-unused-alias-decls)
37
38template <typename Rep, typename Period = Ratio<1, 1>>
39using DurationBase = std::chrono::duration<Rep, Period>;
40
41template <typename Clock, typename Duration = typename Clock::duration>
42using TimePointBase = std::chrono::time_point<Clock, Duration>;
43
44using std::chrono::abs;
45using std::chrono::ceil;
46using std::chrono::duration_cast;
47using std::chrono::floor;
48using std::chrono::round;
49using std::chrono::time_point_cast;
50
54using Milliseconds = DurationBase<int64_t, Ratio<1, 1'000>>;
55using Microseconds = DurationBase<int64_t, Ratio<1, 1'000'000>>;
56using Nanoseconds = DurationBase<int64_t, Ratio<1, 1'000'000'000>>;
57
61using FloatMilliseconds = DurationBase<float, Ratio<1, 1'000>>;
62using FloatMicroseconds = DurationBase<float, Ratio<1, 1'000'000>>;
63using FloatNanoseconds = DurationBase<float, Ratio<1, 1'000'000'000>>;
64
78template <typename Timer, typename DeltaTime>
79constexpr bool countdown(Timer& timer, DeltaTime deltaTime) {
80 timer -= deltaTime;
81 if (timer <= Timer{}) {
82 timer = Timer{};
83 return true;
84 }
85 return false;
86}
87
105template <typename Timer, typename DeltaTime, typename TargetTime>
106constexpr bool countdown(Timer& timer, DeltaTime deltaTime, TargetTime targetTime) {
107 timer -= deltaTime;
108 if (timer <= targetTime) {
109 timer = targetTime;
110 return true;
111 }
112 return false;
113}
114
132template <typename Timer, typename DeltaTime, typename TargetTime>
133constexpr bool countup(Timer& timer, DeltaTime deltaTime, TargetTime targetTime) {
134 timer += deltaTime;
135 if (timer >= targetTime) {
136 timer = targetTime;
137 return true;
138 }
139 return false;
140}
141
160template <typename Timer, typename DeltaTime, typename Interval>
161[[nodiscard]] constexpr size_t countdownLoop(Timer& timer, DeltaTime deltaTime, Interval interval) {
162 if (interval <= Interval{}) {
163 timer = Timer{};
164 return 1;
165 }
166 size_t ticks = 0;
167 timer -= deltaTime;
168 while (timer <= Timer{}) {
169 timer += interval;
170 ++ticks;
171 }
172 return ticks;
173}
174
193template <typename Timer, typename DeltaTime, typename Interval>
194[[nodiscard]] constexpr size_t countupLoop(Timer& timer, DeltaTime deltaTime, Interval interval) {
195 if (interval <= Interval{}) {
196 timer = Timer{};
197 return 1;
198 }
199 size_t ticks = 0;
200 timer += deltaTime;
201 while (timer >= interval) {
202 timer -= interval;
203 ++ticks;
204 }
205 return ticks;
206}
207
238template <typename Timer, typename DeltaTime, typename Interval>
239[[nodiscard]] constexpr size_t countdownLoop(Timer& timer, DeltaTime deltaTime, Interval interval, bool active) {
240 if (active) {
241 return countdownLoop(timer, deltaTime, interval);
242 }
243 countdown(timer, deltaTime);
244 return 0;
245}
246
275template <typename Timer, typename DeltaTime, typename Interval>
276[[nodiscard]] constexpr size_t countupLoop(Timer& timer, DeltaTime deltaTime, Interval interval, bool active) {
277 if (active) {
278 return countupLoop(timer, deltaTime, interval);
279 }
280 timer = Timer{};
281 return 0;
282}
283
284namespace time_literals {
285
286[[nodiscard]] consteval Seconds operator""_seconds(unsigned long long value) {
287 return Seconds{static_cast<Seconds::rep>(value)};
288}
289
290[[nodiscard]] consteval Seconds operator""_second(unsigned long long value) {
291 if (value != 1ull) {
292 throw Error{"Syntax error: Expected \"1_second\". For values other than 1, use the plural form \"_seconds\" instead."};
293 }
294 return operator""_seconds(value);
295}
296
297[[nodiscard]] consteval Milliseconds operator""_milliseconds(unsigned long long value) {
298 return Milliseconds{static_cast<Milliseconds::rep>(value)};
299}
300
301[[nodiscard]] consteval Milliseconds operator""_millisecond(unsigned long long value) {
302 if (value != 1ull) {
303 throw Error{"Syntax error: Expected \"1_millisecond\". For values other than 1, use the plural form \"_milliseconds\" instead."};
304 }
305 return operator""_milliseconds(value);
306}
307
308[[nodiscard]] consteval Microseconds operator""_microseconds(unsigned long long value) {
309 return Microseconds{static_cast<Microseconds::rep>(value)};
310}
311
312[[nodiscard]] consteval Microseconds operator""_microsecond(unsigned long long value) {
313 if (value != 1ull) {
314 throw Error{"Syntax error: Expected \"1_microsecond\". For values other than 1, use the plural form \"_microseconds\" instead."};
315 }
316 return operator""_microseconds(value);
317}
318
319[[nodiscard]] consteval Nanoseconds operator""_nanoseconds(unsigned long long value) {
320 return Nanoseconds{static_cast<Nanoseconds::rep>(value)};
321}
322
323[[nodiscard]] consteval Nanoseconds operator""_nanosecond(unsigned long long value) {
324 if (value != 1ull) {
325 throw Error{"Syntax error: Expected \"1_nanosecond\". For values other than 1, use the plural form \"_nanoseconds\" instead."};
326 }
327 return operator""_nanoseconds(value);
328}
329
330[[nodiscard]] consteval Minutes operator""_minutes(unsigned long long value) {
331 return Minutes{static_cast<Minutes::rep>(value)};
332}
333
334[[nodiscard]] consteval Minutes operator""_minute(unsigned long long value) {
335 if (value != 1ull) {
336 throw Error{"Syntax error: Expected \"1_minute\". For values other than 1, use the plural form \"_minutes\" instead."};
337 }
338 return operator""_minutes(value);
339}
340
341[[nodiscard]] consteval Hours operator""_hours(unsigned long long value) {
342 return Hours{static_cast<Hours::rep>(value)};
343}
344
345[[nodiscard]] consteval Hours operator""_hour(unsigned long long value) {
346 if (value != 1ull) {
347 throw Error{"Syntax error: Expected \"1_hour\". For values other than 1, use the plural form \"_hours\" instead."};
348 }
349 return operator""_hours(value);
350}
351
352}; // namespace time_literals
353
354} // namespace grem
355
356template <typename Rep, typename Period>
357struct grem::Formatter<grem::DurationBase<Rep, Period>> : Formatter<Rep> {
359
360 [[nodiscard]] constexpr const char* parseFormatSpecification(const char* p) {
362 if (*p == 'z') {
363 symbolSeparator = {};
364 ++p;
365 }
366 return p;
367 }
368
369 void formatTo(FormatOutput& output, const grem::DurationBase<Rep, Period>& value) const {
370 Formatter<Rep>::formatTo(output, value.count());
371 output.append(symbolSeparator);
372 if constexpr (ratio_equal_v<Period, Ratio<1, 1>>) {
373 output.append("s");
374 } else if constexpr (ratio_equal_v<Period, Ratio<1, 1'000>>) {
375 output.append("ms");
376 } else if constexpr (ratio_equal_v<Period, Ratio<1, 1'000'000>>) {
377 output.append("us");
378 } else if constexpr (ratio_equal_v<Period, Ratio<1, 1'000'000'000>>) {
379 output.append("ns");
380 } else if constexpr (ratio_equal_v<Period, Ratio<60, 1>>) {
381 output.append("min");
382 } else if constexpr (ratio_equal_v<Period, Ratio<60 * 60, 1>>) {
383 output.append("h");
384 } else {
385 formatTo(output, "({}/{})s", Period::num, Period::den);
386 }
387 }
388};
389
390#endif
Base exception type for runtime errors.
Definition Error.hpp:38
Definition formatting.hpp:41
virtual constexpr void append(StringView string)=0
Definition aliases.hpp:9
DurationBase< int64_t, Ratio< 1, 1 '000 '000 > > Microseconds
Definition time.hpp:55
DurationBase< float, Ratio< 60 *60, 1 > > FloatHours
Definition time.hpp:58
constexpr size_t countdownLoop(Timer &timer, DeltaTime deltaTime, Interval interval)
Subtract time from a time value and then check how many times it reached 0 while looping back to a gi...
Definition time.hpp:161
DurationBase< int64_t, Ratio< 1, 1 '000 '000 '000 > > Nanoseconds
Definition time.hpp:56
DurationBase< float, Ratio< 1, 1 '000 '000 > > FloatMicroseconds
Definition time.hpp:62
DurationBase< int64_t, Ratio< 60 *60, 1 > > Hours
Definition time.hpp:51
DurationBase< int64_t, Ratio< 1, 1 '000 > > Milliseconds
Definition time.hpp:54
constexpr bool countup(Timer &timer, DeltaTime deltaTime, TargetTime targetTime)
Add time to a time value and then check if it reached a given target time value.
Definition time.hpp:133
DurationBase< float, Ratio< 60, 1 > > FloatMinutes
Definition time.hpp:59
std::ratio< Num, Den > Ratio
Definition time.hpp:19
std::chrono::time_point< Clock, Duration > TimePointBase
Definition time.hpp:42
DurationBase< int64_t, Ratio< 60, 1 > > Minutes
Definition time.hpp:52
DurationBase< float, Ratio< 1, 1 > > FloatSeconds
Definition time.hpp:60
std::chrono::duration< Rep, Period > DurationBase
Definition time.hpp:39
DurationBase< int64_t, Ratio< 1, 1 > > Seconds
Definition time.hpp:53
DurationBase< float, Ratio< 1, 1 '000 > > FloatMilliseconds
Definition time.hpp:61
constexpr bool countdown(Timer &timer, DeltaTime deltaTime)
Subtract time from a time value and then check if it reached 0.
Definition time.hpp:79
StringViewBase< char, std::char_traits< char > > StringView
Definition StringView.hpp:16
DurationBase< float, Ratio< 1, 1 '000 '000 '000 > > FloatNanoseconds
Definition time.hpp:63
constexpr size_t countupLoop(Timer &timer, DeltaTime deltaTime, Interval interval)
Add time to a time value and then check how many times it reached a given time interval while looping...
Definition time.hpp:194
constexpr const char * parseFormatSpecification(const char *p)
Definition time.hpp:360
StringView symbolSeparator
Definition time.hpp:358
void formatTo(FormatOutput &output, const grem::DurationBase< Rep, Period > &value) const
Definition time.hpp:369
Definition formatting.hpp:47
constexpr const char * parseFormatSpecification(const auto &p)
Definition formatting.hpp:50
void formatTo(FormatOutput &output, const auto &value) const
Definition formatting.hpp:56