-
Notifications
You must be signed in to change notification settings - Fork 0
/
Time.h
187 lines (166 loc) · 2.86 KB
/
Time.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
/*
* Copyright (C) 2017-2019 Miloš Stojanović
*
* SPDX-License-Identifier: MIT
*/
#ifndef TIME_H
#define TIME_H
#include <cmath>
#include <ctime>
#include <iomanip>
#include <sstream>
#include <string>
using namespace std;
class Time {
private:
int m = 0;
public:
Time() = default;
Time(string time)
{
parseTime(time);
}
int getH() const
{
if (m < 0)
return -m / 60;
else
return m / 60;
}
int getM() const
{
if (m < 0)
return -m % 60;
else
return m % 60;
}
bool isNegative() const
{
return m < 0;
}
static bool isTime(string time)
{
return Time().parseTime(time);
}
void clearTime()
{
m = 0;
}
bool parseTime(string time_str)
{
if (time_str == "now") {
time_t raw_time;
struct tm *time_info;
time(&raw_time);
time_info = localtime(&raw_time);
m = 60 * time_info->tm_hour;
m += time_info->tm_min;
// Round to the nearest minute
if (time_info->tm_sec > 30)
m++;
} else if (time_str == "0") {
m = 0;
} else {
int first_number = 0;
char c = '\0';
istringstream ss(time_str);
if (ss.peek() != '.') {
ss >> first_number;
if (!ss || ss.eof())
return false;
}
switch (ss.get()) {
case ':':
case '/':
ss >> c;
if (!ss || c < '0' || c > '5')
return false;
m = (c - '0') * 10;
if (c != '0') {
ss >> c;
if (!ss || c < '0' || c > '9')
return false;
m += (c - '0');
} else {
ss >> c;
if (!ss.eof()) {
if (c < '0' || c > '9')
return false;
m += (c - '0');
}
}
// Check for a third character after : or /
ss.get();
if (ss)
return false;
m += 60 * first_number;
break;
case '.':
ss >> c;
if (!ss || c < '0' || c > '9')
return false;
m = (c - '0') * 10;
ss >> c;
if (!ss.eof()) {
if (c < '0' || c > '9')
return false;
m += c - '0';
}
// Check for a third character after .
ss.get();
if (ss)
return false;
m = round(m * 60 / 100.0);
m += 60 * first_number;
break;
case 'h':
// Check for trailing characters
ss.get();
if (ss)
return false;
m = 60 * first_number;
break;
case 'm':
// Check for trailing characters
ss.get();
if (ss)
return false;
m = first_number;
break;
default:
return false;
break;
}
}
return true;
}
Time operator+(const Time& time)
{
m += time.m;
return *this;
}
Time& operator+=(const Time& time)
{
*this = *this + time;
return *this;
}
Time operator-(const Time& time)
{
m -= time.m;
return *this;
}
Time& operator-=(const Time& time)
{
*this = *this - time;
return *this;
}
};
ostream& operator<<(ostream& os, const Time& time)
{
if (time.isNegative())
os << '-';
os << setfill('0') << setw(2) << time.getH() << ':'
<< setfill('0') << setw(2) << time.getM();
return os;
}
#endif