From 458765c8070d56f754138f2a674ebccd9ccfa036 Mon Sep 17 00:00:00 2001 From: Zenz Date: Mon, 18 Sep 2023 10:20:16 +0800 Subject: [PATCH 1/3] Update NtpClientLib.h fix uptime limited to max 49days issue. --- src/NtpClientLib.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/NtpClientLib.h b/src/NtpClientLib.h index 2d07631..e06f1f1 100644 --- a/src/NtpClientLib.h +++ b/src/NtpClientLib.h @@ -452,7 +452,7 @@ class NTPClient { int _longInterval = DEFAULT_NTP_INTERVAL; ///< Interval to set periodic time sync time_t _lastSyncd = 0; ///< Stored time of last successful sync time_t _firstSync = 0; ///< Stored time of first successful sync after boot - unsigned long _uptime = 0; ///< Time since boot + unsigned long long _uptime = 0; ///< Time since boot uint16_t ntpTimeout = 1500; ///< Response timeout for NTP requests onSyncEvent_t onSyncEvent; ///< Event handler callback From a9936642b41134c05ed9cd2e3dffeac3e2cd7bbd Mon Sep 17 00:00:00 2001 From: Zenz Date: Mon, 18 Sep 2023 11:11:52 +0800 Subject: [PATCH 2/3] Update NtpClientLib.h define a new var to save lastMillis, it's duty is to avoid millis() rollover. --- src/NtpClientLib.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/NtpClientLib.h b/src/NtpClientLib.h index e06f1f1..549f7d1 100644 --- a/src/NtpClientLib.h +++ b/src/NtpClientLib.h @@ -453,6 +453,7 @@ class NTPClient { time_t _lastSyncd = 0; ///< Stored time of last successful sync time_t _firstSync = 0; ///< Stored time of first successful sync after boot unsigned long long _uptime = 0; ///< Time since boot + unsigned long _lastMillis = 0; ///< defined to avoid millis() rollover uint16_t ntpTimeout = 1500; ///< Response timeout for NTP requests onSyncEvent_t onSyncEvent; ///< Event handler callback From cabf31ed3404723044f4fbc64dcaad08e56e03fd Mon Sep 17 00:00:00 2001 From: Zenz Date: Mon, 18 Sep 2023 11:14:03 +0800 Subject: [PATCH 3/3] Update NTPClientLib.cpp count uptime and avoid millis() rollover --- src/NTPClientLib.cpp | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/NTPClientLib.cpp b/src/NTPClientLib.cpp index 00b809c..b122b6e 100644 --- a/src/NTPClientLib.cpp +++ b/src/NTPClientLib.cpp @@ -612,7 +612,17 @@ void NTPClient::onNTPSyncEvent (onSyncEvent_t handler) { } time_t NTPClient::getUptime () { - _uptime = _uptime + (millis () - _uptime); + unsigned long long currentMillis = millis(); + if (currentMillis < _lastMillis) + { + // millis() has rolled over + _uptime += (ULONG_MAX - _lastMillis) + currentMillis; + } + else + { + _uptime += (currentMillis - _lastMillis); + } + _lastMillis = currentMillis; return _uptime / 1000; }