Fix Internet Time on ESP32

This commit is contained in:
Luc 2024-09-08 08:46:26 +08:00
parent 6f6c1842ce
commit 4b01d89095
2 changed files with 13 additions and 2 deletions

View File

@ -114,7 +114,7 @@ bool TimeService::begin() {
s2 = ESP3DSettings::readString(ESP_TIME_SERVER2);
s3 = ESP3DSettings::readString(ESP_TIME_SERVER3);
#if defined(ARDUINO_ARCH_ESP32)
configTzTime(_time_zone.c_str(), s1.c_str(), s2.c_str(), s3.c_str());
configTzTime(_time_zone_config.c_str(), s1.c_str(), s2.c_str(), s3.c_str());
#endif // ARDUINO_ARCH_ESP32
#if defined(ARDUINO_ARCH_ESP8266)
configTime(t1.c_str(), s1.c_str(), s2.c_str(), s3.c_str());
@ -181,16 +181,26 @@ bool TimeService::updateTimeZone(bool fromsettings) {
setTZ(stmp.c_str());
#endif // ARDUINO_ARCH_ESP8266
_time_zone_config = "<";
_time_zone_config += _time_zone[0];
_time_zone_config += _time_zone[1];
_time_zone_config += _time_zone[2];
_time_zone_config += _time_zone[4];
_time_zone_config += _time_zone[5];
_time_zone_config += ">";
_time_zone_config += _time_zone[0]=='+' ? "-" : "+";
_time_zone_config += &_time_zone[1];
esp3d_log_d("Time zone is %s", _time_zone_config.c_str());
return true;
}
const char* TimeService::getCurrentTime() {
struct tm tmstruct;
static char buf[20];
time_t now;
// get current time
time(&now);
localtime_r(&now, &tmstruct);
static char buf[20];
strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", &tmstruct);
esp3d_log("Time string is %s", buf);
return buf;

View File

@ -45,6 +45,7 @@ class TimeService {
bool _started;
bool _isInternetTime;
String _time_zone;
String _time_zone_config;
};
extern TimeService timeService;