Skip to content

Commit c8140f8

Browse files
committed
add configTime, getLocalTime and support for time in Print
example: ```cpp //done once on WiFi init configTime(-7200, 3600, "pool.ntp.org"); //get local time struct tm timeinfo; if(!getLocalTime(&timeinfo)){ Serial.println("Failed to obtain time"); return; } //print time Serial.println(&timeinfo); //print time with different format Serial.println(&timeinfo, "%A, %B %d %Y %H:%M:%S"); ``` fixes: espressif#29
1 parent 6dfbdbc commit c8140f8

File tree

4 files changed

+108
-1
lines changed

4 files changed

+108
-1
lines changed

Diff for: cores/esp32/Arduino.h

+4
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ uint16_t makeWord(byte h, byte l);
162162
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
163163
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
164164

165+
extern "C" bool getLocalTime(struct tm * info, uint32_t ms = 5000);
166+
extern "C" void configTime(long gmtOffset_sec, int daylightOffset_sec,
167+
const char* server1, const char* server2 = nullptr, const char* server3 = nullptr);
168+
165169
// WMath prototypes
166170
long random(long);
167171
#endif /* __cplusplus */

Diff for: cores/esp32/Print.cpp

+20-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#include "Print.h"
3131
extern "C" {
32-
//#include "esp_common.h"
32+
#include "time.h"
3333
}
3434

3535
// Public Methods //////////////////////////////////////////////////////////////
@@ -153,6 +153,18 @@ size_t Print::print(const Printable& x)
153153
return x.printTo(*this);
154154
}
155155

156+
size_t Print::print(struct tm * timeinfo, const char * format)
157+
{
158+
const char * f = format;
159+
if(!f){
160+
f = "%c";
161+
}
162+
char buf[64];
163+
size_t written = strftime(buf, 64, f, timeinfo);
164+
print(buf);
165+
return written;
166+
}
167+
156168
size_t Print::println(void)
157169
{
158170
return print("\r\n");
@@ -228,6 +240,13 @@ size_t Print::println(const Printable& x)
228240
return n;
229241
}
230242

243+
size_t Print::println(struct tm * timeinfo, const char * format)
244+
{
245+
size_t n = print(timeinfo, format);
246+
n += println();
247+
return n;
248+
}
249+
231250
// Private Methods /////////////////////////////////////////////////////////////
232251

233252
size_t Print::printNumber(unsigned long n, uint8_t base)

Diff for: cores/esp32/Print.h

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ class Print
8383
size_t print(unsigned long, int = DEC);
8484
size_t print(double, int = 2);
8585
size_t print(const Printable&);
86+
size_t print(struct tm * timeinfo, const char * format = NULL);
8687

8788
//size_t println(const __FlashStringHelper *);
8889
size_t println(const String &s);
@@ -95,6 +96,7 @@ class Print
9596
size_t println(unsigned long, int = DEC);
9697
size_t println(double, int = 2);
9798
size_t println(const Printable&);
99+
size_t println(struct tm * timeinfo, const char * format = NULL);
98100
size_t println(void);
99101
};
100102

Diff for: cores/esp32/esp32-hal-time.c

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// Copyright 2015-2016 Espressif Systems (Shanghai) PTE LTD
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
#include "esp32-hal.h"
16+
#include "apps/sntp/sntp.h"
17+
18+
static void setTimeZone(long offset, int daylight)
19+
{
20+
char cst[16] = {0};
21+
char cdt[16] = "CDT";
22+
char tz[32] = {0};
23+
24+
if(offset % 3600){
25+
sprintf(cst, "CST%ld:%02u:%02u", offset / 3600, abs((offset % 3600) / 60), abs(offset % 60));
26+
} else {
27+
sprintf(cst, "CST%ld", offset / 3600);
28+
}
29+
if(daylight != 3600){
30+
long tz_dst = offset - daylight;
31+
if(tz_dst % 3600){
32+
sprintf(cdt, "CDT%ld:%02u:%02u", tz_dst / 3600, abs((tz_dst % 3600) / 60), abs(tz_dst % 60));
33+
} else {
34+
sprintf(cdt, "CDT%ld", tz_dst / 3600);
35+
}
36+
}
37+
sprintf(tz, "%s%s", cst, cdt);
38+
setenv("TZ", tz, 1);
39+
tzset();
40+
}
41+
42+
/*
43+
* configTime
44+
* Source: https://github.com/esp8266/Arduino/blob/master/cores/esp8266/time.c
45+
* */
46+
void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3)
47+
{
48+
49+
if(sntp_enabled()){
50+
sntp_stop();
51+
}
52+
sntp_setoperatingmode(SNTP_OPMODE_POLL);
53+
sntp_setservername(0, (char*)server1);
54+
sntp_setservername(1, (char*)server2);
55+
sntp_setservername(2, (char*)server3);
56+
sntp_init();
57+
setTimeZone(gmtOffset_sec, daylightOffset_sec);
58+
}
59+
60+
bool getLocalTime(struct tm * info, uint32_t ms)
61+
{
62+
uint32_t count = ms / 10;
63+
time_t now;
64+
65+
time(&now);
66+
localtime_r(&now, info);
67+
68+
if(info->tm_year > (2016 - 1900)){
69+
return true;
70+
}
71+
72+
while(count--) {
73+
delay(10);
74+
time(&now);
75+
localtime_r(&now, info);
76+
if(info->tm_year > (2016 - 1900)){
77+
return true;
78+
}
79+
}
80+
return false;
81+
}
82+

0 commit comments

Comments
 (0)