-
Notifications
You must be signed in to change notification settings - Fork 15
/
Timer.h
117 lines (104 loc) · 3 KB
/
Timer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#pragma once
#include <chrono>
#include <string>
#ifdef _WIN32
#define localtime _localtime64
#endif
class Timer
{
private:
std::chrono::time_point<std::chrono::system_clock> t0_, t1_, t_last_;
bool running_ = false;
public:
Timer() { start(); }
// Returns current time as a string
static std::string getNowAsString(const std::string& format = "%F %a %H:%M:%S")
{
//#ifdef __cpp_lib_format
// auto t = std::chrono::time_point_cast<std::chrono::seconds>(std::chrono::system_clock::now());
// auto t1 = std::chrono::current_zone()->to_local(t);
// auto fmt = "{:" + format + "}";
// return std::vformat(fmt, std::make_format_args(t1));
//#else
auto t = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
auto tm = *localtime(&t);
char buffer[64] = {};
strftime(buffer, sizeof(buffer), format.c_str(), &tm);
return std::string(buffer);
//#endif
}
void start()
{
running_ = true;
t0_ = std::chrono::system_clock::now();
t_last_ = t0_;
}
void stop()
{
running_ = false;
t1_ = std::chrono::system_clock::now();
}
double getElapsedTime(bool restart = false)
{
if (running_)
{
t1_ = std::chrono::system_clock::now();
}
auto s = std::chrono::duration_cast<std::chrono::duration<double>>(t1_ - t0_);
if (restart)
{
t0_ = std::chrono::system_clock::now();
}
return s.count();
}
double getLastPeriod()
{
return getElapsedTime(true);
}
static std::string autoFormatTime(double s)
{
const int size = 80;
char buffer[size];
int h = s / 3600;
int m = (s - h * 3600) / 60;
s = s - h * 3600 - m * 60;
if (h > 0)
{
snprintf(buffer, size, "%d:%02d:%05.2f", h, m, s);
}
else if (m > 0)
{
snprintf(buffer, size, "%d:%05.2f", m, s);
}
else
{
snprintf(buffer, size, "%.2f s", s);
}
return std::string(buffer);
}
static std::string formatTime(double s, const std::string& format_str = "%d:%02d:%05.2f")
{
const int size = 80;
char buffer[size];
int h = s / 3600;
int m = (s - h * 3600) / 60;
s = s - h * 3600 - m * 60;
snprintf(buffer, size, format_str.c_str(), h, m, s);
return std::string(buffer);
}
static int64_t getNanoTime()
{
return std::chrono::system_clock::to_time_t(std::chrono::system_clock::now());
}
// return the time within two callings of getLastPeriod2, no restart
double getLastPeriod2()
{
if (running_)
{
t1_ = std::chrono::system_clock::now();
}
auto s = std::chrono::duration_cast<std::chrono::duration<double>>(t1_ - t_last_);
t_last_ = t1_;
return s.count();
}
};