diff --git a/esphome/components/time/real_time_clock.cpp b/esphome/components/time/real_time_clock.cpp index cec9711dfb8..1d0695466b9 100644 --- a/esphome/components/time/real_time_clock.cpp +++ b/esphome/components/time/real_time_clock.cpp @@ -35,7 +35,8 @@ void RealTimeClock::synchronize_epoch_(uint32_t epoch) { // and prevent clock jumping backwards due to network latency auto current = this->utcnow(); if (current.is_valid()) { - int32_t diff = static_cast(epoch) - static_cast(current.timestamp); + // Unsigned subtraction handles wraparound correctly, then cast to signed + int32_t diff = static_cast(epoch - current.timestamp); if (diff >= -1 && diff <= 1) { return; }