Allow setting RTC clock backwards and fix elapsed-time underflow#1349
Open
weebl2000 wants to merge 1 commit intomeshcore-dev:devfrom
Open
Allow setting RTC clock backwards and fix elapsed-time underflow#1349weebl2000 wants to merge 1 commit intomeshcore-dev:devfrom
weebl2000 wants to merge 1 commit intomeshcore-dev:devfrom
Conversation
4d9e093 to
1cc133b
Compare
1cc133b to
6ffa6c9
Compare
Remove the "clock cannot go backwards" restriction from all set-time paths (CLI `time`, `clock sync`, companion radio CMD_SET_DEVICE_TIME, and simple_secure_chat). The ESP32-S3 RTC drifts 5-10% during deep sleep, making backwards correction necessary after even a few days. Add safeElapsedSecs() helper in ArduinoHelpers.h that clamps elapsed time to 0 when a stored timestamp appears to be in the future after a clock correction. Applied to: - Neighbor "heard X ago" displays in simple_repeater - UI time displays in companion_radio - TimeSeriesData calculations in simple_sensor Switch BaseChatMesh connection expiry from RTC timestamps to monotonic millis(), making it immune to RTC adjustments from GPS, NTP, or manual sync. Rename last_activity to last_activity_ms to reflect the change.
6ffa6c9 to
31e5e57
Compare
This was referenced Feb 26, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Build firmware: Build from this branch
Problem
The ESP32-S3 RTC drifts 5-10% during deep sleep. After a few days with powersaving ON, the clock can run hours ahead. Previously it was impossible to correct this — all set-time paths rejected timestamps older than the current time with "clock cannot go backwards".
Additionally, when the clock is corrected backwards (e.g. via GPS sync), stored timestamps appear to be in the future, causing unsigned subtraction underflow (wrapping to ~4 billion seconds / 136 years).
Changes
1. Remove forward-only clock restriction
All set-time paths now accept any timestamp:
time <epoch>andclock synccommands (CommonCLI.cpp)CMD_SET_DEVICE_TIME(companion_radio/MyMesh.cpp)simple_secure_chatsetClock()Only admins/owners can set the time, so replay risk is minimal.
2. Add
safeElapsedSecs()helperNew inline function in
ArduinoHelpers.hthat clamps elapsed time to 0 when a stored timestamp is in the future (i.e. clock was corrected backwards). Applied to:simple_repeatercompanion_radioTimeSeriesDataage calculations insimple_sensor3. Switch connection expiry to monotonic time
BaseChatMeshconnection expiry now usesmillis()instead of RTC timestamps, making it immune to clock adjustments from GPS, NTP, or manual sync. Field renamed fromlast_activitytolast_activity_msto reflect the semantic change.This assumes light sleep mode where
millis()continues to increment (deep sleep resets it, butBaseChatMeshis only used bycompanion_radiowhich uses light sleep).Trade-offs
clock sync. However, only authenticated admins/owners can issue these commands.safeElapsedSecs()clamps to 0 ("just now") rather than preserving relative ordering — acceptable for display purposes.