1
0
mirror of https://github.com/esphome/esphome.git synced 2026-02-08 00:31:58 +00:00
This commit is contained in:
J. Nick Koston
2026-01-29 23:29:10 -06:00
parent 1353dbc31e
commit 300b7169ad
4 changed files with 13 additions and 10 deletions

View File

@@ -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<char, 24> buf) {
size_t format_dst_rule(const DSTRule &rule, std::span<char, DST_RULE_BUF_SIZE> buf) {
// Format rule part
int pos = 0;
switch (rule.type) {

View File

@@ -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<char, 24> buf);
size_t format_dst_rule(const DSTRule &rule, std::span<char, DST_RULE_BUF_SIZE> buf);
} // namespace internal

View File

@@ -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);

View File

@@ -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);