From 4b01d89095c7479a01e2f703834476a944ee90d7 Mon Sep 17 00:00:00 2001 From: Luc <8822552+luc-github@users.noreply.github.com> Date: Sun, 8 Sep 2024 08:46:26 +0800 Subject: [PATCH] Fix Internet Time on ESP32 --- esp3d/src/modules/time/time_service.cpp | 14 ++++++++++++-- esp3d/src/modules/time/time_service.h | 1 + 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/esp3d/src/modules/time/time_service.cpp b/esp3d/src/modules/time/time_service.cpp index 2be4d3d8..1d1f5f91 100644 --- a/esp3d/src/modules/time/time_service.cpp +++ b/esp3d/src/modules/time/time_service.cpp @@ -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; diff --git a/esp3d/src/modules/time/time_service.h b/esp3d/src/modules/time/time_service.h index 00269f60..f41fab21 100644 --- a/esp3d/src/modules/time/time_service.h +++ b/esp3d/src/modules/time/time_service.h @@ -45,6 +45,7 @@ class TimeService { bool _started; bool _isInternetTime; String _time_zone; + String _time_zone_config; }; extern TimeService timeService;