-
Notifications
You must be signed in to change notification settings - Fork 37
/
ESP32Time.cpp
378 lines (345 loc) · 8.23 KB
/
ESP32Time.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
/*
MIT License
Copyright (c) 2021 Felix Biego
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
#include "ESP32Time.h"
#include "time.h"
#include <sys/time.h>
#ifdef RTC_DATA_ATTR
RTC_DATA_ATTR static bool overflow;
#else
static bool overflow;
#endif
/*!
@brief Constructor for ESP32Time
*/
ESP32Time::ESP32Time(){
}
/*!
@brief Constructor for ESP32Time
@param offest
gmt offset in seconds
*/
ESP32Time::ESP32Time(long offset){
this->offset = offset;
}
/*!
@brief set the internal RTC time
@param sc
second (0-59)
@param mn
minute (0-59)
@param hr
hour of day (0-23)
@param dy
day of month (1-31)
@param mt
month (1-12)
@param yr
year ie 2021
@param ms
microseconds (optional)
*/
void ESP32Time::setTime(int sc, int mn, int hr, int dy, int mt, int yr, int ms) const {
// seconds, minute, hour, day, month, year $ microseconds(optional)
// ie setTime(20, 34, 8, 1, 4, 2021) = 8:34:20 1/4/2021
struct tm t = {0, 0, 0, 0, 0, 0, 0, 0, 0}; // Initalize to all 0's
t.tm_year = yr - 1900; // This is year-1900, so 121 = 2021
t.tm_mon = mt - 1;
t.tm_mday = dy;
t.tm_hour = hr;
t.tm_min = mn;
t.tm_sec = sc;
time_t timeSinceEpoch = mktime(&t);
setTime(timeSinceEpoch, ms);
}
/*!
@brief set time from struct
@param tm
time struct
*/
void ESP32Time::setTimeStruct(tm t) const {
time_t timeSinceEpoch = mktime(&t);
setTime(timeSinceEpoch, 0);
}
/*!
@brief set the internal RTC time
@param epoch
epoch time in seconds
@param ms
microseconds (optional)
*/
void ESP32Time::setTime(unsigned long epoch, int ms) const {
struct timeval tv;
if (epoch > 2082758399){
overflow = true;
tv.tv_sec = epoch - 2082758399; // epoch time (seconds)
} else {
overflow = false;
tv.tv_sec = epoch; // epoch time (seconds)
}
tv.tv_usec = ms; // microseconds
settimeofday(&tv, NULL);
}
/*!
@brief get the internal RTC time as a tm struct
*/
tm ESP32Time::getTimeStruct() const {
struct tm timeinfo;
time_t now;
time(&now);
localtime_r(&now, &timeinfo);
time_t tt = mktime (&timeinfo);
if (overflow){
tt += 63071999;
}
if (offset > 0){
tt += (unsigned long) offset;
} else {
tt -= (unsigned long) (offset * -1);
}
struct tm * tn = localtime(&tt);
if (overflow){
tn->tm_year += 64;
}
return *tn;
}
/*!
@brief get the time and date as an Arduino String object
@param mode
true = Long date format
false = Short date format
*/
String ESP32Time::getDateTime(bool mode) const {
struct tm timeinfo = getTimeStruct();
char s[51];
if (mode)
{
strftime(s, 50, "%A, %B %d %Y %H:%M:%S", &timeinfo);
}
else
{
strftime(s, 50, "%a, %b %d %Y %H:%M:%S", &timeinfo);
}
return String(s);
}
/*!
@brief get the time and date as an Arduino String object
@param mode
true = Long date format
false = Short date format
*/
String ESP32Time::getTimeDate(bool mode) const {
struct tm timeinfo = getTimeStruct();
char s[51];
if (mode)
{
strftime(s, 50, "%H:%M:%S %A, %B %d %Y", &timeinfo);
}
else
{
strftime(s, 50, "%H:%M:%S %a, %b %d %Y", &timeinfo);
}
return String(s);
}
/*!
@brief get the time as an Arduino String object
*/
String ESP32Time::getTime() const {
struct tm timeinfo = getTimeStruct();
char s[51];
strftime(s, 50, "%H:%M:%S", &timeinfo);
return String(s);
}
/*!
@brief get the time as an Arduino String object with the specified format
@param format
time format
http://www.cplusplus.com/reference/ctime/strftime/
*/
String ESP32Time::getTime(String format) const {
struct tm timeinfo = getTimeStruct();
char s[128];
char c[128];
format.toCharArray(c, 127);
strftime(s, 127, c, &timeinfo);
return String(s);
}
/*!
@brief get the date as an Arduino String object
@param mode
true = Long date format
false = Short date format
*/
String ESP32Time::getDate(bool mode) const {
struct tm timeinfo = getTimeStruct();
char s[51];
if (mode)
{
strftime(s, 50, "%A, %B %d %Y", &timeinfo);
}
else
{
strftime(s, 50, "%a, %b %d %Y", &timeinfo);
}
return String(s);
}
/*!
@brief get the current milliseconds as unsigned long
*/
unsigned long ESP32Time::getMillis() const {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec/1000;
}
/*!
@brief get the current microseconds as unsigned long
*/
unsigned long ESP32Time::getMicros() const {
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec;
}
/*!
@brief get the current epoch seconds as unsigned long
*/
unsigned long ESP32Time::getEpoch() const {
struct tm timeinfo = getTimeStruct();
return mktime(&timeinfo);
}
/*!
@brief get the current epoch seconds as unsigned long from the rtc without offset
*/
unsigned long ESP32Time::getLocalEpoch() const {
struct timeval tv;
gettimeofday(&tv, NULL);
unsigned long epoch = tv.tv_sec;
if (overflow){
epoch += 63071999 + 2019686400;
}
return epoch;
}
/*!
@brief get the current seconds as int
*/
int ESP32Time::getSecond() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_sec;
}
/*!
@brief get the current minutes as int
*/
int ESP32Time::getMinute() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_min;
}
/*!
@brief get the current hour as int
@param mode
true = 24 hour mode (0-23)
false = 12 hour mode (1-12)
*/
int ESP32Time::getHour(bool mode) const {
struct tm timeinfo = getTimeStruct();
if (mode)
{
return timeinfo.tm_hour;
}
else
{
int hour = timeinfo.tm_hour;
if (hour > 12)
{
return timeinfo.tm_hour-12;
}
else if (hour == 0)
{
return 12; // 12 am
}
else
{
return timeinfo.tm_hour;
}
}
}
/*!
@brief return current hour am or pm
@param lowercase
true = lowercase
false = uppercase
*/
String ESP32Time::getAmPm(bool lowercase) const {
struct tm timeinfo = getTimeStruct();
if (timeinfo.tm_hour >= 12)
{
if (lowercase)
{
return "pm";
}
else
{
return "PM";
}
}
else
{
if (lowercase)
{
return "am";
}
else
{
return "AM";
}
}
}
/*!
@brief get the current day as int (1-31)
*/
int ESP32Time::getDay() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_mday;
}
/*!
@brief get the current day of week as int (0-6)
*/
int ESP32Time::getDayofWeek() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_wday;
}
/*!
@brief get the current day of year as int (0-365)
*/
int ESP32Time::getDayofYear() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_yday;
}
/*!
@brief get the current month as int (0-11)
*/
int ESP32Time::getMonth() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_mon;
}
/*!
@brief get the current year as int
*/
int ESP32Time::getYear() const {
struct tm timeinfo = getTimeStruct();
return timeinfo.tm_year+1900;
}