From d6db522b1d4a48df9e44532e22705a904aa757a1 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Wed, 11 Mar 2026 09:40:23 -1000 Subject: [PATCH] [time] Fix settimeofday() failure on ESP8266 ESP8266's settimeofday() returns EINVAL (22) directly as the return value when the timezone parameter is non-NULL, rather than following POSIX convention of returning -1 and setting errno. The previous fallback code checked errno == EINVAL which never matched because errno was never set, so the retry with nullptr never triggered. Fix by always passing nullptr on ESP8266 since the platform requires it. --- esphome/components/time/real_time_clock.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/esphome/components/time/real_time_clock.cpp b/esphome/components/time/real_time_clock.cpp index 566344fa880..37344015187 100644 --- a/esphome/components/time/real_time_clock.cpp +++ b/esphome/components/time/real_time_clock.cpp @@ -88,13 +88,13 @@ void RealTimeClock::synchronize_epoch_(uint32_t epoch) { struct timeval timev { .tv_sec = static_cast(epoch), .tv_usec = 0, }; +#ifdef USE_ESP8266 + // ESP8266 settimeofday() requires tz to be nullptr + int ret = settimeofday(&timev, nullptr); +#else struct timezone tz = {0, 0}; int ret = settimeofday(&timev, &tz); - if (ret != 0 && errno == EINVAL) { - // Some ESP8266 frameworks abort when timezone parameter is not NULL - // while ESP32 expects it not to be NULL - ret = settimeofday(&timev, nullptr); - } +#endif if (ret != 0) { ESP_LOGW(TAG, "setimeofday() failed with code %d", ret);