-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDTime.cpp
More file actions
76 lines (62 loc) · 1.87 KB
/
DTime.cpp
File metadata and controls
76 lines (62 loc) · 1.87 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
#include "DTime.h"
DTime::DTime(uint16_t Y, uint8_t M, uint8_t D, uint8_t h, uint8_t m, uint8_t s) {
_year = Y;
_month = M;
_day = D;
_hour = h;
_minute = m;
_second = s;
encode();
}
bool DTime::isLeapYear(uint16_t Y) {
return !((Y % 4) * (!(Y % 100) + (Y % 400)));
}
DTime DTime::setDate(uint16_t Y, uint8_t M, uint8_t D) {
_year = Y;
_month = M;
_day = D;
encode();
return *this;
}
DTime DTime::setTime(uint8_t h, uint8_t m, uint8_t s) {
_hour = h;
_minute = m;
_second = s;
encode();
return *this;
}
DTime DTime::setTimestamp(uint32_t t) {
_timestamp = t;
decode();
return *this;
}
DTime DTime::tick() {
_timestamp++;
decode();
return *this;
}
uint8_t DTime::wday(uint16_t Y, uint8_t M, uint8_t D) {
uint8_t n[12] = {31, 28 + isLeapYear(Y), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
Y = (D + (((Y - 1) * 365UL) + ((Y - 1) / 4) - ((Y - 1) / 100) + ((Y - 1) / 400))) % 7;
while (M > 1) Y += n[--M - 1];
return Y % 7;
}
void DTime::decode() {
uint32_t t = _timestamp;
_second = t % 60;
_minute = (t /= 60) % 60;
_hour = (t /= 60) % 24;
t /= 24;
for (_year = 1970; t >= (365 + isLeapYear(_year)); _year++) t -= (365 + isLeapYear(_year));
uint8_t n[12] = {31, 28 + isLeapYear(_year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (_month = 1; t >= n[_month - 1]; _month++) t -= n[_month - 1];
_day = t + 1;
_weekday = wday(_year, _month, _day);
}
void DTime::encode() {
_timestamp = ((_day - 1) * 86400UL) + (_hour * 3600UL) + (_minute * 60UL) + _second;
uint8_t n[12] = {31, 28 + isLeapYear(_year), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for (uint8_t M = _month; M > 1;) _timestamp += (n[--M - 1] * 86400UL);
for (uint16_t Y = _year; 1970 < Y; Y--) _timestamp += ((isLeapYear(Y - 1) + 365) * 86400UL);
_weekday = wday(_year, _month, _day);
}