Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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