diff --git a/NTPClient.cpp b/NTPClient.cpp index b435855..c5c8c1b 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -130,7 +130,27 @@ bool NTPClient::isTimeSet() const { return (this->_lastUpdate != 0); // returns true if the time has been set, else false } +bool NTPClient::isTimeValid() const { + // First check if time has been set at all + if (!isTimeSet()) { + return false; + } + + // Then check if the epoch is reasonable (after Jan 1, 2000) + // This catches edge cases where time might be "set" but invalid + // 946684800 = Jan 1, 2000, 00:00:00 UTC + unsigned long epoch = getEpochTime(); + return (epoch > 946684800); +} + unsigned long NTPClient::getEpochTime() const { + // Return 0 if time has not been synchronized yet + // This prevents returning fabricated uptime values (millis()/1000) + + if (this->_lastUpdate == 0) { + return 0; + } + return this->_timeOffset + // User offset this->_currentEpoc + // Epoch returned by the NTP server ((millis() - this->_lastUpdate) / 1000); // Time since last update diff --git a/NTPClient.h b/NTPClient.h index a31d32f..1482a7e 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -81,6 +81,18 @@ class NTPClient { */ bool isTimeSet() const; + /** + * Checks if the time is both set AND valid (reasonable epoch value). + * This is a stronger check than isTimeSet() - it verifies the epoch is + * greater than a minimum threshold (Jan 1, 2000) to catch edge cases. + * + * Recommended for security-sensitive applications that need to ensure + * time is not only synchronized but also plausible. + * + * @return true if time is set and epoch > 946684800 (Jan 1, 2000), else false + */ + bool isTimeValid() const; + int getDay() const; int getHours() const; int getMinutes() const;