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