-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer.cpp
More file actions
45 lines (40 loc) · 1008 Bytes
/
timer.cpp
File metadata and controls
45 lines (40 loc) · 1008 Bytes
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
#include "timer.h"
float Timer::getTime() const {
high_resolution_clock::time_point t2=high_resolution_clock::now();
duration<float> time_span;
time_span=duration_cast<duration<float>>(t2 - t1);
return time_span.count();
};
string Timer::getDHMS() const {
float time = getTime();
return getDHMS(time);
};
string Timer::getDHMS(float secs) const {
int mins=0;
int hrs=0;
int days=0;
string str;
char tmp[4]; //extra char for null terminator
mins=floor(secs/60);
if (mins > 0) {
secs = secs - mins*60;
hrs = floor(mins/60);
if (hrs > 0) {
mins = mins - hrs*60;
days = floor(hrs/24);
if (days > 0) {
hrs = hrs - days*24;
str.append(to_string(days));
str.append("-");
}
//only print hrs if non-zero, but print mins:secs no matter what
str.append(to_string(hrs));
str.append(":");
}
}
sprintf(tmp,"%02d:",mins);
str.append(tmp);
sprintf(tmp,"%02d",(int) round(secs));
str.append(tmp);
return str;
}