Skip to content

Commit 975ca91

Browse files
Update NTPClient.cpp
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
1 parent d7d8b45 commit 975ca91

File tree

1 file changed

+74
-1
lines changed

1 file changed

+74
-1
lines changed

Diff for: NTPClient.cpp

+74-1
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,79 @@ String NTPClient::getFormattedTime() const {
159159
return hoursStr + ":" + minuteStr + ":" + secondStr;
160160
}
161161

162+
bool LEAP_YEAR(int y) {
163+
164+
if(y % 4 == 0)
165+
{
166+
//Nested if else
167+
if( y % 100 == 0)
168+
{
169+
if ( y % 400 == 0)
170+
return true;
171+
//printf("%d is a Leap Year", y);
172+
//else
173+
//printf("%d is not a Leap Year", y);
174+
}
175+
else
176+
return true;
177+
//printf("%d is a Leap Year", y );
178+
}
179+
else
180+
//printf("%d is not a Leap Year", y);
181+
return false;
182+
183+
}
184+
185+
String NTPClient::getFormattedDate() {
186+
unsigned long rawTime = this->getEpochTime() / 86400L; // in days
187+
unsigned long days = 0, year = 1970;
188+
uint8_t month;
189+
static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
190+
191+
while((days += (LEAP_YEAR(year) ? 366 : 365)) <= rawTime)
192+
year++;
193+
rawTime -= days - (LEAP_YEAR(year) ? 366 : 365); // now it is days in this year, starting at 0
194+
days=0;
195+
for (month=0; month<12; month++) {
196+
uint8_t monthLength;
197+
if (month==1) { // february
198+
monthLength = LEAP_YEAR(year) ? 29 : 28;
199+
} else {
200+
monthLength = monthDays[month];
201+
}
202+
if (rawTime < monthLength) break;
203+
rawTime -= monthLength;
204+
}
205+
String monthStr = ++month < 10 ? "0" + String(month) : String(month); // jan is month 1
206+
String dayStr = ++rawTime < 10 ? "0" + String(rawTime) : String(rawTime); // day of month
207+
return String(year) + "-" + monthStr + "-" + dayStr;
208+
}
209+
210+
String NTPClient::getFormattedUTC() {
211+
unsigned long rawTime = this->getEpochTime() / 86400L; // in days
212+
unsigned long days = 0, year = 1970;
213+
uint8_t month;
214+
static const uint8_t monthDays[]={31,28,31,30,31,30,31,31,30,31,30,31};
215+
216+
while((days += (LEAP_YEAR(year) ? 366 : 365)) <= rawTime)
217+
year++;
218+
rawTime -= days - (LEAP_YEAR(year) ? 366 : 365); // now it is days in this year, starting at 0
219+
days=0;
220+
for (month=0; month<12; month++) {
221+
uint8_t monthLength;
222+
if (month==1) { // february
223+
monthLength = LEAP_YEAR(year) ? 29 : 28;
224+
} else {
225+
monthLength = monthDays[month];
226+
}
227+
if (rawTime < monthLength) break;
228+
rawTime -= monthLength;
229+
}
230+
String monthStr = ++month < 10 ? "0" + String(month) : String(month); // jan is month 1
231+
String dayStr = ++rawTime < 10 ? "0" + String(rawTime) : String(rawTime); // day of month
232+
return String(year) + "-" + monthStr + "-" + dayStr + "T" + getFormattedTime() + "Z";
233+
}
234+
162235
void NTPClient::end() {
163236
this->_udp->stop();
164237

@@ -207,4 +280,4 @@ void NTPClient::sendNTPPacket() {
207280
void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) {
208281
randomSeed(analogRead(0));
209282
this->_port = random(minValue, maxValue);
210-
}
283+
}

0 commit comments

Comments
 (0)