VIPRA Documentation
Loading...
Searching...
No Matches
clock.hpp
1#pragma once
2
3#include <chrono>
4
5namespace VIPRA::Util {
6using nano = std::chrono::nanoseconds;
7using micro = std::chrono::microseconds;
8using milli = std::chrono::milliseconds;
9using seconds = std::chrono::seconds;
10
11template <typename time_t>
12[[nodiscard]] inline auto time_string(time_t time) -> std::string
13{
14 auto minutes = std::chrono::duration_cast<std::chrono::minutes>(time);
15 time -= minutes;
16 auto seconds = std::chrono::duration_cast<std::chrono::seconds>(time);
17 time -= seconds;
18 auto milliseconds = std::chrono::duration_cast<std::chrono::milliseconds>(time);
19 time -= milliseconds;
20 auto nanoseconds = std::chrono::duration_cast<std::chrono::nanoseconds>(time);
21
22 std::string timeStr;
23
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";
28
29 if ( timeStr.empty() ) timeStr = "00";
30
31 return timeStr;
32}
33
40template <typename time_t = seconds, typename clock_t = std::chrono::steady_clock>
41class Clock {
42 public:
48 [[nodiscard]] inline auto now() noexcept -> time_t { return clock_t::now(); }
49
54 inline void start() noexcept
55 {
56 _running = true;
57 _runningTime = time_t::zero();
58 _start = clock_t::now();
59 }
60
61 [[nodiscard]] inline auto is_running() const noexcept -> bool { return _running; }
62
67 void pause() noexcept
68 {
69 if ( ! _running ) return;
70
71 _runningTime += std::chrono::duration_cast<time_t>(clock_t::now() - _start);
72 _running = false;
73 }
74
79 void resume() noexcept
80 {
81 if ( _running ) return;
82
83 _running = true;
84 _start = clock_t::now();
85 }
86
92 [[nodiscard]] inline auto running_time() noexcept -> time_t
93 {
94 if ( ! _running ) return _runningTime;
95
96 return std::chrono::duration_cast<time_t>(clock_t::now() - _start) + _runningTime;
97 }
98
104 inline auto stop() noexcept -> time_t
105 {
106 time_t duration;
107 if ( _running ) {
108 duration =
109 std::chrono::duration_cast<time_t>(clock_t::now() - _start) + _runningTime;
110 }
111 else {
112 duration = _runningTime;
113 }
114
115 _running = false;
116 _runningTime = time_t::zero();
117
118 return duration;
119 }
120
121 static constexpr time_t ZERO = time_t::zero();
122
123 private:
124 bool _running = false;
125 std::chrono::time_point<clock_t> _start;
126 time_t _runningTime;
127};
128} // namespace VIPRA::Util
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