Skip to content

Commit

Permalink
Allow to use temporary strings in configTime (#8606)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcspr authored Jun 24, 2022
1 parent 7935bed commit 8decdc3
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
13 changes: 11 additions & 2 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down
26 changes: 26 additions & 0 deletions cores/esp8266/time.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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)
Expand Down

0 comments on commit 8decdc3

Please sign in to comment.