Skip to content

Commit 2ff10b1

Browse files
committed
offset support #17
1 parent c7f60fd commit 2ff10b1

File tree

8 files changed

+112
-4
lines changed

8 files changed

+112
-4
lines changed

ESP32Time.cpp

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@
3131
*/
3232
ESP32Time::ESP32Time(){}
3333

34+
/*!
35+
@brief Constructor for ESP32Time
36+
@param offest
37+
gmt offset in seconds
38+
*/
39+
ESP32Time::ESP32Time(long offset){
40+
this->offset = offset;
41+
}
42+
3443
/*!
3544
@brief set the internal RTC time
3645
@param sc
@@ -92,7 +101,10 @@ void ESP32Time::setTime(long epoch, int ms) {
92101
tm ESP32Time::getTimeStruct(){
93102
struct tm timeinfo;
94103
getLocalTime(&timeinfo);
95-
return timeinfo;
104+
time_t tt = mktime (&timeinfo);
105+
tt = tt + offset;
106+
struct tm * tn = localtime(&tt);;
107+
return *tn;
96108
}
97109

98110
/*!
@@ -202,6 +214,14 @@ long ESP32Time::getMicros(){
202214
@brief get the current epoch seconds as long
203215
*/
204216
long ESP32Time::getEpoch(){
217+
struct tm timeinfo = getTimeStruct();
218+
return mktime(&timeinfo);
219+
}
220+
221+
/*!
222+
@brief get the current epoch seconds as long from the rtc without offset
223+
*/
224+
long ESP32Time::getLocalEpoch(){
205225
struct timeval tv;
206226
gettimeofday(&tv, NULL);
207227
return tv.tv_sec;

ESP32Time.h

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

3232
public:
3333
ESP32Time();
34+
ESP32Time(long offset);
3435
void setTime(long epoch = 1609459200, int ms = 0); // default (1609459200) = 1st Jan 2021
3536
void setTime(int sc, int mn, int hr, int dy, int mt, int yr, int ms = 0);
3637
void setTimeStruct(tm t);
@@ -55,6 +56,9 @@ class ESP32Time {
5556
int getMonth();
5657
int getYear();
5758

59+
long offset;
60+
long getLocalEpoch();
61+
5862

5963
};
6064

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ An Arduino library for setting and retrieving internal RTC time on ESP32 boards
66
## Functions
77

88
```
9+
ESP32Time rtc(offset); // create an instance with a specifed offset in seconds
10+
rtc.offset; // get or modify the current offset
911
setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
1012
setTime(1609459200); // 1st Jan 2021 00:00:00
1113
setTimeStruct(time); // set with time struct
@@ -21,6 +23,7 @@ getTimeDate(true) // (String) 15:24:38 Sunday, January 17 2021
2123
getMicros() // (long) 723546
2224
getMillis() // (long) 723
2325
getEpoch() // (long) 1609459200
26+
getLocalEpoch() // (long) 1609459200 // local epoch without offset
2427
getSecond() // (int) 38 (0-59)
2528
getMinute() // (int) 24 (0-59)
2629
getHour() // (int) 3 (0-12)

examples/esp32_time/esp32_time.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,14 @@
2424

2525
#include <ESP32Time.h>
2626

27-
ESP32Time rtc;
27+
//ESP32Time rtc;
28+
ESP32Time rtc(3600); // offset in seconds GMT+1
2829

2930
void setup() {
3031
Serial.begin(115200);
3132
rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
3233
//rtc.setTime(1609459200); // 1st Jan 2021 00:00:00
34+
//rtc.offset = 7200; // change offset value
3335

3436
/*---------set with NTP---------------*/
3537
// configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
@@ -63,6 +65,7 @@ void loop() {
6365
// Serial.println(rtc.getMonth()); // (int) 0 (0-11)
6466
// Serial.println(rtc.getYear()); // (int) 2021
6567

68+
// Serial.println(rtc.getLocalEpoch()); // (long) 1609459200 epoch without offset
6669
Serial.println(rtc.getTime("%A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
6770
// formating options http://www.cplusplus.com/reference/ctime/strftime/
6871

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
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+
ESP32Time rtc;
28+
ESP32Time rtc1(-3600); // offset GMT-1
29+
ESP32Time rtc2(7200); // offset GMT+2
30+
31+
void setup() {
32+
Serial.begin(115200);
33+
rtc.setTime(30, 24, 15, 17, 1, 2021); // 17th Jan 2021 15:24:30
34+
//rtc1.setTime(1609459200); // 1st Jan 2021 00:00:00
35+
// time can be set on one instance
36+
// no need for rtc1.setTime() or rtc2.setTime()
37+
38+
39+
}
40+
41+
void loop() {
42+
// Serial.println(rtc.getTime()); // (String) 15:24:38
43+
// Serial.println(rtc.getDate()); // (String) Sun, Jan 17 2021
44+
// Serial.println(rtc.getDate(true)); // (String) Sunday, January 17 2021
45+
// Serial.println(rtc.getDateTime()); // (String) Sun, Jan 17 2021 15:24:38
46+
// Serial.println(rtc.getDateTime(true)); // (String) Sunday, January 17 2021 15:24:38
47+
// Serial.println(rtc.getTimeDate()); // (String) 15:24:38 Sun, Jan 17 2021
48+
// Serial.println(rtc.getTimeDate(true)); // (String) 15:24:38 Sunday, January 17 2021
49+
//
50+
// Serial.println(rtc.getMicros()); // (long) 723546
51+
// Serial.println(rtc.getMillis()); // (long) 723
52+
// Serial.println(rtc.getEpoch()); // (long) 1609459200
53+
// Serial.println(rtc.getSecond()); // (int) 38 (0-59)
54+
// Serial.println(rtc.getMinute()); // (int) 24 (0-59)
55+
// Serial.println(rtc.getHour()); // (int) 3 (0-12)
56+
// Serial.println(rtc.getHour(true)); // (int) 15 (0-23)
57+
// Serial.println(rtc.getAmPm()); // (String) pm
58+
// Serial.println(rtc.getAmPm(true)); // (String) PM
59+
// Serial.println(rtc.getDay()); // (int) 17 (1-31)
60+
// Serial.println(rtc.getDayofWeek()); // (int) 0 (0-6)
61+
// Serial.println(rtc.getDayofYear()); // (int) 16 (0-365)
62+
// Serial.println(rtc.getMonth()); // (int) 0 (0-11)
63+
// Serial.println(rtc.getYear()); // (int) 2021
64+
65+
Serial.println(rtc.getTime("RTC0: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
66+
Serial.println(rtc1.getTime("RTC1: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
67+
Serial.println(rtc2.getTime("RTC2: %A, %B %d %Y %H:%M:%S")); // (String) returns time with specified format
68+
69+
// formating options http://www.cplusplus.com/reference/ctime/strftime/
70+
71+
Serial.println(rtc.getEpoch()); // (long)
72+
Serial.println(rtc1.getEpoch()); // (long)
73+
Serial.println(rtc2.getEpoch()); // (long)
74+
75+
Serial.println(rtc.getLocalEpoch()); // (long) epoch without offset, same for all instances
76+
delay(1000);
77+
}

keywords.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ getAmPm KEYWORD2
1111
getMillis KEYWORD2
1212
getMicros KEYWORD2
1313
getEpoch KEYWORD2
14+
getLocalEpoch KEYWORD2
1415
getSecond KEYWORD2
1516
getMinute KEYWORD2
1617
getHour KEYWORD2

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.0.4",
3+
"version": "1.1.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.0.4
2+
version=1.1.0
33
author=fbiego
44
maintainer=fbiego
55
sentence=Set and retrieve internal RTC time on ESP32 boards.

0 commit comments

Comments
 (0)