Skip to content

Fix clock thread spinning at midnight due to missing tm_mday increment#369

Open
jamesmulcahy wants to merge 1 commit intoNSPManager:develfrom
jamesmulcahy:fix/clock-midnight-day-rollover
Open

Fix clock thread spinning at midnight due to missing tm_mday increment#369
jamesmulcahy wants to merge 1 commit intoNSPManager:develfrom
jamesmulcahy:fix/clock-midnight-day-rollover

Conversation

@jamesmulcahy
Copy link
Copy Markdown
Contributor

Summary

  • publish_time_and_date never incremented tm_mday when crossing midnight, so mktime normalised the struct back to midnight of the current day (already in the past) — causing sleep_until to return immediately and the date message to be published twice in quick succession each night before recovering
  • The existing manual rollover check (tm_mday >= 32) was unreachable dead code (tm_mday was never incremented) and would have been wrong anyway for February and 30-day months
  • Fix: increment tm_mday and remove the manual rollover logic; mktime is C-standard-specified to normalise out-of-range struct tm fields (e.g. January 32 → February 1, month 12 → January of next year) including leap-year-aware February, and is already called on the very next line

Test plan

  • Deploy and observe clock MQTT messages across a midnight boundary — expect exactly one publish per minute with no double-fire at 00:00
  • Verify correct date rollover at end of month (including February in a non-leap year) and end of year

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@tpanajott
Copy link
Copy Markdown
Collaborator

Thanks for this. I've spent a bit of time on this issues a few times but never quite got it to work as I wanted. Need to verify that it works as expected at midnight but should be fine as far as I understand it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants