Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for the year 2036 problem #77

Merged
merged 11 commits into from
Jan 4, 2019
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,5 @@ examples/NTPClient/__vm/

# Visual Studio Code workspace folder
.vscode/
*.bak
**/__vm/**
16 changes: 16 additions & 0 deletions src/NTPClientLib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ time_t NTPClient::getTime () {
DEBUGLOG ("-- Receive NTP Response\n");
udp->read (ntpPacketBuffer, NTP_PACKET_SIZE); // read packet into the buffer
time_t timeValue = decodeNtpMessage (ntpPacketBuffer);
if (timeValue != 0) {
setSyncInterval (getLongInterval ());
if (!_firstSync) {
// if (timeStatus () == timeSet)
Expand All @@ -170,6 +171,15 @@ time_t NTPClient::getTime () {
if (onSyncEvent)
onSyncEvent (timeSyncd);
return timeValue;
}
else {
DEBUGLOG ("-- No valid NTP data :-(\n");
udp->stop ();
setSyncInterval (getShortInterval ()); // Retry connection more often
if (onSyncEvent)
onSyncEvent (noResponse);
return 0; // return 0 if unable to get the time
}
}
#ifdef ARDUINO_ARCH_ESP8266
ESP.wdtFeed ();
Expand Down Expand Up @@ -603,6 +613,12 @@ time_t NTPClient::decodeNtpMessage (uint8_t *messageBuffer) {
secsSince1900 |= (unsigned long)messageBuffer[42] << 8;
secsSince1900 |= (unsigned long)messageBuffer[43];

DEBUGLOG("Secs: %u \n", secsSince1900);

if(secsSince1900 == 0) {
DEBUGLOG ("--Timestamp is Zero\n");
return 0;
}
#define SEVENTY_YEARS 2208988800UL
time_t timeTemp = secsSince1900 - SEVENTY_YEARS + _timeZone * SECS_PER_HOUR + _minutesOffset * SECS_PER_MIN;

Expand Down