forked from matth-x/MicroOcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTime.cpp
More file actions
382 lines (313 loc) · 10.9 KB
/
Time.cpp
File metadata and controls
382 lines (313 loc) · 10.9 KB
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
// matth-x/MicroOcpp
// Copyright Matthias Akstaller 2019 - 2024
// MIT License
#include <MicroOcpp/Core/Time.h>
#include <string.h>
#include <ctype.h>
namespace MicroOcpp {
const Timestamp MIN_TIME = Timestamp(2010, 0, 0, 0, 0, 0);
const Timestamp MAX_TIME = Timestamp(2037, 0, 0, 0, 0, 0);
Timestamp::Timestamp() : MemoryManaged("Timestamp") {
}
Timestamp::Timestamp(const Timestamp& other) : MemoryManaged("Timestamp") {
*this = other;
}
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
Timestamp::Timestamp(int16_t year, int16_t month, int16_t day, int32_t hour, int32_t minute, int32_t second, int32_t ms) :
MemoryManaged("Timestamp"), year(year), month(month), day(day), hour(hour), minute(minute), second(second), ms(ms) { }
#else
Timestamp::Timestamp(int16_t year, int16_t month, int16_t day, int32_t hour, int32_t minute, int32_t second) :
MemoryManaged("Timestamp"), year(year), month(month), day(day), hour(hour), minute(minute), second(second) { }
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
int noDays(int month, int year) {
return (month == 0 || month == 2 || month == 4 || month == 6 || month == 7 || month == 9 || month == 11) ? 31 :
((month == 3 || month == 5 || month == 8 || month == 10) ? 30 :
((year % 4 == 0 && (year % 100 != 0 || year % 400 == 0)) ? 29 : 28));
}
bool Timestamp::setTime(const char *jsonDateString) {
const int JSONDATE_MINLENGTH = 19;
if (strlen(jsonDateString) < JSONDATE_MINLENGTH){
return false;
}
if (!isdigit(jsonDateString[0]) || //2
!isdigit(jsonDateString[1]) || //0
!isdigit(jsonDateString[2]) || //1
!isdigit(jsonDateString[3]) || //3
jsonDateString[4] != '-' || //-
!isdigit(jsonDateString[5]) || //0
!isdigit(jsonDateString[6]) || //2
jsonDateString[7] != '-' || //-
!isdigit(jsonDateString[8]) || //0
!isdigit(jsonDateString[9]) || //1
jsonDateString[10] != 'T' || //T
!isdigit(jsonDateString[11]) || //2
!isdigit(jsonDateString[12]) || //0
jsonDateString[13] != ':' || //:
!isdigit(jsonDateString[14]) || //5
!isdigit(jsonDateString[15]) || //3
jsonDateString[16] != ':' || //:
!isdigit(jsonDateString[17]) || //3
!isdigit(jsonDateString[18])) { //2
//ignore subsequent characters
return false;
}
int year = (jsonDateString[0] - '0') * 1000 +
(jsonDateString[1] - '0') * 100 +
(jsonDateString[2] - '0') * 10 +
(jsonDateString[3] - '0');
int month = (jsonDateString[5] - '0') * 10 +
(jsonDateString[6] - '0') - 1;
int day = (jsonDateString[8] - '0') * 10 +
(jsonDateString[9] - '0') - 1;
int hour = (jsonDateString[11] - '0') * 10 +
(jsonDateString[12] - '0');
int minute = (jsonDateString[14] - '0') * 10 +
(jsonDateString[15] - '0');
int second = (jsonDateString[17] - '0') * 10 +
(jsonDateString[18] - '0');
//optional fractals
int ms = 0;
if (jsonDateString[19] == '.') {
if (isdigit(jsonDateString[20]) || //1
isdigit(jsonDateString[21]) || //2
isdigit(jsonDateString[22])) {
ms = (jsonDateString[20] - '0') * 100 +
(jsonDateString[21] - '0') * 10 +
(jsonDateString[22] - '0');
} else {
return false;
}
}
if (year < 1970 || year >= 2038 ||
month < 0 || month >= 12 ||
day < 0 || day >= noDays(month, year) ||
hour < 0 || hour >= 24 ||
minute < 0 || minute >= 60 ||
second < 0 || second > 60 || //tolerate leap seconds -- (23:59:60) can be a valid time
ms < 0 || ms >= 1000) {
return false;
}
this->year = year;
this->month = month;
this->day = day;
this->hour = hour;
this->minute = minute;
this->second = second;
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
this->ms = ms;
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
return true;
}
bool Timestamp::toJsonString(char *jsonDateString, size_t buffsize) const {
if (buffsize < JSONDATE_LENGTH + 1) return false;
jsonDateString[0] = ((char) ((year / 1000) % 10)) + '0';
jsonDateString[1] = ((char) ((year / 100) % 10)) + '0';
jsonDateString[2] = ((char) ((year / 10) % 10)) + '0';
jsonDateString[3] = ((char) ((year / 1) % 10)) + '0';
jsonDateString[4] = '-';
jsonDateString[5] = ((char) (((month + 1) / 10) % 10)) + '0';
jsonDateString[6] = ((char) (((month + 1) / 1) % 10)) + '0';
jsonDateString[7] = '-';
jsonDateString[8] = ((char) (((day + 1) / 10) % 10)) + '0';
jsonDateString[9] = ((char) (((day + 1) / 1) % 10)) + '0';
jsonDateString[10] = 'T';
jsonDateString[11] = ((char) ((hour / 10) % 10)) + '0';
jsonDateString[12] = ((char) ((hour / 1) % 10)) + '0';
jsonDateString[13] = ':';
jsonDateString[14] = ((char) ((minute / 10) % 10)) + '0';
jsonDateString[15] = ((char) ((minute / 1) % 10)) + '0';
jsonDateString[16] = ':';
jsonDateString[17] = ((char) ((second / 10) % 10)) + '0';
jsonDateString[18] = ((char) ((second / 1) % 10)) + '0';
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
jsonDateString[19] = '.';
jsonDateString[20] = ((char) ((ms / 100) % 10)) + '0';
jsonDateString[21] = ((char) ((ms / 10) % 10)) + '0';
jsonDateString[22] = ((char) ((ms / 1) % 10)) + '0';
jsonDateString[23] = 'Z';
jsonDateString[24] = '\0';
#else
jsonDateString[19] = 'Z';
jsonDateString[20] = '\0';
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
return true;
}
Timestamp &Timestamp::operator+=(int secs) {
second += secs;
if (second >= 0 && second < 60) return *this;
minute += second / 60;
second %= 60;
if (second < 0) {
minute--;
second += 60;
}
if (minute >= 0 && minute < 60) return *this;
hour += minute / 60;
minute %= 60;
if (minute < 0) {
hour--;
minute += 60;
}
if (hour >= 0 && hour < 24) return *this;
day += hour / 24;
hour %= 24;
if (hour < 0) {
day--;
hour += 24;
}
while (day >= noDays(month, year)) {
day -= noDays(month, year);
month++;
if (month >= 12) {
month -= 12;
year++;
}
}
while (day < 0) {
month--;
if (month < 0) {
month += 12;
year--;
}
day += noDays(month, year);
}
return *this;
}
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
Timestamp &Timestamp::addMilliseconds(int val) {
ms += val;
if (ms >= 0 && ms < 1000) return *this;
auto dsecond = ms / 1000;
ms %= 1000;
if (ms < 0) {
dsecond--;
ms += 1000;
}
return this->operator+=(dsecond);
}
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
Timestamp &Timestamp::operator-=(int secs) {
return operator+=(-secs);
}
int Timestamp::operator-(const Timestamp &rhs) const {
//dt = rhs - mocpp_base
int16_t year_base, year_end;
if (year <= rhs.year) {
year_base = year;
year_end = rhs.year;
} else {
year_base = rhs.year;
year_end = year;
}
int16_t lhsDays = day;
int16_t rhsDays = rhs.day;
for (int16_t iy = year_base; iy <= year_end; iy++) {
for (int16_t im = 0; im < 12; im++) {
if (year > iy || (year == iy && month > im)) {
lhsDays += noDays(im, iy);
}
if (rhs.year > iy || (rhs.year == iy && rhs.month > im)) {
rhsDays += noDays(im, iy);
}
}
}
int dt = (lhsDays - rhsDays) * (24 * 3600) + (hour - rhs.hour) * 3600 + (minute - rhs.minute) * 60 + second - rhs.second;
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
// Make it so that we round the difference to the nearest second, instead of being up to almost a whole second off
if ((ms - rhs.ms) > 500) dt++;
if ((ms - rhs.ms) < -500) dt--;
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
return dt;
}
Timestamp &Timestamp::operator=(const Timestamp &rhs) {
year = rhs.year;
month = rhs.month;
day = rhs.day;
hour = rhs.hour;
minute = rhs.minute;
second = rhs.second;
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
ms = rhs.ms;
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
return *this;
}
Timestamp operator+(const Timestamp &lhs, int secs) {
Timestamp res = lhs;
res += secs;
return res;
}
Timestamp operator-(const Timestamp &lhs, int secs) {
return operator+(lhs, -secs);
}
bool operator==(const Timestamp &lhs, const Timestamp &rhs) {
return lhs.year == rhs.year && lhs.month == rhs.month && lhs.day == rhs.day && lhs.hour == rhs.hour && lhs.minute == rhs.minute && lhs.second == rhs.second
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
&& lhs.ms == rhs.ms
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
;
}
bool operator!=(const Timestamp &lhs, const Timestamp &rhs) {
return !(lhs == rhs);
}
bool operator<(const Timestamp &lhs, const Timestamp &rhs) {
if (lhs.year != rhs.year)
return lhs.year < rhs.year;
if (lhs.month != rhs.month)
return lhs.month < rhs.month;
if (lhs.day != rhs.day)
return lhs.day < rhs.day;
if (lhs.hour != rhs.hour)
return lhs.hour < rhs.hour;
if (lhs.minute != rhs.minute)
return lhs.minute < rhs.minute;
if (lhs.second != rhs.second)
return lhs.second < rhs.second;
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
if (lhs.ms != rhs.ms)
return lhs.ms < rhs.ms;
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
return false;
}
bool operator<=(const Timestamp &lhs, const Timestamp &rhs) {
return lhs < rhs || lhs == rhs;
}
bool operator>(const Timestamp &lhs, const Timestamp &rhs) {
return rhs < lhs;
}
bool operator>=(const Timestamp &lhs, const Timestamp &rhs) {
return rhs <= lhs;
}
Clock::Clock() {
}
bool Clock::setTime(const char* jsonDateString) {
Timestamp timestamp = Timestamp();
if (!timestamp.setTime(jsonDateString)) {
return false;
}
system_basetime = mocpp_tick_ms();
mocpp_basetime = timestamp;
currentTime = mocpp_basetime;
lastUpdate = system_basetime;
return true;
}
const Timestamp &Clock::now() {
auto tReading = mocpp_tick_ms();
auto delta = tReading - lastUpdate;
#if MO_ENABLE_TIMESTAMP_MILLISECONDS
currentTime.addMilliseconds(delta);
lastUpdate = tReading;
#else
auto deltaSecs = delta / 1000;
currentTime += deltaSecs;
lastUpdate += deltaSecs * 1000;
#endif //MO_ENABLE_TIMESTAMP_MILLISECONDS
return currentTime;
}
Timestamp Clock::adjustPrebootTimestamp(const Timestamp& t) {
auto systemtime_in = t - Timestamp();
if (systemtime_in > (int) system_basetime / 1000) {
return mocpp_basetime;
}
return mocpp_basetime - ((int) (system_basetime / 1000) - systemtime_in);
}
}