6using nano = std::chrono::nanoseconds;
7using micro = std::chrono::microseconds;
8using milli = std::chrono::milliseconds;
9using seconds = std::chrono::seconds;
11template <
typename time_t>
12[[nodiscard]]
inline auto time_string(time_t time) -> std::string
14 auto minutes = std::chrono::duration_cast<std::chrono::minutes>(time);
16 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(time);
18 auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(time);
20 auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(time);
24 if ( minutes.count() > 0 ) timeStr += std::to_string(minutes.count()) +=
'm';
25 if ( seconds.count() > 0 ) timeStr += std::to_string(seconds.count()) +=
's';
26 if ( milliseconds.count() > 0 ) timeStr += std::to_string(milliseconds.count()) +=
"ms";
27 if ( nanoseconds.count() > 0 ) timeStr += std::to_string(nanoseconds.count()) +=
"ns";
29 if ( timeStr.empty() ) timeStr =
"00";
40template <
typename time_t = seconds,
typename clock_t = std::chrono::steady_clock>
48 [[nodiscard]]
inline auto now() noexcept -> time_t {
return clock_t::now(); }
57 _runningTime = time_t::zero();
58 _start = clock_t::now();
61 [[nodiscard]]
inline auto is_running() const noexcept ->
bool {
return _running; }
69 if ( ! _running )
return;
71 _runningTime += std::chrono::duration_cast<time_t>(clock_t::now() - _start);
81 if ( _running )
return;
84 _start = clock_t::now();
94 if ( ! _running )
return _runningTime;
96 return std::chrono::duration_cast<time_t>(clock_t::now() - _start) + _runningTime;
104 inline auto stop() noexcept -> time_t
109 std::chrono::duration_cast<time_t>(clock_t::now() - _start) + _runningTime;
112 duration = _runningTime;
116 _runningTime = time_t::zero();
121 static constexpr time_t ZERO = time_t::zero();
124 bool _running =
false;
125 std::chrono::time_point<clock_t> _start;
Clock to time execution time.
Definition clock.hpp:41
void pause() noexcept
Pauses the timer on the clock.
Definition clock.hpp:67
auto now() noexcept -> time_t
Returns the current time.
Definition clock.hpp:48
auto stop() noexcept -> time_t
Resets the clock, Returns the time since it was started.
Definition clock.hpp:104
void resume() noexcept
Continues the timer after pausing.
Definition clock.hpp:79
void start() noexcept
Starts the timer.
Definition clock.hpp:54
auto running_time() noexcept -> time_t
Returns the total accumulated time on the clock.
Definition clock.hpp:92