-
Notifications
You must be signed in to change notification settings - Fork 1
/
datetime.h
144 lines (123 loc) · 3.08 KB
/
datetime.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
/*\
|*| datetime.h
|*|
|*| (c) 2022 Trent M. Wyatt.
|*| companion file for Reverse Geocache Box project
|*|
\*/
#if !defined(DATETIME_H)
#define DATETIME_H
#include <EEPROM.h>
struct datetime_t {
private:
uint16_t
year : 12, // 38b 5B for year (0-4095)
month : 4, // 26b 4B for month (1-12)
day : 5, // 22b 3B for day (1-31)
hours : 5, // 17b 3B for hours (0-23)
minutes : 6, // 12b 2B for minutes (0-59)
seconds : 6; // 6b 1B for seconds (0-59)
public:
datetime_t() {
// Initialize all fields to 0
year = 0;
month = 0;
day = 0;
hours = 0;
minutes = 0;
seconds = 0;
}
bool operator == (datetime_t const &r) {
return r.year == year &&
r.month == month &&
r.day == day &&
r.hours == hours &&
r.minutes == minutes &&
r.seconds == seconds;
}
bool operator != (datetime_t const &r) {
return r.year != year ||
r.month != month ||
r.day != day ||
r.hours != hours ||
r.minutes != minutes ||
r.seconds != seconds;
}
datetime_t(uint16_t y, uint16_t m, uint16_t d, uint16_t hr, uint16_t min, uint16_t sec) {
year = y;
month = m;
day = d;
hours = hr;
minutes = min;
seconds = sec;
}
// Set the seconds field
void set_seconds(unsigned int seconds) {
if (seconds < 60) {
seconds = seconds;
}
}
// Get the seconds field
unsigned int get_seconds() const {
return seconds;
}
// Set the minutes field
void set_minutes(unsigned int minutes) {
if (minutes < 60) {
minutes = minutes;
}
}
// Get the minutes field
unsigned int get_minutes() const {
return minutes;
}
// Set the hours field
void set_hours(unsigned int hours) {
if (hours < 24) {
hours = hours;
}
}
// Get the hours field
unsigned int get_hours() const {
return hours;
}
// Set the day field
void set_day(unsigned int day) {
if (day > 0 && day < 32) {
day = day;
}
}
// Get the day field
unsigned int get_day() const {
return day;
}
// Set the month field
void set_month(unsigned int month) {
if (month > 0 && month < 13) {
month = month;
}
}
// Get the month field
unsigned int get_month() const {
return month;
}
// Set the year field
void set_year(unsigned int year) {
if (year < 4096) {
year = year;
}
}
// Get the year field
unsigned int get_year() const {
return year;
}
// Convert a time of day to seconds
static unsigned int to_seconds(
unsigned int hr,
unsigned int min,
unsigned int sec)
{
return (hr * 60 * 60) + (min * 60) + sec;
}
};
#endif // DATETIME_H