8 #include <fmt/format.h>
22 template <
typename T,
typename Period = std::ratio<1>>
28 using Duration = std::chrono::duration<T, Period>;
33 constexpr
Time() noexcept = default;
41 : duration(duration) {}
48 constexpr
Time(std::chrono::seconds seconds) noexcept
49 : duration(duration_cast<Duration>(seconds)) {}
57 constexpr
Time(std::chrono::milliseconds milliseconds) noexcept
58 : duration(duration_cast<Duration>(milliseconds)) {}
66 constexpr
Time(std::chrono::microseconds microseconds) noexcept
67 : duration(duration_cast<Duration>(microseconds)) {}
75 constexpr
Time(std::chrono::nanoseconds nanoseconds) noexcept
76 : duration(duration_cast<Duration>(nanoseconds)) {}
83 constexpr
Time(std::chrono::minutes minutes) noexcept
84 : duration(duration_cast<Duration>(minutes)) {}
91 constexpr
Time(std::chrono::hours hours) noexcept
92 : duration(duration_cast<Duration>(hours)) {}
108 constexpr
operator T() const noexcept {
109 return duration.count();
122 return a.duration == b.duration;
135 return a.duration != b.duration;
148 return a.duration < b.duration;
161 return a.duration <= b.duration;
174 return a.duration > b.duration;
187 return a.duration >= b.duration;
198 duration += deltaTime;
210 duration -= deltaTime;
222 return Time{+a.duration};
233 return Time{-a.duration};
279 duration -= deltaTime;
280 if (duration <= targetTime) {
281 duration = targetTime;
304 duration += deltaTime;
305 if (duration >= targetTime) {
306 duration = targetTime;
331 if (interval <=
Time{}) {
335 std::size_t ticks = 0;
336 duration -= deltaTime;
337 while (duration <=
Time{}) {
338 duration += interval;
363 if (interval <=
Time{}) {
367 std::size_t ticks = 0;
368 duration += deltaTime;
369 while (duration >= interval) {
370 duration -= interval;
457 template <
typename T,
typename Period>
462 template <
typename T,
typename Period>
467 template <
typename T,
typename Period>
472 template <length_t L,
typename T,
typename Period>
474 return a *
static_cast<T
>(b);
477 template <length_t L,
typename T,
typename Period>
479 return static_cast<T
>(a) * b;
482 template <length_t L,
typename T,
typename Period>
484 return a /
static_cast<T
>(b);
487 template <length_t L,
typename T,
typename Period>
489 return static_cast<T
>(a) / b;
494 template <
typename T,
typename Period,
typename CharT>
495 struct fmt::formatter<
donut::Time<T, Period>, CharT> {
Time duration value wrapper.
Definition: Time.hpp:23
constexpr Time(std::chrono::hours hours) noexcept
Construct a time value from a number of hours.
Definition: Time.hpp:91
constexpr Time() noexcept=default
Construct a time value of 0.
constexpr friend Time operator+(Time a) noexcept
Get the additive identity of a time value.
Definition: Time.hpp:221
constexpr friend bool operator<(const Time &a, const Time &b) noexcept
Check if a time value is less than another time value.
Definition: Time.hpp:147
constexpr friend bool operator>(const Time &a, const Time &b) noexcept
Check if a time value is greater than another time value.
Definition: Time.hpp:173
constexpr friend Time operator-(Time a, Time b) noexcept
Subtract a time value from another.
Definition: Time.hpp:257
constexpr Time(std::chrono::milliseconds milliseconds) noexcept
Construct a time value from a number of milliseconds.
Definition: Time.hpp:57
constexpr bool countUp(Time deltaTime, Time targetTime) noexcept
Add time to this value and then check if it reached a given target time value.
Definition: Time.hpp:303
constexpr std::size_t countDownLoop(Time deltaTime, Time interval, bool active) noexcept
Update a countdown loop with a boolean trigger that determines whether the loop is active or not.
Definition: Time.hpp:408
constexpr friend bool operator!=(const Time &a, const Time &b) noexcept
Compare two time values for inequality.
Definition: Time.hpp:134
std::chrono::duration< T, Period > Duration
Underlying duration type.
Definition: Time.hpp:28
constexpr Time(std::chrono::minutes minutes) noexcept
Construct a time value from a number of minutes.
Definition: Time.hpp:83
constexpr bool countDown(Time deltaTime, Time targetTime=Duration{}) noexcept
Subtract time from this value and then check if it reached a given target time value.
Definition: Time.hpp:278
constexpr Time & operator-=(Time deltaTime) noexcept
Subtract time from this time value.
Definition: Time.hpp:209
constexpr friend Time operator+(Time a, Time b) noexcept
Add two time values.
Definition: Time.hpp:244
constexpr friend bool operator<=(const Time &a, const Time &b) noexcept
Check if a time value is less than or equal to another time value.
Definition: Time.hpp:160
constexpr friend bool operator>=(const Time &a, const Time &b) noexcept
Check if a time value is greater than or equal to another time value.
Definition: Time.hpp:186
constexpr friend bool operator==(const Time &a, const Time &b) noexcept
Compare two time values for equality.
Definition: Time.hpp:121
constexpr std::size_t countUpLoop(Time deltaTime, Time interval) noexcept
Add time to this value and then check how many times it reached a given time interval while looping b...
Definition: Time.hpp:362
constexpr std::size_t countDownLoop(Time deltaTime, Time interval) noexcept
Subtract time from this value and then check how many times it reached 0 while looping back to a give...
Definition: Time.hpp:330
constexpr std::size_t countUpLoop(Time deltaTime, Time interval, bool active) noexcept
Update a countup loop with a boolean trigger that determines whether the loop is active or not.
Definition: Time.hpp:445
constexpr Time & operator+=(Time deltaTime) noexcept
Add time to this time value.
Definition: Time.hpp:197
constexpr Time(std::chrono::seconds seconds) noexcept
Construct a time value from a number of seconds.
Definition: Time.hpp:48
constexpr Time(std::chrono::microseconds microseconds) noexcept
Construct a time value from a number of microseconds.
Definition: Time.hpp:66
constexpr friend Time operator-(Time a) noexcept
Get the additive inverse of a time value.
Definition: Time.hpp:232
constexpr Time(std::chrono::nanoseconds nanoseconds) noexcept
Construct a time value from a number of nanoseconds.
Definition: Time.hpp:75
Definition: Application.hpp:9
constexpr Time< T, Period > operator*(Time< T, Period > a, T b) noexcept
Definition: Time.hpp:458
constexpr Time< T, Period > operator/(Time< T, Period > a, T b) noexcept
Definition: Time.hpp:468