Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to use temporary strings in configTime #8606

Merged
merged 2 commits into from
Jun 24, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);

d-a-v marked this conversation as resolved.
Show resolved Hide resolved
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];
d-a-v marked this conversation as resolved.
Show resolved Hide resolved
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