NTPClient now returns date, year, month and day.

To get day of week now use getDayOfWeek()
This commit is contained in:
Mikhail Pershukov 2021-12-24 17:02:33 +03:00
parent 531eff39d9
commit 1fab9d7a24
3 changed files with 116 additions and 3 deletions

View File

@ -136,7 +136,7 @@ unsigned long NTPClient::getEpochTime() const {
((millis() - this->_lastUpdate) / 1000); // Time since last update
}
int NTPClient::getDay() const {
int NTPClient::getDayOfWeek() const {
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday
}
int NTPClient::getHours() const {
@ -149,6 +149,97 @@ int NTPClient::getSeconds() const {
return (this->getEpochTime() % 60);
}
int NTPClient::getDay() const {
long days = this->getEpochTime() / 86400L;
int fullYears = days / 365;
int overDays = days % 365;
int leapYears = (fullYears - 2) / 4;
if (leapYears > overDays) {
fullYears--;
}
int currentYear = 1970 + fullYears;
int thisYearIsLeap = currentYear % 4 == 0 ? 1 : 0;
int dayOfYear = (days - leapYears) % ( 365 + thisYearIsLeap);
if(dayOfYear == 0) {
dayOfYear = 365 + thisYearIsLeap;
}
int daysInMonth[12] = {31, 28 + thisYearIsLeap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for( int month = 0; month < 12; month++) {
if(dayOfYear < daysInMonth[month]) {
return dayOfYear;
} else {
dayOfYear -= daysInMonth[month];
}
}
return -1;
}
int NTPClient::getMonth() const {
long days = this->getEpochTime() / 86400L;
int fullYears = days / 365;
int overDays = days % 365;
int leapYears = (fullYears - 2) / 4;
if (leapYears > overDays) {
fullYears--;
}
int currentYear = 1970 + leapYears;
int thisYearIsLeap = currentYear % 4 == 0 ? 1 : 0;
int dayOfYear = (days - leapYears) % ( 365 + thisYearIsLeap);
if(dayOfYear == 0) {
dayOfYear = 365 + thisYearIsLeap;
}
int daysInMonth[12] = {31, 28 + thisYearIsLeap, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
for( int month = 0; month < 12; month++) {
if(dayOfYear < daysInMonth[month]) {
return month + 1;
} else {
dayOfYear -= daysInMonth[month];
}
}
return -1;
}
int NTPClient::getYear() const {
long days = this->getEpochTime() / 86400L;
int fullYears = days / 365;
int overDays = days % 365;
int leapYears = (fullYears - 2) / 4;
if (leapYears > overDays) {
fullYears--;
}
return 1970 + fullYears;
}
String NTPClient::getFormattedDate() const {
String yearStr = String(this->getYear());
unsigned int month = this->getMonth();
String monthStr = month < 10 ? "0" + String(month) : String(month);
unsigned int day = this->getDay();
String dayStr = day < 10 ? "0" + String(day) : String(day);
return yearStr + "-" + monthStr + "-" + dayStr;
}
String NTPClient::getFormattedTime() const {
unsigned long rawTime = this->getEpochTime();
unsigned long hours = (rawTime % 86400L) / 3600;
@ -163,6 +254,10 @@ String NTPClient::getFormattedTime() const {
return hoursStr + ":" + minuteStr + ":" + secondStr;
}
String NTPClient::getFormattedDateTime() const {
return this->getFormattedDate() + "T" + this->getFormattedTime();
}
void NTPClient::end() {
this->_udp->stop();

View File

@ -81,10 +81,13 @@ class NTPClient {
*/
bool isTimeSet() const;
int getDay() const;
int getHours() const;
int getMinutes() const;
int getSeconds() const;
int getDayOfWeek() const;
int getDay() const;
int getMonth() const;
int getYear() const;
/**
* Changes the time offset. Useful for changing timezones dynamically
@ -97,11 +100,21 @@ class NTPClient {
*/
void setUpdateInterval(unsigned long updateInterval);
/**
* @return date formatted like `YYYY-MM-DD`
*/
String getFormattedDate() const;
/**
* @return time formatted like `hh:mm:ss`
*/
String getFormattedTime() const;
/**
* @return datetime formatted like `YYYY-MM-DDTHH:mm:ss`
*/
String getFormattedDateTime() const;
/**
* @return time in seconds since Jan. 1, 1970
*/

View File

@ -13,11 +13,16 @@ end KEYWORD2
update KEYWORD2
forceUpdate KEYWORD2
isTimeSet KEYWORD2
getDay KEYWORD2
getHours KEYWORD2
getMinutes KEYWORD2
getSeconds KEYWORD2
getDayOfWeek KEYWORD2
getDay KEYWORD2
getMonth KEYWORD2
getYear KEYWORD2
getFormattedDate KEYWORD2
getFormattedTime KEYWORD2
getFormattedDateTime KEYWORD2
getEpochTime KEYWORD2
setTimeOffset KEYWORD2
setUpdateInterval KEYWORD2