From a8eab3fc8b9290bebeb9d71186606283ae5c1095 Mon Sep 17 00:00:00 2001 From: Christian Oelschlegel <18003795+oelison@users.noreply.github.com> Date: Sat, 4 Jun 2022 12:35:16 +0200 Subject: [PATCH 1/2] NTPClient decode also extern epoch time overloaded function getDay, getHours, getMinutes and getSeconds with extern epoch time as argument added. --- NTPClient.cpp | 13 +++++++++++++ NTPClient.h | 6 +++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index b435855..cc1f006 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -148,6 +148,19 @@ int NTPClient::getMinutes() const { int NTPClient::getSeconds() const { return (this->getEpochTime() % 60); } +// functions for decode extern epoch time +int NTPClient::getDay(unsigned long epochTime) const { + return (((epochTime / 86400L) + 4 ) % 7); //0 is Sunday +} +int NTPClient::getHours(unsigned long epochTime) const { + return ((epochTime % 86400L) / 3600); +} +int NTPClient::getMinutes(unsigned long epochTime) const { + return ((epochTime % 3600) / 60); +} +int NTPClient::getSeconds(unsigned long epochTime) const { + return (epochTime % 60); +} String NTPClient::getFormattedTime() const { unsigned long rawTime = this->getEpochTime(); diff --git a/NTPClient.h b/NTPClient.h index a31d32f..8df5321 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -85,7 +85,11 @@ class NTPClient { int getHours() const; int getMinutes() const; int getSeconds() const; - + int getDay(unsigned long epochTime) const; + int getHours(unsigned long epochTime) const; + int getMinutes(unsigned long epochTime) const; + int getSeconds(unsigned long epochTime) const; + /** * Changes the time offset. Useful for changing timezones dynamically */ From 94de24746c334a9ab7cf61733a1f6222052c775c Mon Sep 17 00:00:00 2001 From: Christian Oelschlegel <18003795+oelison@users.noreply.github.com> Date: Mon, 22 Aug 2022 21:05:34 +0200 Subject: [PATCH 2/2] Somtimes got times from 2036 * added some more checks for plausibility [no-ticket] --- NTPClient.cpp | 40 ++++++++++++++++++++++++++-------------- 1 file changed, 26 insertions(+), 14 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index cc1f006..61b48f2 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -101,20 +101,32 @@ bool NTPClient::forceUpdate() { if (timeout > 100) return false; // timeout after 1000 ms timeout++; } while (cb == 0); - - this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time - - this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE); - - unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]); - unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]); - // combine the four bytes (two words) into a long integer - // this is NTP time (seconds since Jan 1 1900): - unsigned long secsSince1900 = highWord << 16 | lowWord; - - this->_currentEpoc = secsSince1900 - SEVENZYYEARS; - - return true; // return true after successful update + unsigned long newLastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time + + int len = this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE); + if (len == 48) { + unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]); + unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]); + // combine the four bytes (two words) into a long integer + // this is NTP time (seconds since Jan 1 1900): + unsigned long secsSince1900 = highWord << 16 | lowWord; + if (secsSince1900 > 0x8000000UL) { + this->_lastUpdate = newLastUpdate; + this->_currentEpoc = secsSince1900 - SEVENZYYEARS; + return true; // return true after successful update + } + else { + #ifdef DEBUG_NTPClient + Serial.println("NTP Server time too far in the past"); + #endif + } + } + else { + #ifdef DEBUG_NTPClient + Serial.println("NTP Server message lenght not 48"); + #endif + } + return false; } bool NTPClient::update() {