Skip to content

Commit

Permalink
Using getFormattedTime() to format a given time & setEpochTime() method
Browse files Browse the repository at this point in the history
  • Loading branch information
taranais committed Jul 31, 2017
1 parent 47d0e56 commit 7760d6c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
8 changes: 6 additions & 2 deletions NTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,8 @@ int NTPClient::getSeconds() {
return (this->getEpochTime() % 60);
}

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

Expand Down Expand Up @@ -199,3 +199,7 @@ void NTPClient::sendNTPPacket() {
this->_udp->write(this->_packetBuffer, NTP_PACKET_SIZE);
this->_udp->endPacket();
}

void NTPClient::setEpochTime(unsigned long secs) {
this->_currentEpoc = secs;
}
11 changes: 8 additions & 3 deletions NTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ class NTPClient {
void setUpdateInterval(unsigned long updateInterval);

/**
* @return time formatted like `hh:mm:ss`
*/
String getFormattedTime();
* @return secs argument (or 0 for current time) formatted like `hh:mm:ss`
*/
String getFormattedTime(unsigned long secs = 0);

/**
* @return time in seconds since Jan. 1, 1970
Expand All @@ -89,4 +89,9 @@ class NTPClient {
* Stops the underlying UDP client
*/
void end();

/**
* Replace the NTP-fetched time with seconds since Jan. 1, 1970
*/
void setEpochTime(unsigned long secs);
};

0 comments on commit 7760d6c

Please sign in to comment.