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; } diff --git a/src/NtpClientLib.h b/src/NtpClientLib.h index 2d07631..549f7d1 100644 --- a/src/NtpClientLib.h +++ b/src/NtpClientLib.h @@ -452,7 +452,8 @@ 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 + unsigned long _lastMillis = 0; ///< defined to avoid millis() rollover uint16_t ntpTimeout = 1500; ///< Response timeout for NTP requests onSyncEvent_t onSyncEvent; ///< Event handler callback