Skip to content

Commit 7760d6c

Browse files
committed
Using getFormattedTime() to format a given time & setEpochTime() method
arduino-libraries#30 arduino-libraries#26
1 parent 47d0e56 commit 7760d6c

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

NTPClient.cpp

+6-2
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,8 @@ int NTPClient::getSeconds() {
150150
return (this->getEpochTime() % 60);
151151
}
152152

153-
String NTPClient::getFormattedTime() {
154-
unsigned long rawTime = this->getEpochTime();
153+
String NTPClient::getFormattedTime(unsigned long secs) {
154+
unsigned long rawTime = secs ? secs : this->getEpochTime();
155155
unsigned long hours = (rawTime % 86400L) / 3600;
156156
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
157157

@@ -199,3 +199,7 @@ void NTPClient::sendNTPPacket() {
199199
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
200200
this->_udp->endPacket();
201201
}
202+
203+
void NTPClient::setEpochTime(unsigned long secs) {
204+
this->_currentEpoc = secs;
205+
}

NTPClient.h

+8-3
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,9 @@ class NTPClient {
7676
void setUpdateInterval(unsigned long updateInterval);
7777

7878
/**
79-
* @return time formatted like `hh:mm:ss`
80-
*/
81-
String getFormattedTime();
79+
* @return secs argument (or 0 for current time) formatted like `hh:mm:ss`
80+
*/
81+
String getFormattedTime(unsigned long secs = 0);
8282

8383
/**
8484
* @return time in seconds since Jan. 1, 1970
@@ -89,4 +89,9 @@ class NTPClient {
8989
* Stops the underlying UDP client
9090
*/
9191
void end();
92+
93+
/**
94+
* Replace the NTP-fetched time with seconds since Jan. 1, 1970
95+
*/
96+
void setEpochTime(unsigned long secs);
9297
};

0 commit comments

Comments
 (0)