-
Notifications
You must be signed in to change notification settings - Fork 0
/
time.cpp
58 lines (51 loc) · 1.52 KB
/
time.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
struct Time {
int hour;
int minute;
int second = 0;
bool usingSeconds = 0;
bool valid() { return hour < 24 && minute < 60 && second < 60; }
int timeInMinutes() { return hour * 60 + minute; }
int timeInSeconds() { return hour * 3600 + minute * 60 + second; }
void printRaw() {
cout << (hour < 10 ? "0" : "") << hour << ":"
<< (minute < 10 ? "0" : "") << minute;
if (usingSeconds)
cout << ":" << (second < 10 ? "0" : "") << second;
cout << endl;
}
Time(string timeRaw) {
vector<int> res;
for (int i = 0; i < timeRaw.size(); i += 3) {
int v = (timeRaw[i] - '0') * 10 + (timeRaw[i + 1] - '0');
res.push_back(v);
}
this -> hour = res[0];
this -> minute = res[1];
if (res.size() == 3) {
this -> second = res[2];
this -> usingSeconds = 1;
}
}
int operator-(Time other) {
if (other.second == 0 && this -> second == 0)
return abs(other.timeInMinutes() - this -> timeInMinutes());
else
return abs(other.timeInSeconds() - this -> timeInSeconds());
}
void operator++(int32_t k) { k = 0; k++;
if (usingSeconds) {
second++;
if (second == 60)
second = 0;
else
return;
}
minute++;
if (minute == 60) {
minute = 0;
hour++;
if (hour == 24)
hour = 0;
}
}
};