Somtimes got times from 2036

* added some more checks for plausibility

[no-ticket]
This commit is contained in:
Christian Oelschlegel 2022-08-22 21:05:34 +02:00
parent a8eab3fc8b
commit 94de24746c

View File

@ -101,21 +101,33 @@ bool NTPClient::forceUpdate() {
if (timeout > 100) return false; // timeout after 1000 ms if (timeout > 100) return false; // timeout after 1000 ms
timeout++; timeout++;
} while (cb == 0); } while (cb == 0);
unsigned long newLastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time
this->_lastUpdate = millis() - (10 * (timeout + 1)); // Account for delay in reading the time int len = this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
if (len == 48) {
this->_udp->read(this->_packetBuffer, NTP_PACKET_SIZE);
unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]); unsigned long highWord = word(this->_packetBuffer[40], this->_packetBuffer[41]);
unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]); unsigned long lowWord = word(this->_packetBuffer[42], this->_packetBuffer[43]);
// combine the four bytes (two words) into a long integer // combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900): // this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord; unsigned long secsSince1900 = highWord << 16 | lowWord;
if (secsSince1900 > 0x8000000UL) {
this->_lastUpdate = newLastUpdate;
this->_currentEpoc = secsSince1900 - SEVENZYYEARS; this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
return true; // return true after successful update 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() { bool NTPClient::update() {
if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval if ((millis() - this->_lastUpdate >= this->_updateInterval) // Update after _updateInterval