Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions NTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions NTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down