Antares Xpansion
Investment simulations for Antares studies
Loading...
Searching...
No Matches
Timer.h
1#pragma once
2
3#include <chrono>
4#include <sstream>
5#include <time.h>
6
7typedef std::chrono::time_point<std::chrono::system_clock> TimePoint;
8
9class Timer
10{
11public:
12 Timer();
13 explicit Timer(const double begin_time);
14 virtual ~Timer() = default;
15
16 double elapsed() const;
17 void restart();
18
19private:
20 TimePoint _start;
21 double _begin_time = 0;
22};
23
24inline Timer::Timer()
25{
26 restart();
27}
28
29inline Timer::Timer(const double begin_time):
30 _begin_time(begin_time)
31{
32 // _start
33 restart();
34}
35
36inline void Timer::restart()
37{
38 _start = std::chrono::system_clock::now();
39}
40
41inline double Timer::elapsed() const
42{
43 return std::chrono::duration<double>(std::chrono::system_clock::now() - _start).count()
44 + _begin_time;
45}
46
47inline std::string format_time_str(const long int time_in_seconds)
48{
49 std::time_t seconds(time_in_seconds); // you have to convert your input_seconds into time_t
50 std::tm p;
51
52 // convert to broken down time
53#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
54 gmtime_s(&p, &seconds);
55#else // defined(__unix__) || (__APPLE__)
56 gmtime_r(&seconds, &p);
57#endif
58 std::stringstream ss;
59 const auto days = p.tm_yday;
60 const auto hours = p.tm_hour;
61 const auto mins = p.tm_min;
62 const auto secs = p.tm_sec;
63 if (days > 0)
64 {
65 ss << days << " days ";
66 }
67 if (hours > 0)
68 {
69 ss << hours << " hours ";
70 }
71 if (mins > 0)
72 {
73 ss << mins << " minutes ";
74 }
75 if (secs > 0)
76 {
77 ss << secs << " seconds ";
78 }
79 return ss.str();
80}
Definition Timer.h:10