Skip to content

Commit 198c603

Browse files
committed
v2.0.0
Temporary fix 2K38 bug #10 #12
1 parent 2ff10b1 commit 198c603

8 files changed

Lines changed: 128 additions & 37 deletions

File tree

ESP32Time.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ ESP32Time::ESP32Time(){}
3636
@param offest
3737
gmt offset in seconds
3838
*/
39-
ESP32Time::ESP32Time(long offset){
39+
ESP32Time::ESP32Time(unsigned long offset){
4040
this->offset = offset;
4141
}
4242

@@ -88,9 +88,14 @@ void ESP32Time::setTimeStruct(tm t) {
8888
@param ms
8989
microseconds (optional)
9090
*/
91-
void ESP32Time::setTime(long epoch, int ms) {
91+
void ESP32Time::setTime(unsigned long epoch, int ms) {
9292
struct timeval tv;
93-
tv.tv_sec = epoch; // epoch time (seconds)
93+
if (epoch > 2082758399){
94+
this->overflow = true;
95+
tv.tv_sec = epoch - 2082758399; // epoch time (seconds)
96+
} else {
97+
tv.tv_sec = epoch; // epoch time (seconds)
98+
}
9499
tv.tv_usec = ms; // microseconds
95100
settimeofday(&tv, NULL);
96101
}
@@ -100,10 +105,19 @@ void ESP32Time::setTime(long epoch, int ms) {
100105
*/
101106
tm ESP32Time::getTimeStruct(){
102107
struct tm timeinfo;
103-
getLocalTime(&timeinfo);
108+
time_t now;
109+
time(&now);
110+
localtime_r(&now, &timeinfo);
104111
time_t tt = mktime (&timeinfo);
105-
tt = tt + offset;
106-
struct tm * tn = localtime(&tt);;
112+
113+
if (this->overflow){
114+
tt += 63071999;
115+
}
116+
tt += offset;
117+
struct tm * tn = localtime(&tt);
118+
if (this->overflow){
119+
tn->tm_year += 64;
120+
}
107121
return *tn;
108122
}
109123

@@ -193,38 +207,42 @@ String ESP32Time::getDate(bool mode){
193207
}
194208

195209
/*!
196-
@brief get the current milliseconds as long
210+
@brief get the current milliseconds as unsigned long
197211
*/
198-
long ESP32Time::getMillis(){
212+
unsigned long ESP32Time::getMillis(){
199213
struct timeval tv;
200214
gettimeofday(&tv, NULL);
201215
return tv.tv_usec/1000;
202216
}
203217

204218
/*!
205-
@brief get the current microseconds as long
219+
@brief get the current microseconds as unsigned long
206220
*/
207-
long ESP32Time::getMicros(){
221+
unsigned long ESP32Time::getMicros(){
208222
struct timeval tv;
209223
gettimeofday(&tv, NULL);
210224
return tv.tv_usec;
211225
}
212226

213227
/*!
214-
@brief get the current epoch seconds as long
228+
@brief get the current epoch seconds as unsigned long
215229
*/
216-
long ESP32Time::getEpoch(){
230+
unsigned long ESP32Time::getEpoch(){
217231
struct tm timeinfo = getTimeStruct();
218232
return mktime(&timeinfo);
219233
}
220234

221235
/*!
222-
@brief get the current epoch seconds as long from the rtc without offset
236+
@brief get the current epoch seconds as unsigned long from the rtc without offset
223237
*/
224-
long ESP32Time::getLocalEpoch(){
238+
unsigned long ESP32Time::getLocalEpoch(){
225239
struct timeval tv;
226240
gettimeofday(&tv, NULL);
227-
return tv.tv_sec;
241+
unsigned long epoch = tv.tv_sec;
242+
if (this->overflow){
243+
epoch += 63071999 + 2019686400;
244+
}
245+
return epoch;
228246
}
229247

230248
/*!

ESP32Time.h

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@ class ESP32Time {
3131

3232
public:
3333
ESP32Time();
34-
ESP32Time(long offset);
35-
void setTime(long epoch = 1609459200, int ms = 0); // default (1609459200) = 1st Jan 2021
34+
ESP32Time(unsigned long offset);
35+
void setTime(unsigned long epoch = 1609459200, int ms = 0); // default (1609459200) = 1st Jan 2021
3636
void setTime(int sc, int mn, int hr, int dy, int mt, int yr, int ms = 0);
3737
void setTimeStruct(tm t);
3838
tm getTimeStruct();
@@ -44,9 +44,9 @@ class ESP32Time {
4444
String getDate(bool mode = false);
4545
String getAmPm(bool lowercase = false);
4646

47-
long getEpoch();
48-
long getMillis();
49-
long getMicros();
47+
unsigned long getEpoch();
48+
unsigned long getMillis();
49+
unsigned long getMicros();
5050
int getSecond();
5151
int getMinute();
5252
int getHour(bool mode = false);
@@ -56,8 +56,11 @@ class ESP32Time {
5656
int getMonth();
5757
int getYear();
5858

59-
long offset;
60-
long getLocalEpoch();
59+
unsigned long offset;
60+
unsigned long getLocalEpoch();
61+
62+
private:
63+
bool overflow;
6164

6265

6366
};

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@ getDateTime(true) // (String) Sunday, January 17 2021 15:24:38
2020
getTimeDate() // (String) 15:24:38 Sun, Jan 17 2021
2121
getTimeDate(true) // (String) 15:24:38 Sunday, January 17 2021
2222
23-
getMicros() // (long) 723546
24-
getMillis() // (long) 723
25-
getEpoch() // (long) 1609459200
26-
getLocalEpoch() // (long) 1609459200 // local epoch without offset
23+
getMicros() // (unsigned long) 723546
24+
getMillis() // (unsigned long) 723
25+
getEpoch() // (unsigned long) 1609459200
26+
getLocalEpoch() // (unsigned long) 1609459200 // local epoch without offset
2727
getSecond() // (int) 38 (0-59)
2828
getMinute() // (int) 24 (0-59)
2929
getHour() // (int) 3 (0-12)
3030
getHour(true) // (int) 15 (0-23)
3131
getAmPm() // (String) pm
32-
getAmPm(true) // (String) PM
32+
getAmPm(false) // (String) PM
3333
getDay() // (int) 17 (1-31)
3434
getDayofWeek() // (int) 0 (0-6)
3535
getDayofYear() // (int) 16 (0-365)

examples/esp32_time/esp32_time.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ ESP32Time rtc(3600); // offset in seconds GMT+1
2929

3030
void setup() {
3131
Serial.begin(115200);
32-
rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
32+
rtc.setTime(30, 24, 15, 17, 1, 2042); // 17th Jan 2021 15:24:30
3333
//rtc.setTime(1609459200); // 1st Jan 2021 00:00:00
3434
//rtc.offset = 7200; // change offset value
3535

examples/esp32_time_multiple/esp32_time_multiple.ino

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ void loop() {
4747
// Serial.println(rtc.getTimeDate()); // (String) 15:24:38 Sun, Jan 17 2021
4848
// Serial.println(rtc.getTimeDate(true)); // (String) 15:24:38 Sunday, January 17 2021
4949
//
50-
// Serial.println(rtc.getMicros()); // (long) 723546
51-
// Serial.println(rtc.getMillis()); // (long) 723
52-
// Serial.println(rtc.getEpoch()); // (long) 1609459200
50+
// Serial.println(rtc.getMicros()); // (unsigned long) 723546
51+
// Serial.println(rtc.getMillis()); // (unsigned long) 723
52+
// Serial.println(rtc.getEpoch()); // (unsigned long) 1609459200
5353
// Serial.println(rtc.getSecond()); // (int) 38 (0-59)
5454
// Serial.println(rtc.getMinute()); // (int) 24 (0-59)
5555
// Serial.println(rtc.getHour()); // (int) 3 (0-12)
@@ -68,10 +68,10 @@ void loop() {
6868

6969
// formating options http://www.cplusplus.com/reference/ctime/strftime/
7070

71-
Serial.println(rtc.getEpoch()); // (long)
72-
Serial.println(rtc1.getEpoch()); // (long)
73-
Serial.println(rtc2.getEpoch()); // (long)
71+
Serial.println(rtc.getEpoch()); // (unsigned long)
72+
Serial.println(rtc1.getEpoch()); // (unsigned long)
73+
Serial.println(rtc2.getEpoch()); // (unsigned long)
7474

75-
Serial.println(rtc.getLocalEpoch()); // (long) epoch without offset, same for all instances
75+
Serial.println(rtc.getLocalEpoch()); // (unsigned long) epoch without offset, same for all instances
7676
delay(1000);
7777
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2021 Felix Biego
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
#include <ESP32Time.h>
26+
27+
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
28+
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */
29+
30+
ESP32Time rtc;
31+
32+
33+
void wakeup_reason() {
34+
esp_sleep_wakeup_cause_t wakeup_reason;
35+
36+
wakeup_reason = esp_sleep_get_wakeup_cause();
37+
switch (wakeup_reason)
38+
{
39+
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
40+
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
41+
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
42+
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
43+
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
44+
default :
45+
Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason);
46+
rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
47+
//rtc.setTime(1609459200); // 1st Jan 2021 00:00:00
48+
//rtc.offset = 7200; // change offset value
49+
50+
break;
51+
}
52+
}
53+
54+
void setup() {
55+
Serial.begin(115200);
56+
57+
wakeup_reason();
58+
59+
Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
60+
61+
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);
62+
63+
Serial.println("Going to sleep now");
64+
Serial.flush();
65+
esp_deep_sleep_start();
66+
}
67+
68+
void loop() {
69+
70+
}

library.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ESP32Time",
3-
"version": "1.1.0",
3+
"version": "2.0.0",
44
"keywords": "Arduino, ESP32, Time, Internal RTC",
55
"description": "An Arduino library for setting and retrieving internal RTC time on ESP32 boards",
66
"repository":

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP32Time
2-
version=1.1.0
2+
version=2.0.0
33
author=fbiego
44
maintainer=fbiego
55
sentence=Set and retrieve internal RTC time on ESP32 boards.

0 commit comments

Comments
 (0)