-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathDTime.h
More file actions
46 lines (37 loc) · 1.65 KB
/
DTime.h
File metadata and controls
46 lines (37 loc) · 1.65 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
#ifndef DTIME_H
#define DTIME_H
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
#define DTIME_DAYS_UNTIL(y) ({((((y) - 1) * 365UL) + (((y) - 1) / 4) - (((y) - 1) / 100) + (((y) - 1) / 400));})
#define DTIME_DAYS_BETWEEN_YEARS(y1,y2) ({(DTIME_DAYS_UNTIL(((y1) > (y2)) ? (y1) : (y2)) - DTIME_DAYS_UNTIL(((y1) > (y2)) ? (y2) : (y1)));})
#define DTIME_LEAP_YEAR(y) ({!(((y) % 4) * (!((y) % 100) + ((y) % 400)));})
#define DTIME_MONTH_DAYS(y,m) ({(31 - (((m) > 2) ? ((((m) - 1) - (5 * ((m) > 7))) % 2) : (((m) - 1) * (3 - DTIME_LEAP_YEAR(y)))));})
#define DTIME_YEAR_DAYS_UNTIL(y,m) ({uint16_t d = 0; for(uint8_t i = 1; i < (m); i++) d += DTIME_MONTH_DAYS((y), i); d;})
#define DTIME_WEEKDAY(y,m,d) ({(DTIME_DAYS_UNTIL(y) + DTIME_YEAR_DAYS_UNTIL((y), (m)) + (d)) % 7;})
class DTime {
public:
const uint8_t &month = _month, &weekday = _weekday, &day = _day, &hour = _hour, &minute = _minute, &second = _second;
const uint16_t &year = _year;
const uint32_t ×tamp = _timestamp;
explicit DTime() {};
explicit DTime(uint32_t t): _timestamp(t) {
decode();
};
DTime(uint16_t Y, uint8_t M, uint8_t D, uint8_t h, uint8_t m, uint8_t s);
DTime setDate(uint16_t Y, uint8_t M, uint8_t D);
DTime setTime(uint8_t h, uint8_t m, uint8_t s);
DTime setTimestamp(uint32_t t);
DTime tick();
bool isLeapYear(uint16_t Y);
uint8_t wday(uint16_t Y, uint8_t M, uint8_t D);
private:
uint8_t _month = 1, _weekday = 4, _day = 1, _hour = 0, _minute = 0, _second = 0;
uint16_t _year = 1970;
uint32_t _timestamp = 0;
void decode();
void encode();
};
#endif