Skip to content

Commit

Permalink
v2.0.0
Browse files Browse the repository at this point in the history
Temporary fix 2K38 bug #10
#12
  • Loading branch information
fbiego committed Jul 29, 2022
1 parent 2ff10b1 commit 198c603
Show file tree
Hide file tree
Showing 8 changed files with 128 additions and 37 deletions.
48 changes: 33 additions & 15 deletions ESP32Time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ ESP32Time::ESP32Time(){}
@param offest
gmt offset in seconds
*/
ESP32Time::ESP32Time(long offset){
ESP32Time::ESP32Time(unsigned long offset){
this->offset = offset;
}

Expand Down Expand Up @@ -88,9 +88,14 @@ void ESP32Time::setTimeStruct(tm t) {
@param ms
microseconds (optional)
*/
void ESP32Time::setTime(long epoch, int ms) {
void ESP32Time::setTime(unsigned long epoch, int ms) {
struct timeval tv;
tv.tv_sec = epoch; // epoch time (seconds)
if (epoch > 2082758399){
this->overflow = true;
tv.tv_sec = epoch - 2082758399; // epoch time (seconds)
} else {
tv.tv_sec = epoch; // epoch time (seconds)
}
tv.tv_usec = ms; // microseconds
settimeofday(&tv, NULL);
}
Expand All @@ -100,10 +105,19 @@ void ESP32Time::setTime(long epoch, int ms) {
*/
tm ESP32Time::getTimeStruct(){
struct tm timeinfo;
getLocalTime(&timeinfo);
time_t now;
time(&now);
localtime_r(&now, &timeinfo);
time_t tt = mktime (&timeinfo);
tt = tt + offset;
struct tm * tn = localtime(&tt);;

if (this->overflow){
tt += 63071999;
}
tt += offset;
struct tm * tn = localtime(&tt);
if (this->overflow){
tn->tm_year += 64;
}
return *tn;
}

Expand Down Expand Up @@ -193,38 +207,42 @@ String ESP32Time::getDate(bool mode){
}

/*!
@brief get the current milliseconds as long
@brief get the current milliseconds as unsigned long
*/
long ESP32Time::getMillis(){
unsigned long ESP32Time::getMillis(){
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec/1000;
}

/*!
@brief get the current microseconds as long
@brief get the current microseconds as unsigned long
*/
long ESP32Time::getMicros(){
unsigned long ESP32Time::getMicros(){
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec;
}

/*!
@brief get the current epoch seconds as long
@brief get the current epoch seconds as unsigned long
*/
long ESP32Time::getEpoch(){
unsigned long ESP32Time::getEpoch(){
struct tm timeinfo = getTimeStruct();
return mktime(&timeinfo);
}

/*!
@brief get the current epoch seconds as long from the rtc without offset
@brief get the current epoch seconds as unsigned long from the rtc without offset
*/
long ESP32Time::getLocalEpoch(){
unsigned long ESP32Time::getLocalEpoch(){
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_sec;
unsigned long epoch = tv.tv_sec;
if (this->overflow){
epoch += 63071999 + 2019686400;
}
return epoch;
}

/*!
Expand Down
17 changes: 10 additions & 7 deletions ESP32Time.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ class ESP32Time {

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

long getEpoch();
long getMillis();
long getMicros();
unsigned long getEpoch();
unsigned long getMillis();
unsigned long getMicros();
int getSecond();
int getMinute();
int getHour(bool mode = false);
Expand All @@ -56,8 +56,11 @@ class ESP32Time {
int getMonth();
int getYear();

long offset;
long getLocalEpoch();
unsigned long offset;
unsigned long getLocalEpoch();

private:
bool overflow;


};
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,16 @@ getDateTime(true) // (String) Sunday, January 17 2021 15:24:38
getTimeDate() // (String) 15:24:38 Sun, Jan 17 2021
getTimeDate(true) // (String) 15:24:38 Sunday, January 17 2021
getMicros() // (long) 723546
getMillis() // (long) 723
getEpoch() // (long) 1609459200
getLocalEpoch() // (long) 1609459200 // local epoch without offset
getMicros() // (unsigned long) 723546
getMillis() // (unsigned long) 723
getEpoch() // (unsigned long) 1609459200
getLocalEpoch() // (unsigned long) 1609459200 // local epoch without offset
getSecond() // (int) 38 (0-59)
getMinute() // (int) 24 (0-59)
getHour() // (int) 3 (0-12)
getHour(true) // (int) 15 (0-23)
getAmPm() // (String) pm
getAmPm(true) // (String) PM
getAmPm(false) // (String) PM
getDay() // (int) 17 (1-31)
getDayofWeek() // (int) 0 (0-6)
getDayofYear() // (int) 16 (0-365)
Expand Down
2 changes: 1 addition & 1 deletion examples/esp32_time/esp32_time.ino
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ ESP32Time rtc(3600); // offset in seconds GMT+1

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

Expand Down
14 changes: 7 additions & 7 deletions examples/esp32_time_multiple/esp32_time_multiple.ino
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ void loop() {
// Serial.println(rtc.getTimeDate()); // (String) 15:24:38 Sun, Jan 17 2021
// Serial.println(rtc.getTimeDate(true)); // (String) 15:24:38 Sunday, January 17 2021
//
// Serial.println(rtc.getMicros()); // (long) 723546
// Serial.println(rtc.getMillis()); // (long) 723
// Serial.println(rtc.getEpoch()); // (long) 1609459200
// Serial.println(rtc.getMicros()); // (unsigned long) 723546
// Serial.println(rtc.getMillis()); // (unsigned long) 723
// Serial.println(rtc.getEpoch()); // (unsigned long) 1609459200
// Serial.println(rtc.getSecond()); // (int) 38 (0-59)
// Serial.println(rtc.getMinute()); // (int) 24 (0-59)
// Serial.println(rtc.getHour()); // (int) 3 (0-12)
Expand All @@ -68,10 +68,10 @@ void loop() {

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

Serial.println(rtc.getEpoch()); // (long)
Serial.println(rtc1.getEpoch()); // (long)
Serial.println(rtc2.getEpoch()); // (long)
Serial.println(rtc.getEpoch()); // (unsigned long)
Serial.println(rtc1.getEpoch()); // (unsigned long)
Serial.println(rtc2.getEpoch()); // (unsigned long)

Serial.println(rtc.getLocalEpoch()); // (long) epoch without offset, same for all instances
Serial.println(rtc.getLocalEpoch()); // (unsigned long) epoch without offset, same for all instances
delay(1000);
}
70 changes: 70 additions & 0 deletions examples/esp32_time_sleep/esp32_time_sleep.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
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>

#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 5 /* Time ESP32 will go to sleep (in seconds) */

ESP32Time rtc;


void wakeup_reason() {
esp_sleep_wakeup_cause_t wakeup_reason;

wakeup_reason = esp_sleep_get_wakeup_cause();
switch (wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default :
Serial.printf("Wakeup was not caused by deep sleep: %d\n", wakeup_reason);
rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
//rtc.setTime(1609459200); // 1st Jan 2021 00:00:00
//rtc.offset = 7200; // change offset value

break;
}
}

void setup() {
Serial.begin(115200);

wakeup_reason();

Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format

esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR);

Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
}

void loop() {

}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ESP32Time",
"version": "1.1.0",
"version": "2.0.0",
"keywords": "Arduino, ESP32, Time, Internal RTC",
"description": "An Arduino library for setting and retrieving internal RTC time on ESP32 boards",
"repository":
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=ESP32Time
version=1.1.0
version=2.0.0
author=fbiego
maintainer=fbiego
sentence=Set and retrieve internal RTC time on ESP32 boards.
Expand Down

0 comments on commit 198c603

Please sign in to comment.