From 2ff10b17fb90a980e7f4dcdb79df73faf9bdbcb8 Mon Sep 17 00:00:00 2001 From: Felix Biego Date: Sun, 15 May 2022 14:34:26 +0000 Subject: [PATCH] offset support #17 --- ESP32Time.cpp | 22 +++++- ESP32Time.h | 4 + README.md | 3 + examples/esp32_time/esp32_time.ino | 5 +- .../esp32_time_multiple.ino | 77 +++++++++++++++++++ keywords.txt | 1 + library.json | 2 +- library.properties | 2 +- 8 files changed, 112 insertions(+), 4 deletions(-) create mode 100644 examples/esp32_time_multiple/esp32_time_multiple.ino diff --git a/ESP32Time.cpp b/ESP32Time.cpp index 3a8168f..168ffc8 100644 --- a/ESP32Time.cpp +++ b/ESP32Time.cpp @@ -31,6 +31,15 @@ */ 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 @@ -92,7 +101,10 @@ void ESP32Time::setTime(long epoch, int ms) { tm ESP32Time::getTimeStruct(){ struct tm timeinfo; getLocalTime(&timeinfo); - return timeinfo; + time_t tt = mktime (&timeinfo); + tt = tt + offset; + struct tm * tn = localtime(&tt);; + return *tn; } /*! @@ -202,6 +214,14 @@ long ESP32Time::getMicros(){ @brief get the current epoch seconds as long */ long ESP32Time::getEpoch(){ + struct tm timeinfo = getTimeStruct(); + return mktime(&timeinfo); +} + +/*! + @brief get the current epoch seconds as long from the rtc without offset +*/ +long ESP32Time::getLocalEpoch(){ struct timeval tv; gettimeofday(&tv, NULL); return tv.tv_sec; diff --git a/ESP32Time.h b/ESP32Time.h index f4f0990..3038aec 100644 --- a/ESP32Time.h +++ b/ESP32Time.h @@ -31,6 +31,7 @@ class ESP32Time { public: ESP32Time(); + ESP32Time(long offset); void setTime(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); @@ -55,6 +56,9 @@ class ESP32Time { int getMonth(); int getYear(); + long offset; + long getLocalEpoch(); + }; diff --git a/README.md b/README.md index ccd102d..d7d79e7 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,8 @@ An Arduino library for setting and retrieving internal RTC time on ESP32 boards ## Functions ``` +ESP32Time rtc(offset); // create an instance with a specifed offset in seconds +rtc.offset; // get or modify the current offset setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30 setTime(1609459200); // 1st Jan 2021 00:00:00 setTimeStruct(time); // set with time struct @@ -21,6 +23,7 @@ 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 getSecond() // (int) 38 (0-59) getMinute() // (int) 24 (0-59) getHour() // (int) 3 (0-12) diff --git a/examples/esp32_time/esp32_time.ino b/examples/esp32_time/esp32_time.ino index 8c18ff8..53a7066 100644 --- a/examples/esp32_time/esp32_time.ino +++ b/examples/esp32_time/esp32_time.ino @@ -24,12 +24,14 @@ #include -ESP32Time rtc; +//ESP32Time rtc; +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(1609459200); // 1st Jan 2021 00:00:00 + //rtc.offset = 7200; // change offset value /*---------set with NTP---------------*/ // configTime(gmtOffset_sec, daylightOffset_sec, ntpServer); @@ -63,6 +65,7 @@ void loop() { // Serial.println(rtc.getMonth()); // (int) 0 (0-11) // Serial.println(rtc.getYear()); // (int) 2021 +// Serial.println(rtc.getLocalEpoch()); // (long) 1609459200 epoch without offset Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format // formating options http://www.cplusplus.com/reference/ctime/strftime/ diff --git a/examples/esp32_time_multiple/esp32_time_multiple.ino b/examples/esp32_time_multiple/esp32_time_multiple.ino new file mode 100644 index 0000000..bbab7e8 --- /dev/null +++ b/examples/esp32_time_multiple/esp32_time_multiple.ino @@ -0,0 +1,77 @@ +/* + 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 rtc; +ESP32Time rtc1(-3600); // offset GMT-1 +ESP32Time rtc2(7200); // offset GMT+2 + +void setup() { + Serial.begin(115200); + rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30 + //rtc1.setTime(1609459200); // 1st Jan 2021 00:00:00 + // time can be set on one instance + // no need for rtc1.setTime() or rtc2.setTime() + + +} + +void loop() { +// Serial.println(rtc.getTime()); // (String) 15:24:38 +// Serial.println(rtc.getDate()); // (String) Sun, Jan 17 2021 +// Serial.println(rtc.getDate(true)); // (String) Sunday, January 17 2021 +// Serial.println(rtc.getDateTime()); // (String) Sun, Jan 17 2021 15:24:38 +// Serial.println(rtc.getDateTime(true)); // (String) Sunday, January 17 2021 15:24:38 +// 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.getSecond()); // (int) 38 (0-59) +// Serial.println(rtc.getMinute()); // (int) 24 (0-59) +// Serial.println(rtc.getHour()); // (int) 3 (0-12) +// Serial.println(rtc.getHour(true)); // (int) 15 (0-23) +// Serial.println(rtc.getAmPm()); // (String) pm +// Serial.println(rtc.getAmPm(true)); // (String) PM +// Serial.println(rtc.getDay()); // (int) 17 (1-31) +// Serial.println(rtc.getDayofWeek()); // (int) 0 (0-6) +// Serial.println(rtc.getDayofYear()); // (int) 16 (0-365) +// Serial.println(rtc.getMonth()); // (int) 0 (0-11) +// Serial.println(rtc.getYear()); // (int) 2021 + + Serial.println(rtc.getTime("RTC0: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format + Serial.println(rtc1.getTime("RTC1: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format + Serial.println(rtc2.getTime("RTC2: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format + + // 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.getLocalEpoch()); // (long) epoch without offset, same for all instances + delay(1000); +} diff --git a/keywords.txt b/keywords.txt index 95aa012..9914509 100644 --- a/keywords.txt +++ b/keywords.txt @@ -11,6 +11,7 @@ getAmPm KEYWORD2 getMillis KEYWORD2 getMicros KEYWORD2 getEpoch KEYWORD2 +getLocalEpoch KEYWORD2 getSecond KEYWORD2 getMinute KEYWORD2 getHour KEYWORD2 diff --git a/library.json b/library.json index 23fc6ea..50cdb52 100644 --- a/library.json +++ b/library.json @@ -1,6 +1,6 @@ { "name": "ESP32Time", - "version": "1.0.4", + "version": "1.1.0", "keywords": "Arduino, ESP32, Time, Internal RTC", "description": "An Arduino library for setting and retrieving internal RTC time on ESP32 boards", "repository": diff --git a/library.properties b/library.properties index 98ab488..3f4816a 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=ESP32Time -version=1.0.4 +version=1.1.0 author=fbiego maintainer=fbiego sentence=Set and retrieve internal RTC time on ESP32 boards.