From d1fabd9580961e2881d84eafcc95904a2f848656 Mon Sep 17 00:00:00 2001 From: James Mulcahy Date: Thu, 30 Apr 2026 22:36:50 -0700 Subject: [PATCH] Fix clock thread spinning at midnight due to missing tm_mday increment MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When the publish_time_and_date loop crossed midnight, tm_hour was reset to 0 but tm_mday was never incremented. The subsequent mktime call then normalised the struct to midnight of the *current* day (already in the past), so sleep_until returned immediately. The thread would spin an extra iteration around midnight, publishing the date message twice in quick succession before recovering on the next pass. The existing manual month/year rollover check (tm_mday >= 32) was also incorrect: it would miss month boundaries for February and months with 30 days, since it never incremented tm_mday in the first place. Fix: increment tm_mday and remove the manual rollover logic. mktime is specified by the C standard to normalise out-of-range struct tm fields (e.g. January 32 → February 1, month 12 → January of next year) and is leap-year-aware, so calling it already on the next line is sufficient. Co-Authored-By: Claude Sonnet 4.6 --- docker/MQTTManager/src/main.cpp | 13 +++---------- 1 file changed, 3 insertions(+), 10 deletions(-) diff --git a/docker/MQTTManager/src/main.cpp b/docker/MQTTManager/src/main.cpp index 18ad27bb..396227bb 100644 --- a/docker/MQTTManager/src/main.cpp +++ b/docker/MQTTManager/src/main.cpp @@ -98,16 +98,9 @@ void publish_time_and_date() { next_minute_tm.tm_hour++; if (next_minute_tm.tm_hour >= 24) { // We went over to next day next_minute_tm.tm_hour = 0; - - // Handle day change - if (next_minute_tm.tm_mday >= 32) { // We went over to next month - next_minute_tm.tm_mday = 1; - next_minute_tm.tm_mon++; - if (next_minute_tm.tm_mon >= 12) { // We went over to next year - next_minute_tm.tm_mon = 0; - next_minute_tm.tm_year++; - } - } + next_minute_tm.tm_mday++; + // mktime (called below) normalises month/year overflow and handles + // varying month lengths and leap years, so no manual rollover needed. } }