From 695df9b979f659b759f83c8ff8db214096cd59c3 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 29 Jan 2026 23:49:07 -0600 Subject: [PATCH] bot review --- esphome/components/time/real_time_clock.cpp | 9 +++++---- esphome/components/time/real_time_clock.h | 4 ++++ tests/components/time/posix_tz_parser.cpp | 5 +++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/esphome/components/time/real_time_clock.cpp b/esphome/components/time/real_time_clock.cpp index 84d611d423..f6478251e5 100644 --- a/esphome/components/time/real_time_clock.cpp +++ b/esphome/components/time/real_time_clock.cpp @@ -14,8 +14,8 @@ #include #endif #include - #include +#include namespace esphome::time { @@ -31,10 +31,11 @@ void RealTimeClock::dump_config() { ESP_LOGCONFIG(TAG, "Timezone: UTC%+d:%02d", std_hours, std_mins); if (tz.has_dst) { int dst_hours = -tz.dst_offset_seconds / 3600; + int dst_mins = abs(tz.dst_offset_seconds % 3600) / 60; // Always use M format - tzdata and aioesphomeapi only generate M format rules - ESP_LOGCONFIG(TAG, " DST: UTC%+d, M%d.%d.%d/%" PRId32 " - M%d.%d.%d/%" PRId32, dst_hours, tz.dst_start.month, - tz.dst_start.week, tz.dst_start.day_of_week, tz.dst_start.time_seconds / 3600, tz.dst_end.month, - tz.dst_end.week, tz.dst_end.day_of_week, tz.dst_end.time_seconds / 3600); + ESP_LOGCONFIG(TAG, " DST: UTC%+d:%02d, M%d.%d.%d/%" PRId32 " - M%d.%d.%d/%" PRId32, dst_hours, dst_mins, + tz.dst_start.month, tz.dst_start.week, tz.dst_start.day_of_week, tz.dst_start.time_seconds / 3600, + tz.dst_end.month, tz.dst_end.week, tz.dst_end.day_of_week, tz.dst_end.time_seconds / 3600); } #endif auto time = this->now(); diff --git a/esphome/components/time/real_time_clock.h b/esphome/components/time/real_time_clock.h index 78e99a1924..c608351310 100644 --- a/esphome/components/time/real_time_clock.h +++ b/esphome/components/time/real_time_clock.h @@ -29,6 +29,10 @@ class RealTimeClock : public PollingComponent { /// Set the time zone from a character buffer with known length. /// The buffer does not need to be null-terminated. void set_timezone(const char *tz, size_t len) { + if (tz == nullptr) { + this->apply_timezone_(nullptr); + return; + } // Stack buffer - TZ strings are typically short but allow up to 128 char buf[128]; if (len >= sizeof(buf)) diff --git a/tests/components/time/posix_tz_parser.cpp b/tests/components/time/posix_tz_parser.cpp index 1a84a39835..4a276d7b71 100644 --- a/tests/components/time/posix_tz_parser.cpp +++ b/tests/components/time/posix_tz_parser.cpp @@ -782,13 +782,14 @@ TEST_P(LibcVerificationTest, MatchesLibc) { ASSERT_TRUE(parse_posix_tz(tz_str, tz)); // Our implementation - struct tm our_tm; - epoch_to_local_tm(epoch, tz, &our_tm); + struct tm our_tm {}; + ASSERT_TRUE(epoch_to_local_tm(epoch, tz, &our_tm)); // libc implementation setenv("TZ", tz_str, 1); tzset(); struct tm *libc_tm = localtime(&epoch); + ASSERT_NE(libc_tm, nullptr); EXPECT_EQ(our_tm.tm_year, libc_tm->tm_year); EXPECT_EQ(our_tm.tm_mon, libc_tm->tm_mon);