fix: robust 3h trend across non-monotonic epochs (NTP jumps)

Scan all samples for the newest one >=3h old instead of assuming
strictly increasing timestamps, so an NTP time correction no longer
breaks trend detection.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-17 21:27:10 +07:00
co-authored by Claude Opus 4.8
parent e29faf641a
commit 74121a2ef2
+6 -1
View File
@@ -26,10 +26,15 @@ Sample historyLatest() { return historyGet(s_count - 1); }
bool historyTrendDelta(float& outDelta) {
if (s_count < 2) return false;
Sample latest = historyLatest();
if (latest.epoch < 10800UL) return false;
uint32_t target = latest.epoch - 10800UL; // 3 h earlier
// Pick the newest sample at or before `target`. Scan all samples (do not
// assume epochs are strictly increasing: an NTP correction can shift them).
int idx = -1;
uint32_t bestEpoch = 0;
for (int i = 0; i < s_count; i++) {
if (historyGet(i).epoch <= target) idx = i; else break;
Sample s = historyGet(i);
if (s.epoch <= target && s.epoch >= bestEpoch) { bestEpoch = s.epoch; idx = i; }
}
if (idx < 0) return false;
Sample past = historyGet(idx);