Skip to content

Commit 234c855

Browse files
markyadme-no-dev
authored andcommitted
add configTzTime() to setup sntp using TZ environment variable (espressif#608)
1 parent 0f83dbc commit 234c855

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

Diff for: cores/esp32/Arduino.h

+2
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 10
168168
extern "C" bool getLocalTime(struct tm * info, uint32_t ms = 5000);
169169
extern "C" void configTime(long gmtOffset_sec, int daylightOffset_sec,
170170
const char* server1, const char* server2 = nullptr, const char* server3 = nullptr);
171+
extern "C" void configTzTime(const char* tz,
172+
const char* server1, const char* server2 = nullptr, const char* server3 = nullptr);
171173

172174
// WMath prototypes
173175
long random(long);

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ static void setTimeZone(long offset, int daylight)
4545
* */
4646
void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1, const char* server2, const char* server3)
4747
{
48-
4948
if(sntp_enabled()){
5049
sntp_stop();
5150
}
@@ -57,6 +56,24 @@ void configTime(long gmtOffset_sec, int daylightOffset_sec, const char* server1,
5756
setTimeZone(gmtOffset_sec, daylightOffset_sec);
5857
}
5958

59+
/*
60+
* configTzTime
61+
* sntp setup using TZ environment variable
62+
* */
63+
void configTzTime(const char* tz, const char* server1, const char* server2, const char* server3)
64+
{
65+
if(sntp_enabled()){
66+
sntp_stop();
67+
}
68+
sntp_setoperatingmode(SNTP_OPMODE_POLL);
69+
sntp_setservername(0, (char*)server1);
70+
sntp_setservername(1, (char*)server2);
71+
sntp_setservername(2, (char*)server3);
72+
sntp_init();
73+
setenv("TZ", tz, 1);
74+
tzset();
75+
}
76+
6077
bool getLocalTime(struct tm * info, uint32_t ms)
6178
{
6279
uint32_t count = ms / 10;

0 commit comments

Comments
 (0)