Skip to content

Commit

Permalink
Merge pull request #77 from TomsCircuits/master
Browse files Browse the repository at this point in the history
Fix for the year 2036 problem
  • Loading branch information
gmag11 authored Jan 4, 2019
2 parents fdc04d9 + 767cd04 commit 83650bc
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
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

0 comments on commit 83650bc

Please sign in to comment.