From 234c855c9ba7c05159a9582f6fcff600609c3921 Mon Sep 17 00:00:00 2001 From: Mark D Date: Tue, 5 Sep 2017 04:15:55 -0400 Subject: [PATCH] add configTzTime() to setup sntp using TZ environment variable (#608) --- cores/esp32/Arduino.h | 2 ++ cores/esp32/esp32-hal-time.c | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/cores/esp32/Arduino.h b/cores/esp32/Arduino.h index bc7516b3..7c78433d 100644 --- a/cores/esp32/Arduino.h +++ b/cores/esp32/Arduino.h @@ -168,6 +168,8 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 10 extern "C" bool getLocalTime(struct tm * info, uint32_t ms = 5000); extern "C" void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2 = nullptr, const char* server3 = nullptr); +extern "C" void configTzTime(const char* tz, + const char* server1, const char* server2 = nullptr, const char* server3 = nullptr); // WMath prototypes long random(long); diff --git a/cores/esp32/esp32-hal-time.c b/cores/esp32/esp32-hal-time.c index ad22bc35..83f10217 100644 --- a/cores/esp32/esp32-hal-time.c +++ b/cores/esp32/esp32-hal-time.c @@ -45,7 +45,6 @@ static void setTimeZone(long offset, int daylight) * */ void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3) { - if(sntp_enabled()){ sntp_stop(); } @@ -57,6 +56,24 @@ void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, setTimeZone(gmtOffset_sec, daylightOffset_sec); } +/* + * configTzTime + * sntp setup using TZ environment variable + * */ +void configTzTime(const char* tz, const char* server1, const char* server2, const char* server3) +{ + if(sntp_enabled()){ + sntp_stop(); + } + sntp_setoperatingmode(SNTP_OPMODE_POLL); + sntp_setservername(0, (char*)server1); + sntp_setservername(1, (char*)server2); + sntp_setservername(2, (char*)server3); + sntp_init(); + setenv("TZ", tz, 1); + tzset(); +} + bool getLocalTime(struct tm * info, uint32_t ms) { uint32_t count = ms / 10;