-
Notifications
You must be signed in to change notification settings - Fork 1
/
timescale.cpp
87 lines (79 loc) · 2.45 KB
/
timescale.cpp
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
/*!
* \file timescale.cpp
* \author Simon Coakley
* \date 2012
* \copyright Copyright (c) 2012 University of Sheffield
* \brief Implementation of time scale
*/
#include <math.h>
#include "./timescale.h"
TimeScale::TimeScale() {
reset();
}
void TimeScale::calcTotalSeconds() {
totalseconds = second;
totalseconds += minute*60;
totalseconds += hour*60*60;
totalseconds += day*60*60*24;
if (millisecond > 0) lowestScale = 0;
else if (second > 0) lowestScale = 1;
else if (minute > 0) lowestScale = 2;
else if (hour > 0) lowestScale = 3;
else if (day > 0) lowestScale = 4;
}
void TimeScale::reset() {
lowestScale = 0;
millisecond = 0;
second = 0;
minute = 0;
hour = 0;
day = 0;
totalseconds = 0;
enabled = false;
displayInVisual = false;
timeString = "";
}
QString TimeScale::calcTimeScale(int iteration) {
double tsecs;
milliseconds = millisecond*iteration/1000.0;
milliseconds = modf(milliseconds, &tsecs);
int totalseconds2 = static_cast<int>(tsecs);
totalseconds2 += totalseconds*iteration;
QString ts;
size_t BufSize = 1000;
char buf[BufSize];
days = static_cast<int>(totalseconds2/86400);
hours = static_cast<int>((totalseconds2%86400)/3600);
minutes = static_cast<int>(((totalseconds2%86400)%3600)/60);
seconds = static_cast<int>(((totalseconds2%86400)%3600)%60);
if (days > 0 || lowestScale == 4) {
snprintf(buf, BufSize, "%d day", days);
ts.append(QString().fromAscii(buf));
if (days > 1)
ts.append("s");
else
ts.append(" ");
if (lowestScale < 4) ts.append(" ");
}
if (hours > 0 || lowestScale < 4) {
snprintf(buf, BufSize, "%02d hrs", hours);
ts.append(QString().fromAscii(buf));
// if(hours > 1) ts.append("s"); else ts.append(" ");
if (lowestScale < 3) ts.append(" ");
}
if (minutes > 0 || lowestScale < 3) {
snprintf(buf, BufSize, "%02d mins", minutes);
ts.append(QString().fromAscii(buf));
// if(minutes > 1) ts.append("s"); else ts.append(" ");
if (lowestScale < 2) ts.append(" ");
}
if (lowestScale == 1) {
snprintf(buf, BufSize, "%02d s", seconds);
ts.append(QString().fromAscii(buf));
} else if (lowestScale == 0) {
snprintf(buf, BufSize, "%02.3f s", seconds+milliseconds);
ts.append(QString().fromAscii(buf));
}
timeString = ts;
return ts;
}