Skip to content

Commit 8decdc3

Browse files
authored
Allow to use temporary strings in configTime (#8606)
1 parent 7935bed commit 8decdc3

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

Diff for: cores/esp8266/Arduino.h

+11-2
Original file line numberDiff line numberDiff line change
@@ -269,10 +269,13 @@ long map(long, long, long, long, long);
269269

270270
void setTZ(const char* tz);
271271

272-
void configTime(int timezone, int daylightOffset_sec, const char* server1,
272+
// configure time using POSIX TZ string
273+
// server pointers *must remain valid* for the duration of the program
274+
void configTime(const char* tz, const char* server1,
273275
const char* server2 = nullptr, const char* server3 = nullptr);
274276

275-
void configTime(const char* tz, const char* server1,
277+
// configures with approximated TZ value. part of the old api, prefer configTime with TZ variable
278+
void configTime(int timezone, int daylightOffset_sec, const char* server1,
276279
const char* server2 = nullptr, const char* server3 = nullptr);
277280

278281
// esp32 api compatibility
@@ -290,6 +293,12 @@ bool getLocalTime(struct tm * info, uint32_t ms = 5000);
290293
#include "WCharacter.h"
291294
#include "WString.h"
292295

296+
// configTime wrappers for temporary server{1,2,3} strings
297+
void configTime(int timezone, int daylightOffset_sec, String server1,
298+
String server2 = String(), String server3 = String());
299+
void configTime(const char* tz, String server1,
300+
String server2 = String(), String server3 = String());
301+
293302
#include "HardwareSerial.h"
294303
#include "Esp.h"
295304
#include "Updater.h"

Diff for: cores/esp8266/time.cpp

+26
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,19 @@ void configTime(int timezone_sec, int daylightOffset_sec, const char* server1, c
208208
sntp_init();
209209
}
210210

211+
void configTime(int timezone_sec, int daylightOffset_sec, String server1, String server2, String server3)
212+
{
213+
static String servers[3];
214+
servers[0] = std::move(server1);
215+
servers[1] = std::move(server2);
216+
servers[2] = std::move(server3);
217+
218+
configTime(timezone_sec, daylightOffset_sec,
219+
servers[0].length() ? servers[0].c_str() : nullptr,
220+
servers[1].length() ? servers[1].c_str() : nullptr,
221+
servers[2].length() ? servers[2].c_str() : nullptr);
222+
}
223+
211224
void setTZ(const char* tz){
212225

213226
char tzram[strlen_P(tz) + 1];
@@ -228,6 +241,19 @@ void configTime(const char* tz, const char* server1, const char* server2, const
228241
sntp_init();
229242
}
230243

244+
void configTime(const char* tz, String server1, String server2, String server3)
245+
{
246+
static String servers[3];
247+
servers[0] = std::move(server1);
248+
servers[1] = std::move(server2);
249+
servers[2] = std::move(server3);
250+
251+
configTime(tz,
252+
servers[0].length() ? servers[0].c_str() : nullptr,
253+
servers[1].length() ? servers[1].c_str() : nullptr,
254+
servers[2].length() ? servers[2].c_str() : nullptr);
255+
}
256+
231257
static BoolCB _settimeofday_cb;
232258

233259
void settimeofday_cb (const TrivialCB& cb)

0 commit comments

Comments
 (0)