Skip to content

Commit

Permalink
Update NTPClient.cpp
Browse files Browse the repository at this point in the history
Sorry I don't know if this is the correct format for submitting proposals. I seem to have this working now. I added a function LEAP_YEAR, getFormattedUTC and getFormattedDate. The getFormattedDate was from vismay2303 on arduino-libraries#119.  I have updates for NTPClient.h and keywords.txt
  • Loading branch information
DataDanCalgary authored Jan 14, 2021
1 parent d7d8b45 commit 975ca91
Showing 1 changed file with 74 additions and 1 deletion.
75 changes: 74 additions & 1 deletion NTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,79 @@ String NTPClient::getFormattedTime() const {
return hoursStr + ":" + minuteStr + ":" + secondStr;
}

bool LEAP_YEAR(int y) {

if(y % 4 == 0)
{
//Nested if else
if( y % 100 == 0)
{
if ( y % 400 == 0)
return true;
//printf("%d is a Leap Year", y);
//else
//printf("%d is not a Leap Year", y);
}
else
return true;
//printf("%d is a Leap Year", y );
}
else
//printf("%d is not a Leap Year", y);
return false;

}

String NTPClient::getFormattedDate() {
unsigned long rawTime = this->getEpochTime() / 86400L; // in days
unsigned long days = 0, year = 1970;
uint8_t month;
static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31};

while((days += (LEAP_YEAR(year) ? 366 : 365)) <= rawTime)
year++;
rawTime -= days - (LEAP_YEAR(year) ? 366 : 365); // now it is days in this year, starting at 0
days=0;
for (month=0; month<12; month++) {
uint8_t monthLength;
if (month==1) { // february
monthLength = LEAP_YEAR(year) ? 29 : 28;
} else {
monthLength = monthDays[month];
}
if (rawTime < monthLength) break;
rawTime -= monthLength;
}
String monthStr = ++month < 10 ? "0" + String(month) : String(month); // jan is month 1
String dayStr = ++rawTime < 10 ? "0" + String(rawTime) : String(rawTime); // day of month
return String(year) + "-" + monthStr + "-" + dayStr;
}

String NTPClient::getFormattedUTC() {
unsigned long rawTime = this->getEpochTime() / 86400L; // in days
unsigned long days = 0, year = 1970;
uint8_t month;
static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31};

while((days += (LEAP_YEAR(year) ? 366 : 365)) <= rawTime)
year++;
rawTime -= days - (LEAP_YEAR(year) ? 366 : 365); // now it is days in this year, starting at 0
days=0;
for (month=0; month<12; month++) {
uint8_t monthLength;
if (month==1) { // february
monthLength = LEAP_YEAR(year) ? 29 : 28;
} else {
monthLength = monthDays[month];
}
if (rawTime < monthLength) break;
rawTime -= monthLength;
}
String monthStr = ++month < 10 ? "0" + String(month) : String(month); // jan is month 1
String dayStr = ++rawTime < 10 ? "0" + String(rawTime) : String(rawTime); // day of month
return String(year) + "-" + monthStr + "-" + dayStr + "T" + getFormattedTime() + "Z";
}

void NTPClient::end() {
this->_udp->stop();

Expand Down Expand Up @@ -207,4 +280,4 @@ void NTPClient::sendNTPPacket() {
void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) {
randomSeed(analogRead(0));
this->_port = random(minValue, maxValue);
}
}

0 comments on commit 975ca91

Please sign in to comment.