diff --git a/cores/esp8266/Arduino.h b/cores/esp8266/Arduino.h index ee9a7964f0..52c6be4cd9 100644 --- a/cores/esp8266/Arduino.h +++ b/cores/esp8266/Arduino.h @@ -269,10 +269,13 @@ long map(long, long, long, long, long); void setTZ(const char* tz); -void configTime(int timezone, int daylightOffset_sec, const char* server1, +// configure time using POSIX TZ string +// server pointers *must remain valid* for the duration of the program +void configTime(const char* tz, const char* server1, const char* server2 = nullptr, const char* server3 = nullptr); -void configTime(const char* tz, const char* server1, +// configures with approximated TZ value. part of the old api, prefer configTime with TZ variable +void configTime(int timezone, int daylightOffset_sec, const char* server1, const char* server2 = nullptr, const char* server3 = nullptr); // esp32 api compatibility @@ -290,6 +293,12 @@ bool getLocalTime(struct tm * info, uint32_t ms = 5000); #include "WCharacter.h" #include "WString.h" +// configTime wrappers for temporary server{1,2,3} strings +void configTime(int timezone, int daylightOffset_sec, String server1, + String server2 = String(), String server3 = String()); +void configTime(const char* tz, String server1, + String server2 = String(), String server3 = String()); + #include "HardwareSerial.h" #include "Esp.h" #include "Updater.h" diff --git a/cores/esp8266/time.cpp b/cores/esp8266/time.cpp index d8308e62d7..970beada48 100644 --- a/cores/esp8266/time.cpp +++ b/cores/esp8266/time.cpp @@ -208,6 +208,19 @@ void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, c sntp_init(); } +void configTime(int timezone_sec, int daylightOffset_sec, String server1, String server2, String server3) +{ + static String servers[3]; + servers[0] = std::move(server1); + servers[1] = std::move(server2); + servers[2] = std::move(server3); + + configTime(timezone_sec, daylightOffset_sec, + servers[0].length() ? servers[0].c_str() : nullptr, + servers[1].length() ? servers[1].c_str() : nullptr, + servers[2].length() ? servers[2].c_str() : nullptr); +} + void setTZ(const char* tz){ char tzram[strlen_P(tz) + 1]; @@ -228,6 +241,19 @@ void configTime(const char* tz, const char* server1, const char* server2, const sntp_init(); } +void configTime(const char* tz, String server1, String server2, String server3) +{ + static String servers[3]; + servers[0] = std::move(server1); + servers[1] = std::move(server2); + servers[2] = std::move(server3); + + configTime(tz, + servers[0].length() ? servers[0].c_str() : nullptr, + servers[1].length() ? servers[1].c_str() : nullptr, + servers[2].length() ? servers[2].c_str() : nullptr); +} + static BoolCB _settimeofday_cb; void settimeofday_cb (const TrivialCB& cb)