From c1d380dee41476974b3b8cc4ef55541ee755adaf Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 29 Jan 2026 23:58:07 -0600 Subject: [PATCH] more fixes --- esphome/components/logger/logger_host.cpp | 7 +++++ esphome/core/time.cpp | 31 ++++++++++++++++------- 2 files changed, 29 insertions(+), 9 deletions(-) diff --git a/esphome/components/logger/logger_host.cpp b/esphome/components/logger/logger_host.cpp index 874cdabd22..e956e74957 100644 --- a/esphome/components/logger/logger_host.cpp +++ b/esphome/components/logger/logger_host.cpp @@ -1,5 +1,8 @@ #if defined(USE_HOST) #include "logger.h" +#ifdef USE_TIME_TIMEZONE +#include "esphome/components/time/posix_tz.h" +#endif namespace esphome::logger { @@ -11,7 +14,11 @@ void HOT Logger::write_msg_(const char *msg, size_t len) { time_t rawtime; time(&rawtime); struct tm timeinfo; +#ifdef USE_TIME_TIMEZONE + time::epoch_to_local_tm(rawtime, time::get_global_tz(), &timeinfo); +#else localtime_r(&rawtime, &timeinfo); // Thread-safe version +#endif size_t pos = strftime(buffer, TIMESTAMP_LEN + 1, "[%H:%M:%S]", &timeinfo); // Copy message (with newline already included by caller) diff --git a/esphome/core/time.cpp b/esphome/core/time.cpp index f229340083..918c3c1ffb 100644 --- a/esphome/core/time.cpp +++ b/esphome/core/time.cpp @@ -257,17 +257,30 @@ void ESPTime::recalc_timestamp_utc(bool use_day_of_year) { } void ESPTime::recalc_timestamp_local() { - struct tm tm; +#ifdef USE_TIME_TIMEZONE + // Calculate timestamp as if fields were UTC + this->recalc_timestamp_utc(false); + if (this->timestamp == -1) { + return; // Invalid time + } - tm.tm_year = this->year - 1900; - tm.tm_mon = this->month - 1; - tm.tm_mday = this->day_of_month; - tm.tm_hour = this->hour; - tm.tm_min = this->minute; - tm.tm_sec = this->second; - tm.tm_isdst = -1; + // Now convert from local to UTC by adding the offset + // POSIX: local = utc - offset, so utc = local + offset + const auto &tz = time::get_global_tz(); - this->timestamp = mktime(&tm); + // Use standard offset as initial guess to determine DST status + time_t approx_utc = this->timestamp + tz.std_offset_seconds; + + // Check if DST is in effect and apply the appropriate offset + if (time::internal::is_in_dst(approx_utc, tz)) { + this->timestamp += tz.dst_offset_seconds; + } else { + this->timestamp += tz.std_offset_seconds; + } +#else + // No timezone support - treat as UTC + this->recalc_timestamp_utc(false); +#endif } int32_t ESPTime::timezone_offset() {