diff --git a/esphome/components/time/posix_tz.cpp b/esphome/components/time/posix_tz.cpp index d54e611443..ab1d07811b 100644 --- a/esphome/components/time/posix_tz.cpp +++ b/esphome/components/time/posix_tz.cpp @@ -342,7 +342,7 @@ bool __attribute__((noinline)) is_in_dst(time_t utc_epoch, const ParsedTimezone } } -size_t format_dst_rule(const DSTRule &rule, std::span buf) { +size_t format_dst_rule(const DSTRule &rule, std::span buf) { // Format rule part int pos = 0; switch (rule.type) { diff --git a/esphome/components/time/posix_tz.h b/esphome/components/time/posix_tz.h index fa2e7f8ab6..e994d53fda 100644 --- a/esphome/components/time/posix_tz.h +++ b/esphome/components/time/posix_tz.h @@ -113,11 +113,14 @@ time_t calculate_dst_transition(int year, const DSTRule &rule, int32_t base_offs /// @return true if DST is in effect at the given time bool is_in_dst(time_t utc_epoch, const ParsedTimezone &tz); +/// Buffer size for format_dst_rule output +static constexpr size_t DST_RULE_BUF_SIZE = 24; + /// Format a DST rule for logging/display /// @param rule The DST rule to format -/// @param buf Output buffer (24 bytes recommended) +/// @param buf Output buffer /// @return Number of characters written (excluding null terminator) -size_t format_dst_rule(const DSTRule &rule, std::span buf); +size_t format_dst_rule(const DSTRule &rule, std::span buf); } // namespace internal diff --git a/esphome/components/time/real_time_clock.cpp b/esphome/components/time/real_time_clock.cpp index 649cacd3f0..025d613a0d 100644 --- a/esphome/components/time/real_time_clock.cpp +++ b/esphome/components/time/real_time_clock.cpp @@ -30,7 +30,7 @@ void RealTimeClock::dump_config() { 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; - char start_buf[24], end_buf[24]; + char start_buf[internal::DST_RULE_BUF_SIZE], end_buf[internal::DST_RULE_BUF_SIZE]; internal::format_dst_rule(this->parsed_tz_.dst_start, start_buf); internal::format_dst_rule(this->parsed_tz_.dst_end, end_buf); ESP_LOGCONFIG(TAG, " DST: UTC%+d, %s - %s", dst_hours, start_buf, end_buf); diff --git a/tests/components/time/posix_tz_parser.cpp b/tests/components/time/posix_tz_parser.cpp index fb3af1ae35..5311edbf27 100644 --- a/tests/components/time/posix_tz_parser.cpp +++ b/tests/components/time/posix_tz_parser.cpp @@ -843,7 +843,7 @@ TEST(PosixTzParser, FormatDstRuleMonthWeekDay) { rule.day_of_week = 0; rule.time_seconds = 2 * 3600; // 2:00 - char buf[24]; + char buf[internal::DST_RULE_BUF_SIZE]; size_t len = internal::format_dst_rule(rule, buf); EXPECT_STREQ(buf, "M3.2.0/2"); EXPECT_EQ(len, 8u); @@ -855,7 +855,7 @@ TEST(PosixTzParser, FormatDstRuleJulian) { rule.day = 60; rule.time_seconds = 2 * 3600; - char buf[24]; + char buf[internal::DST_RULE_BUF_SIZE]; size_t len = internal::format_dst_rule(rule, buf); EXPECT_STREQ(buf, "J60/2"); EXPECT_EQ(len, 5u); @@ -867,7 +867,7 @@ TEST(PosixTzParser, FormatDstRuleDayOfYear) { rule.day = 300; rule.time_seconds = 2 * 3600; - char buf[24]; + char buf[internal::DST_RULE_BUF_SIZE]; size_t len = internal::format_dst_rule(rule, buf); EXPECT_STREQ(buf, "300/2"); EXPECT_EQ(len, 5u); @@ -881,7 +881,7 @@ TEST(PosixTzParser, FormatDstRuleWithMinutes) { rule.day_of_week = 0; rule.time_seconds = 2 * 3600 + 30 * 60; // 2:30 - char buf[24]; + char buf[internal::DST_RULE_BUF_SIZE]; size_t len = internal::format_dst_rule(rule, buf); EXPECT_STREQ(buf, "M11.1.0/2:30"); EXPECT_EQ(len, 12u); @@ -895,7 +895,7 @@ TEST(PosixTzParser, FormatDstRuleWithSeconds) { rule.day_of_week = 0; rule.time_seconds = 2 * 3600 + 30 * 60 + 45; // 2:30:45 - char buf[24]; + char buf[internal::DST_RULE_BUF_SIZE]; size_t len = internal::format_dst_rule(rule, buf); EXPECT_STREQ(buf, "M3.5.0/2:30:45"); EXPECT_EQ(len, 14u); @@ -909,7 +909,7 @@ TEST(PosixTzParser, FormatDstRuleNegativeTime) { rule.day_of_week = 0; rule.time_seconds = -1 * 3600; // -1:00 (11 PM previous day) - char buf[24]; + char buf[internal::DST_RULE_BUF_SIZE]; size_t len = internal::format_dst_rule(rule, buf); EXPECT_STREQ(buf, "M3.2.0-1"); EXPECT_EQ(len, 8u);