From c1295a819b7c00c0e7247c492862427b9e72ce6e Mon Sep 17 00:00:00 2001 From: vismay2303 Date: Tue, 6 Oct 2020 18:16:10 +0530 Subject: [PATCH] Added Date and Time Seperated from T and Z --- NTPClient.cpp | 26 +++++++++++++++++++++++++- NTPClient.h | 5 +++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index 760e142..327680c 100755 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -207,4 +207,28 @@ void NTPClient::sendNTPPacket() { void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) { randomSeed(analogRead(0)); this->_port = random(minValue, maxValue); -} \ No newline at end of file +} +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"; +} diff --git a/NTPClient.h b/NTPClient.h index b518c28..2eb6cf0 100755 --- a/NTPClient.h +++ b/NTPClient.h @@ -94,6 +94,11 @@ class NTPClient { * @return time formatted like `hh:mm:ss` */ 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