4#ifndef GREM_CORE_TIME_HPP
5#define GREM_CORE_TIME_HPP
18template <
intmax_t Num,
intmax_t Den = 1>
19using Ratio = std::ratio<Num, Den>;
22using std::ratio_divide;
23using std::ratio_equal;
24using std::ratio_equal_v;
25using std::ratio_greater;
26using std::ratio_greater_equal;
27using std::ratio_greater_equal_v;
28using std::ratio_greater_v;
30using std::ratio_less_equal;
31using std::ratio_less_equal_v;
32using std::ratio_less_v;
33using std::ratio_multiply;
34using std::ratio_not_equal;
35using std::ratio_not_equal_v;
36using std::ratio_subtract;
38template <
typename Rep,
typename Period = Ratio<1, 1>>
41template <
typename Clock,
typename Duration =
typename Clock::duration>
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;
78template <
typename Timer,
typename DeltaTime>
79constexpr bool countdown(Timer& timer, DeltaTime deltaTime) {
81 if (timer <= Timer{}) {
105template <
typename Timer,
typename DeltaTime,
typename TargetTime>
106constexpr bool countdown(Timer& timer, DeltaTime deltaTime, TargetTime targetTime) {
108 if (timer <= targetTime) {
132template <
typename Timer,
typename DeltaTime,
typename TargetTime>
133constexpr bool countup(Timer& timer, DeltaTime deltaTime, TargetTime targetTime) {
135 if (timer >= targetTime) {
160template <
typename Timer,
typename DeltaTime,
typename Interval>
161[[nodiscard]]
constexpr size_t countdownLoop(Timer& timer, DeltaTime deltaTime, Interval interval) {
162 if (interval <= Interval{}) {
168 while (timer <= Timer{}) {
193template <
typename Timer,
typename DeltaTime,
typename Interval>
194[[nodiscard]]
constexpr size_t countupLoop(Timer& timer, DeltaTime deltaTime, Interval interval) {
195 if (interval <= Interval{}) {
201 while (timer >= interval) {
238template <
typename Timer,
typename DeltaTime,
typename Interval>
239[[nodiscard]]
constexpr size_t countdownLoop(Timer& timer, DeltaTime deltaTime, Interval interval,
bool active) {
275template <
typename Timer,
typename DeltaTime,
typename Interval>
276[[nodiscard]]
constexpr size_t countupLoop(Timer& timer, DeltaTime deltaTime, Interval interval,
bool active) {
284namespace time_literals {
286[[nodiscard]]
consteval Seconds operator""_seconds(
unsigned long long value) {
287 return Seconds{
static_cast<Seconds::rep
>(value)};
290[[nodiscard]]
consteval Seconds operator""_second(
unsigned long long value) {
292 throw Error{
"Syntax error: Expected \"1_second\". For values other than 1, use the plural form \"_seconds\" instead."};
294 return operator""_seconds(value);
297[[nodiscard]]
consteval Milliseconds operator""_milliseconds(
unsigned long long value) {
298 return Milliseconds{
static_cast<Milliseconds::rep
>(value)};
301[[nodiscard]]
consteval Milliseconds operator""_millisecond(
unsigned long long value) {
303 throw Error{
"Syntax error: Expected \"1_millisecond\". For values other than 1, use the plural form \"_milliseconds\" instead."};
305 return operator""_milliseconds(value);
308[[nodiscard]]
consteval Microseconds operator""_microseconds(
unsigned long long value) {
309 return Microseconds{
static_cast<Microseconds::rep
>(value)};
312[[nodiscard]]
consteval Microseconds operator""_microsecond(
unsigned long long value) {
314 throw Error{
"Syntax error: Expected \"1_microsecond\". For values other than 1, use the plural form \"_microseconds\" instead."};
316 return operator""_microseconds(value);
319[[nodiscard]]
consteval Nanoseconds operator""_nanoseconds(
unsigned long long value) {
320 return Nanoseconds{
static_cast<Nanoseconds::rep
>(value)};
323[[nodiscard]]
consteval Nanoseconds operator""_nanosecond(
unsigned long long value) {
325 throw Error{
"Syntax error: Expected \"1_nanosecond\". For values other than 1, use the plural form \"_nanoseconds\" instead."};
327 return operator""_nanoseconds(value);
330[[nodiscard]]
consteval Minutes operator""_minutes(
unsigned long long value) {
331 return Minutes{
static_cast<Minutes::rep
>(value)};
334[[nodiscard]]
consteval Minutes operator""_minute(
unsigned long long value) {
336 throw Error{
"Syntax error: Expected \"1_minute\". For values other than 1, use the plural form \"_minutes\" instead."};
338 return operator""_minutes(value);
341[[nodiscard]]
consteval Hours operator""_hours(
unsigned long long value) {
342 return Hours{
static_cast<Hours::rep
>(value)};
345[[nodiscard]]
consteval Hours operator""_hour(
unsigned long long value) {
347 throw Error{
"Syntax error: Expected \"1_hour\". For values other than 1, use the plural form \"_hours\" instead."};
349 return operator""_hours(value);
356template <
typename Rep,
typename Period>
372 if constexpr (ratio_equal_v<Period, Ratio<1, 1>>) {
374 }
else if constexpr (ratio_equal_v<Period,
Ratio<1, 1'000>>) {
376 }
else if constexpr (ratio_equal_v<Period,
Ratio<1, 1'000'000>>) {
378 }
else if constexpr (ratio_equal_v<Period,
Ratio<1, 1'000'000'000>>) {
380 }
else if constexpr (ratio_equal_v<Period, Ratio<60, 1>>) {
382 }
else if constexpr (ratio_equal_v<Period, Ratio<60 * 60, 1>>) {
385 formatTo(output,
"({}/{})s", Period::num, Period::den);
Base exception type for runtime errors.
Definition Error.hpp:38
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