From d45a20af83e253a5d2943991480485deb389e5f5 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 29 Jan 2026 21:25:46 -0600 Subject: [PATCH] tweak --- esphome/components/time/real_time_clock.cpp | 27 ++++++++++++++------- esphome/components/time/real_time_clock.h | 24 ++++++------------ 2 files changed, 26 insertions(+), 25 deletions(-) diff --git a/esphome/components/time/real_time_clock.cpp b/esphome/components/time/real_time_clock.cpp index 316600d2e4..e04bc3b3d5 100644 --- a/esphome/components/time/real_time_clock.cpp +++ b/esphome/components/time/real_time_clock.cpp @@ -25,7 +25,21 @@ RealTimeClock::RealTimeClock() = default; void RealTimeClock::dump_config() { #ifdef USE_TIME_TIMEZONE - ESP_LOGCONFIG(TAG, "Timezone: '%s'", this->timezone_.c_str()); + int std_hours = -this->parsed_tz_.std_offset_seconds / 3600; + int std_mins = abs(this->parsed_tz_.std_offset_seconds % 3600) / 60; + if (std_mins == 0) { + ESP_LOGCONFIG(TAG, "Timezone: UTC%+d", std_hours); + } else { + ESP_LOGCONFIG(TAG, "Timezone: UTC%+d:%02d", std_hours, std_mins); + } + if (this->parsed_tz_.has_dst) { + int dst_hours = -this->parsed_tz_.dst_offset_seconds / 3600; + ESP_LOGCONFIG(TAG, " DST: UTC%+d, M%d.%d.%d/%" PRId32 " - M%d.%d.%d/%" PRId32, dst_hours, + this->parsed_tz_.dst_start.month, this->parsed_tz_.dst_start.week, + this->parsed_tz_.dst_start.day_of_week, this->parsed_tz_.dst_start.time_seconds / 3600, + this->parsed_tz_.dst_end.month, this->parsed_tz_.dst_end.week, this->parsed_tz_.dst_end.day_of_week, + this->parsed_tz_.dst_end.time_seconds / 3600); + } #endif auto time = this->now(); ESP_LOGCONFIG(TAG, "Current time: %04d-%02d-%02d %02d:%02d:%02d", time.year, time.month, time.day_of_month, time.hour, @@ -72,11 +86,6 @@ void RealTimeClock::synchronize_epoch_(uint32_t epoch) { ret = settimeofday(&timev, nullptr); } -#ifdef USE_TIME_TIMEZONE - // Move timezone back to local timezone. - this->apply_timezone_(); -#endif - if (ret != 0) { ESP_LOGW(TAG, "setimeofday() failed with code %d", ret); } @@ -89,10 +98,10 @@ void RealTimeClock::synchronize_epoch_(uint32_t epoch) { } #ifdef USE_TIME_TIMEZONE -void RealTimeClock::apply_timezone_() { +void RealTimeClock::apply_timezone_(const char *tz) { // Parse the POSIX TZ string using our custom parser to avoid pulling in scanf (~7.6KB) - if (!parse_posix_tz(this->timezone_.c_str(), this->parsed_tz_)) { - ESP_LOGW(TAG, "Failed to parse timezone: %s", this->timezone_.c_str()); + if (!parse_posix_tz(tz, this->parsed_tz_)) { + ESP_LOGW(TAG, "Failed to parse timezone: %s", tz); // Reset to UTC on parse failure this->parsed_tz_ = ParsedTimezone{}; } diff --git a/esphome/components/time/real_time_clock.h b/esphome/components/time/real_time_clock.h index f1f1fcfa8a..055fa7f668 100644 --- a/esphome/components/time/real_time_clock.h +++ b/esphome/components/time/real_time_clock.h @@ -23,22 +23,15 @@ class RealTimeClock : public PollingComponent { explicit RealTimeClock(); #ifdef USE_TIME_TIMEZONE - /// Set the time zone. - void set_timezone(const std::string &tz) { - this->timezone_ = tz; - this->apply_timezone_(); - } + /// Set the time zone from a POSIX TZ string. + void set_timezone(const char *tz) { this->apply_timezone_(tz); } - /// Set the time zone from raw buffer, only if it differs from the current one. - void set_timezone(const char *tz, size_t len) { - if (this->timezone_.length() != len || memcmp(this->timezone_.c_str(), tz, len) != 0) { - this->timezone_.assign(tz, len); - this->apply_timezone_(); - } - } + /// Set the time zone from a null-terminated string with known length. + /// The length parameter is ignored since our parser uses null-terminated strings. + void set_timezone(const char *tz, size_t /*len*/) { this->apply_timezone_(tz); } - /// Get the time zone currently in use. - std::string get_timezone() { return this->timezone_; } + /// Set the time zone from a std::string. + void set_timezone(const std::string &tz) { this->apply_timezone_(tz.c_str()); } #endif /// Get the time in the currently defined timezone. @@ -73,9 +66,8 @@ class RealTimeClock : public PollingComponent { void synchronize_epoch_(uint32_t epoch); #ifdef USE_TIME_TIMEZONE - std::string timezone_{}; ParsedTimezone parsed_tz_{}; - void apply_timezone_(); + void apply_timezone_(const char *tz); #endif CallbackManager time_sync_callback_;