diff --git a/ampel-firmware/src/lib/NTPClient-master/NTPClient.cpp b/ampel-firmware/src/lib/NTPClient-master/NTPClient.cpp index b43585546967298b406758cfd5ade80b69695213..f663326189ad954a8c71ea6f1e184495a453fd69 100755 --- a/ampel-firmware/src/lib/NTPClient-master/NTPClient.cpp +++ b/ampel-firmware/src/lib/NTPClient-master/NTPClient.cpp @@ -210,3 +210,50 @@ void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) { randomSeed(analogRead(0)); this->_port = random(minValue, maxValue); } + + +/*** Custom code for ampel-firmware ***/ +void NTPClient::getFormattedTime(char *formatted_time, unsigned long secs) { + unsigned long rawTime = secs ? secs : this->getEpochTime(); + unsigned int hours = (rawTime % 86400L) / 3600; + unsigned int minutes = (rawTime % 3600) / 60; + unsigned int seconds = rawTime % 60; + + snprintf(formatted_time, 9, "%02d:%02d:%02d", hours, minutes, seconds); +} + +#define LEAP_YEAR(Y) ( (Y>0) && !(Y%4) && ( (Y%100) || !(Y%400) ) ) + +// Based on https://github.com/PaulStoffregen/Time/blob/master/Time.cpp +void NTPClient::getFormattedDate(char *formatted_date, unsigned long secs) { + unsigned long rawTime = (secs ? secs : 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; + } + month++; // jan is month 1 + rawTime++; // first day is day 1 + + char formatted_time[9]; + this->getFormattedTime(formatted_time, secs); + snprintf(formatted_date, 23, "%4lu-%02d-%02lu %s%+03d", year, month, rawTime, formatted_time, this->_timeOffset / 3600); +} + +void NTPClient::setEpochTime(unsigned long secs) { + this->_currentEpoc = secs; +} +/**************************************************************/ \ No newline at end of file diff --git a/ampel-firmware/src/lib/NTPClient-master/NTPClient.h b/ampel-firmware/src/lib/NTPClient-master/NTPClient.h index a31d32f56b47ebe309d0158768390863f1a7329a..fb23e46d4ef2e950720590a97aaf809a9ba0c2c7 100755 --- a/ampel-firmware/src/lib/NTPClient-master/NTPClient.h +++ b/ampel-firmware/src/lib/NTPClient-master/NTPClient.h @@ -111,4 +111,24 @@ class NTPClient { * Stops the underlying UDP client */ void end(); + +/*** Custom code for ampel-firmware ***/ + + /** + * @return secs argument (or 0 for current time) formatted like `hh:mm:ss` + */ + void getFormattedTime(char *formatted_time, unsigned long secs = 0); + + /** + * @return secs argument (or 0 for current date) formatted to ISO 8601 + * like `2004-02-12T15:19:21+00:00` + */ + void getFormattedDate(char *formatted_date, unsigned long secs = 0); + + /** + * Replace the NTP-fetched time with seconds since Jan. 1, 1970 + */ + void setEpochTime(unsigned long secs); + +/**************************************************************/ };