Skip to content

Commit

Permalink
Eliminate need for multiple calls to nondeterministic millis() when g…
Browse files Browse the repository at this point in the history
…etting individual day/time components
  • Loading branch information
LeonPoon committed May 19, 2021
1 parent d935c23 commit 7c71102
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 8 deletions.
31 changes: 23 additions & 8 deletions NTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -133,27 +133,42 @@ unsigned long NTPClient::getEpochTime() 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 {
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 {
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 {
return (this->getEpochTime() % 60);
return getSeconds(this->getEpochTime());
}
unsigned long NTPClient::getSeconds(unsigned long epochTime) const {
return (epochTime % 60);
}

String NTPClient::getFormattedTime() const {
unsigned long rawTime = this->getEpochTime();
unsigned long hours = (rawTime % 86400L) / 3600;
return getFormattedTime(this->getEpochTime());
}

String NTPClient::getFormattedTime(unsigned long rawTime) const {
unsigned long hours = getHours(rawTime);
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);

unsigned long seconds = rawTime % 60;
unsigned long seconds = getSeconds(rawTime);
String secondStr = seconds < 10 ? "0" + String(seconds) : String(seconds);

return hoursStr + ":" + minuteStr + ":" + secondStr;
Expand Down
5 changes: 5 additions & 0 deletions NTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,13 @@ class NTPClient {
bool forceUpdate();

int getDay() const;
unsigned long getDay(unsigned long epochTime) const;
int getHours() const;
unsigned long getHours(unsigned long epochTime) const;
int getMinutes() const;
unsigned long getMinutes(unsigned long epochTime) const;
int getSeconds() const;
unsigned long getSeconds(unsigned long epochTime) const;

/**
* Changes the time offset. Useful for changing timezones dynamically
Expand All @@ -94,6 +98,7 @@ class NTPClient {
* @return time formatted like `hh:mm:ss`
*/
String getFormattedTime() const;
String getFormattedTime(unsigned long epochTime) const;

/**
* @return time in seconds since Jan. 1, 1970
Expand Down

0 comments on commit 7c71102

Please sign in to comment.