Added Date and Time Seperated from T and Z

This commit is contained in:
vismay2303 2020-10-06 18:16:10 +05:30
parent d7d8b4512b
commit c1295a819b
2 changed files with 30 additions and 1 deletions

View File

@ -208,3 +208,27 @@ void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) {
randomSeed(analogRead(0)); randomSeed(analogRead(0));
this->_port = random(minValue, maxValue); this->_port = random(minValue, maxValue);
} }
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 + "T" + this->getFormattedTime(secs ? secs : 0) + "Z";
}

View File

@ -95,6 +95,11 @@ class NTPClient {
*/ */
String getFormattedTime() const; String getFormattedTime() const;
/**
* @return date and time seperated by T and Z obtained from the epoch time
*/
String getFormattedDate();
/** /**
* @return time in seconds since Jan. 1, 1970 * @return time in seconds since Jan. 1, 1970
*/ */