Skip to content

Commit 8288471

Browse files
committed
eliminate need for multiple calls to nondeterministic millis() when getFormattedTime()
1 parent d935c23 commit 8288471

File tree

2 files changed

+28
-8
lines changed

2 files changed

+28
-8
lines changed

NTPClient.cpp

+23-8
Original file line numberDiff line numberDiff line change
@@ -133,27 +133,42 @@ unsigned long NTPClient::getEpochTime() const {
133133
}
134134

135135
int NTPClient::getDay() const {
136-
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday
136+
return getDay(this->getEpochTime());
137+
}
138+
unsigned long NTPClient::getDay(unsigned long epochTime) const {
139+
return (((epochTime / 86400L) + 4 ) % 7); //0 is Sunday
137140
}
138141
int NTPClient::getHours() const {
139-
return ((this->getEpochTime() % 86400L) / 3600);
142+
return getHours(this->getEpochTime());
143+
}
144+
unsigned long NTPClient::getHours(unsigned long epochTime) const {
145+
return ((epochTime % 86400L) / 3600);
140146
}
141147
int NTPClient::getMinutes() const {
142-
return ((this->getEpochTime() % 3600) / 60);
148+
return getMinutes(this->getEpochTime());
149+
}
150+
unsigned long NTPClient::getMinutes(unsigned long epochTime) const {
151+
return ((epochTime % 3600) / 60);
143152
}
144153
int NTPClient::getSeconds() const {
145-
return (this->getEpochTime() % 60);
154+
return getSeconds(this->getEpochTime());
155+
}
156+
unsigned long NTPClient::getSeconds(unsigned long epochTime) const {
157+
return (epochTime % 60);
146158
}
147159

148160
String NTPClient::getFormattedTime() const {
149-
unsigned long rawTime = this->getEpochTime();
150-
unsigned long hours = (rawTime % 86400L) / 3600;
161+
return getFormattedTime(this->getEpochTime());
162+
}
163+
164+
String NTPClient::getFormattedTime(unsigned long rawTime) const {
165+
unsigned long hours = getHours(rawTime);
151166
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
152167

153-
unsigned long minutes = (rawTime % 3600) / 60;
168+
unsigned long minutes = getMinutes(rawTime);
154169
String minuteStr = minutes < 10 ? "0" + String(minutes) : String(minutes);
155170

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

159174
return hoursStr + ":" + minuteStr + ":" + secondStr;

NTPClient.h

+5
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,13 @@ class NTPClient {
7575
bool forceUpdate();
7676

7777
int getDay() const;
78+
unsigned long getDay(unsigned long epochTime) const;
7879
int getHours() const;
80+
unsigned long getHours(unsigned long epochTime) const;
7981
int getMinutes() const;
82+
unsigned long getMinutes(unsigned long epochTime) const;
8083
int getSeconds() const;
84+
unsigned long getSeconds(unsigned long epochTime) const;
8185

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

98103
/**
99104
* @return time in seconds since Jan. 1, 1970

0 commit comments

Comments
 (0)