From 74121a2ef2172c795c0ea9a7f5b357fea51989d1 Mon Sep 17 00:00:00 2001 From: Vitali Date: Fri, 17 Jul 2026 21:27:10 +0700 Subject: [PATCH] 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 --- WeatherPredictor/history.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/WeatherPredictor/history.cpp b/WeatherPredictor/history.cpp index 0df389d..c83dafb 100644 --- a/WeatherPredictor/history.cpp +++ b/WeatherPredictor/history.cpp @@ -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);