Eliminate need for multiple calls to nondeterministic millis() when getting individual day/time components

This commit is contained in:
Leon Poon 2021-05-19 12:10:04 +08:00
parent d935c237ab
commit 7c711026ca
2 changed files with 28 additions and 8 deletions

View File

@ -133,27 +133,42 @@ unsigned long NTPClient::getEpochTime() const {
} }
int NTPClient::getDay() const { int NTPClient::getDay() const {
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday return getDay(this->getEpochTime());
}
unsigned long NTPClient::getDay(unsigned long epochTime) const {
return (((epochTime / 86400L) + 4 ) % 7); //0 is Sunday
} }
int NTPClient::getHours() const { int NTPClient::getHours() const {
return ((this->getEpochTime() % 86400L) / 3600); return getHours(this->getEpochTime());
}
unsigned long NTPClient::getHours(unsigned long epochTime) const {
return ((epochTime % 86400L) / 3600);
} }
int NTPClient::getMinutes() const { int NTPClient::getMinutes() const {
return ((this->getEpochTime() % 3600) / 60); return getMinutes(this->getEpochTime());
}
unsigned long NTPClient::getMinutes(unsigned long epochTime) const {
return ((epochTime % 3600) / 60);
} }
int NTPClient::getSeconds() const { int NTPClient::getSeconds() const {
return (this->getEpochTime() % 60); return getSeconds(this->getEpochTime());
}
unsigned long NTPClient::getSeconds(unsigned long epochTime) const {
return (epochTime % 60);
} }
String NTPClient::getFormattedTime() const { String NTPClient::getFormattedTime() const {
unsigned long rawTime = this->getEpochTime(); return getFormattedTime(this->getEpochTime());
unsigned long hours = (rawTime % 86400L) / 3600; }
String NTPClient::getFormattedTime(unsigned long rawTime) const {
unsigned long hours = getHours(rawTime);
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours); String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
unsigned long minutes = (rawTime % 3600) / 60; unsigned long minutes = getMinutes(rawTime);
String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes); String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
unsigned long seconds = rawTime % 60; unsigned long seconds = getSeconds(rawTime);
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds); String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);
return hoursStr + ":" + minuteStr + ":" + secondStr; return hoursStr + ":" + minuteStr + ":" + secondStr;

View File

@ -75,9 +75,13 @@ class NTPClient {
bool forceUpdate(); bool forceUpdate();
int getDay() const; int getDay() const;
unsigned long getDay(unsigned long epochTime) const;
int getHours() const; int getHours() const;
unsigned long getHours(unsigned long epochTime) const;
int getMinutes() const; int getMinutes() const;
unsigned long getMinutes(unsigned long epochTime) const;
int getSeconds() const; int getSeconds() const;
unsigned long getSeconds(unsigned long epochTime) const;
/** /**
* Changes the time offset. Useful for changing timezones dynamically * Changes the time offset. Useful for changing timezones dynamically
@ -94,6 +98,7 @@ class NTPClient {
* @return time formatted like `hh:mm:ss` * @return time formatted like `hh:mm:ss`
*/ */
String getFormattedTime() const; String getFormattedTime() const;
String getFormattedTime(unsigned long epochTime) const;
/** /**
* @return time in seconds since Jan. 1, 1970 * @return time in seconds since Jan. 1, 1970