-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDateTime.cpp
391 lines (312 loc) · 10.7 KB
/
DateTime.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
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
383
384
385
386
387
388
389
390
391
//
// Lucky Resistor's Deluxe Data Logger
// ---------------------------------------------------------------------------
// (c)2015 by Lucky Resistor. See LICENSE for details.
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
#include "DateTime.h"
namespace lr {
namespace {
// The number of days per month.
static const uint8_t cDaysPerMonth[] PROGMEM = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// The number of seconds per day.
static const uint32_t cSecondsPerDay = 86400;
// The number of seconds per hour.
static const uint16_t cSecondsPerHour = 3600;
// The number of seconds per minute.
static const uint16_t cSecondsPerMinute = 60;
// The number of days for a regular year.
static const uint32_t cDaysPerNormalYear = 365;
// Various output formats for the sprintf function.
static const char cStringFormatISO[] PROGMEM = "%04d-%02d-%02dT%02d:%02d:%02d"; // yyyy-MM-ddThh:mm:ss
static const char cStringFormatLong[] PROGMEM = "%04d-%02d-%02d %02d:%02d:%02d"; // yyyy-MM-dd hh:mm:ss
static const char cStringFormatISODate[] PROGMEM = "%04d-%02d-%02d"; // yyyy-MM-dd
static const char cStringFormatISOBasicDate[] PROGMEM = "%04d%02d%02d"; // yyyyMMdd
static const char cStringFormatISOTime[] PROGMEM = "%02d:%02d:%02d"; // hh:mm:ss
static const char cStringFormatISOBasicTime[] PROGMEM = "%02d%02d%02d"; // hhmmss
static const char cStringFormatShortDate[] PROGMEM = "%02d.%02d."; // dd.MM.
static const char cStringFormatShortTime[] PROGMEM = "%02d:%02d"; // hh:mm
// Calculate the day of the week.
// Using the formula from: http://www.tondering.dk/claus/cal/chrweek.php
static uint8_t calculateDayOfWeek(int16_t year, int16_t month, int16_t day)
{
const int16_t a = ((14 - month) / 12);
const int16_t y = year - a;
const int16_t m = month + (12 * a) - 2;
const int16_t d = (day + y + (y/4) - (y/100) + (y/400) + ((31 * m)/12)) % 7;
return d;
}
static inline bool isLeapYear(uint16_t year)
{
return ((year&3) == 0 && year%100 != 0) || (year%400 == 0);
}
static inline uint8_t getMaxDayPerMonth(uint16_t year, uint8_t month)
{
if (month == 2 && isLeapYear(year)) {
return 29;
}
return pgm_read_byte(&cDaysPerMonth[month]);
}
static inline uint32_t getDaysForYear(uint16_t year)
{
if (isLeapYear(year)) {
return cDaysPerNormalYear + 1;
} else {
return cDaysPerNormalYear;
}
}
}
DateTime::DateTime()
: _year(2000), _month(1), _day(1), _hour(0), _minute(0), _second(0), _dayOfWeek(6)
{
}
DateTime::DateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second)
{
setDate(year, month, day);
setTime(hour, minute, second);
}
DateTime::DateTime(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint8_t dayOfWeek)
: _year(year), _month(month), _day(day), _hour(hour), _minute(minute), _second(second), _dayOfWeek(dayOfWeek)
{
}
DateTime::~DateTime()
{
}
bool DateTime::operator==(const DateTime &other) const
{
return _second != other._second &&
_minute != other._minute &&
_hour != other._hour &&
_day != other._day &&
_month != other._month &&
_year != other._year;
}
bool DateTime::operator!=(const DateTime &other) const
{
return !operator==(other);
}
bool DateTime::operator<(const DateTime &other) const
{
if (_year != other._year) {
return _year < other._year;
} else if (_month != other._month) {
return _month < other._month;
} else if (_day != other._day) {
return _day < other._day;
} else if (_hour != other._hour) {
return _hour < other._hour;
} else if (_minute != other._minute) {
return _minute < other._minute;
} else {
return _second < other._second;
}
}
bool DateTime::operator<=(const DateTime &other) const
{
return operator<(other) || operator==(other);
}
bool DateTime::operator>(const DateTime &other) const
{
if (_year != other._year) {
return _year > other._year;
} else if (_month != other._month) {
return _month > other._month;
} else if (_day != other._day) {
return _day > other._day;
} else if (_hour != other._hour) {
return _hour > other._hour;
} else if (_minute != other._minute) {
return _minute > other._minute;
} else {
return _second > other._second;
}
}
bool DateTime::operator>=(const DateTime &other) const
{
return operator>(other) || operator==(other);
}
void DateTime::setDate(uint16_t year, uint16_t month, uint16_t day)
{
// force all values into valid ranges.
if (year < 2000) {
_year = 2000;
} else if (year > 9999) {
_year = 9999;
} else {
_year = year;
}
if (month < 1) {
_month = 1;
} else if (month > 12) {
_month = 12;
} else {
_month = month;
}
const uint8_t maxDayPerMonth = getMaxDayPerMonth(_year, _month);
if (day < 1) {
_day = 1;
} else if (day > maxDayPerMonth) {
_day = maxDayPerMonth;
} else {
_day = day;
}
_dayOfWeek = calculateDayOfWeek(_year, _month, _day);
}
void DateTime::setTime(uint8_t hour, uint8_t minute, uint8_t second)
{
if (hour > 23) {
_hour = 23;
} else {
_hour = hour;
}
if (minute > 59) {
_minute = 59;
} else {
_minute = minute;
}
if (second > 59) {
_second = 59;
} else {
_second = second;
}
}
uint16_t DateTime::getYear() const
{
return _year;
}
uint8_t DateTime::getMonth() const
{
return _month;
}
uint8_t DateTime::getDay() const
{
return _day;
}
uint8_t DateTime::getDayOfWeek() const
{
return _dayOfWeek;
}
uint8_t DateTime::getHour() const
{
return _hour;
}
uint8_t DateTime::getMinute() const
{
return _minute;
}
uint8_t DateTime::getSecond() const
{
return _second;
}
DateTime DateTime::addSeconds(int32_t seconds) const
{
return fromSecondsSince2000(toSecondsSince2000() + seconds);
}
DateTime DateTime::addDays(int32_t days) const
{
return fromSecondsSince2000(toSecondsSince2000() + days*cSecondsPerDay);
}
int32_t DateTime::secondsTo(const DateTime &other) const
{
return static_cast<int32_t>(other.toSecondsSince2000()) - static_cast<int32_t>(toSecondsSince2000());
}
uint32_t DateTime::toSecondsSince2000() const
{
// This calculation will require some CPU cycles. It is an
// programmatic solution, no mathematical one.
uint32_t seconds = 0;
for (uint16_t year = 2000; year < _year; ++year) {
seconds += (getDaysForYear(year) * static_cast<uint32_t>(cSecondsPerDay));
}
for (uint8_t month = 1; month < _month; ++month) {
seconds += (static_cast<uint32_t>(getMaxDayPerMonth(_year, month)) * static_cast<uint32_t>(cSecondsPerDay));
}
seconds += static_cast<uint32_t>(_day-1) * static_cast<uint32_t>(cSecondsPerDay);
seconds += static_cast<uint32_t>(_hour) * static_cast<uint32_t>(cSecondsPerHour);
seconds += static_cast<uint32_t>(_minute) * static_cast<uint32_t>(cSecondsPerMinute);
seconds += static_cast<uint32_t>(_second);
return seconds;
}
bool DateTime::isFirst() const
{
return _year == 2000 && _month == 1 && _day == 1 && _hour == 0 && _minute == 0 && _second == 0;
}
String DateTime::toString(Format format) const
{
char buffer[20]; // longest format.
switch (format) {
case FormatISO:
sprintf_P(buffer, cStringFormatISO, _year, _month, _day, _hour, _minute, _second);
break;
case FormatLong:
sprintf_P(buffer, cStringFormatLong, _year, _month, _day, _hour, _minute, _second);
break;
case FormatISODate:
sprintf_P(buffer, cStringFormatISODate, _year, _month, _day);
break;
case FormatISOBasicDate:
sprintf_P(buffer, cStringFormatISOBasicDate, _year, _month, _day);
break;
case FormatISOTime:
sprintf_P(buffer, cStringFormatISOTime, _hour, _minute, _second);
break;
case FormatISOBasicTime:
sprintf_P(buffer, cStringFormatISOBasicTime, _hour, _minute, _second);
break;
case FormatShortDate:
sprintf_P(buffer, cStringFormatShortDate, _day, _month);
break;
case FormatShortTime:
sprintf_P(buffer, cStringFormatShortTime, _hour, _minute);
break;
}
return String(buffer);
}
DateTime DateTime::fromSecondsSince2000(uint32_t secondsSince2000)
{
// This calculation will require some CPU cycles. It is an
// programmatic solution, no mathematical one. This function
// is approximate 6 times slower than mathematical implementations.
// Calculate the time
uint32_t secondsSinceMidnight = secondsSince2000%cSecondsPerDay;
const uint8_t hours = secondsSinceMidnight/static_cast<uint32_t>(cSecondsPerHour);
secondsSinceMidnight %= static_cast<uint32_t>(cSecondsPerHour);
const uint8_t minutes = secondsSinceMidnight/static_cast<uint32_t>(cSecondsPerMinute);
const uint8_t seconds = secondsSinceMidnight % static_cast<uint32_t>(cSecondsPerMinute);
// Calculate the date
uint32_t days = secondsSince2000/static_cast<uint32_t>(cSecondsPerDay);
const uint8_t dayOfWeek = (days+6)%7; // 2000-01-01 was Saturday (6)
uint16_t year = 2000;
uint32_t daysForThisSection = getDaysForYear(year);
while (days >= daysForThisSection) {
++year;
days -= daysForThisSection;
daysForThisSection = getDaysForYear(year);
}
uint16_t month = 1;
daysForThisSection = getMaxDayPerMonth(year, month);
while (days >= daysForThisSection) {
++month;
days -= daysForThisSection;
daysForThisSection = getMaxDayPerMonth(year, month);
}
return DateTime(year, month, days+1, hours, minutes, seconds, dayOfWeek);
}
DateTime DateTime::fromUncheckedValues(uint16_t year, uint8_t month, uint8_t day, uint8_t hour, uint8_t minute, uint8_t second, uint8_t dayOfWeek)
{
return DateTime(year, month, day, hour, minute, second, dayOfWeek);
}
}