diff --git a/NTPClient.cpp b/NTPClient.cpp index 15dc9e4..07adce7 100644 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -25,7 +25,7 @@ NTPClient::NTPClient(UDP& udp) { this->_udp = &udp; } -NTPClient::NTPClient(UDP& udp, int timeOffset) { +NTPClient::NTPClient(UDP& udp, long timeOffset) { this->_udp = &udp; this->_timeOffset = timeOffset; } @@ -35,13 +35,13 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName) { this->_poolServerName = poolServerName; } -NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset) { +NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset) { this->_udp = &udp; this->_timeOffset = timeOffset; this->_poolServerName = poolServerName; } -NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval) { +NTPClient::NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval) { this->_udp = &udp; this->_timeOffset = timeOffset; this->_poolServerName = poolServerName; @@ -135,7 +135,7 @@ bool NTPClient::updated() { return (_currentEpoc != 0); } -unsigned long NTPClient::getEpochTime() { +unsigned long NTPClient::getEpochTime() const { return this->_timeOffset + // User offset this->_currentEpoc + // Epoc returned by the NTP server ((millis() - this->_lastUpdate + (_currentFraction / FRACTIONSPERMILLI)) / 1000); // Time since last update @@ -153,20 +153,20 @@ unsigned long long NTPClient::getEpochMillis() { return epoch; } -int NTPClient::getDay() { +int NTPClient::getDay() const { return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday } -int NTPClient::getHours() { +int NTPClient::getHours() const { return ((this->getEpochTime() % 86400L) / 3600); } -int NTPClient::getMinutes() { +int NTPClient::getMinutes() const { return ((this->getEpochTime() % 3600) / 60); } -int NTPClient::getSeconds() { +int NTPClient::getSeconds() const { return (this->getEpochTime() % 60); } -String NTPClient::getFormattedTime() { +String NTPClient::getFormattedTime() const { unsigned long rawTime = this->getEpochTime(); unsigned long hours = (rawTime % 86400L) / 3600; String hoursStr = hours < 10 ? "0" + String(hours) : String(hours); @@ -190,7 +190,7 @@ void NTPClient::setTimeOffset(int timeOffset) { this->_timeOffset = timeOffset; } -void NTPClient::setUpdateInterval(int updateInterval) { +void NTPClient::setUpdateInterval(unsigned long updateInterval) { this->_updateInterval = updateInterval; } @@ -202,6 +202,10 @@ void NTPClient::setUpdateCallback(NTPUpdateCallbackFunction f) { _updateCallback = f; } +void NTPClient::setPoolServerName(const char* poolServerName) { + this->_poolServerName = poolServerName; +} + void NTPClient::sendNTPPacket() { // set all bytes in the buffer to 0 memset(this->_packetBuffer, 0, NTP_PACKET_SIZE); diff --git a/NTPClient.h b/NTPClient.h index 8163ba1..94470cc 100644 --- a/NTPClient.h +++ b/NTPClient.h @@ -18,12 +18,12 @@ class NTPClient { UDP* _udp; bool _udpSetup = false; - const char* _poolServerName = "time.nist.gov"; // Default time server + const char* _poolServerName = "pool.ntp.org"; // Default time server int _port = NTP_DEFAULT_LOCAL_PORT; - int _timeOffset = 0; + long _timeOffset = 0; - unsigned int _updateInterval = 60000; // In ms unsigned int _retryInterval = 1000; // In ms + unsigned long _updateInterval = 60000; // In ms unsigned long _currentEpoc = 0; // In s unsigned long _currentFraction = 0; // In 1/(2^32) s @@ -39,10 +39,17 @@ class NTPClient { public: NTPClient(UDP& udp); - NTPClient(UDP& udp, int timeOffset); + NTPClient(UDP& udp, long timeOffset); NTPClient(UDP& udp, const char* poolServerName); - NTPClient(UDP& udp, const char* poolServerName, int timeOffset); - NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval); + NTPClient(UDP& udp, const char* poolServerName, long timeOffset); + NTPClient(UDP& udp, const char* poolServerName, long timeOffset, unsigned long updateInterval); + + /** + * Set time server name + * + * @param poolServerName + */ + void setPoolServerName(const char* poolServerName); /** * Starts the underlying UDP client with the default local port @@ -81,10 +88,10 @@ class NTPClient { */ bool forceUpdate(); - int getDay(); - int getHours(); - int getMinutes(); - int getSeconds(); + int getDay() const; + int getHours() const; + int getMinutes() const; + int getSeconds() const; /** * Changes the time offset. Useful for changing timezones dynamically @@ -95,7 +102,7 @@ class NTPClient { * Set the update interval to another frequency. E.g. useful when the * timeOffset should not be set in the constructor */ - void setUpdateInterval(int updateInterval); + void setUpdateInterval(unsigned long updateInterval); /** * Set the retry interval to another frequency in ms @@ -105,12 +112,12 @@ class NTPClient { /** * @return time formatted like `hh:mm:ss` */ - String getFormattedTime(); + String getFormattedTime() const; /** * @return time in seconds since Jan. 1, 1970 */ - unsigned long getEpochTime(); + unsigned long getEpochTime() const; /** * @return time in milliseconds since Jan. 1, 1970 diff --git a/README.md b/README.md index af9802a..6c8c07a 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ const char *password = ""; WiFiUDP ntpUDP; -// By default 'time.nist.gov' is used with 60 seconds update interval and +// By default 'pool.ntp.org' is used with 60 seconds update interval and // no offset NTPClient timeClient(ntpUDP); diff --git a/keywords.txt b/keywords.txt index 1430d04..6f7bc6f 100644 --- a/keywords.txt +++ b/keywords.txt @@ -18,3 +18,6 @@ getMinutes KEYWORD2 getSeconds KEYWORD2 getFormattedTime KEYWORD2 getEpochTime KEYWORD2 +setTimeOffset KEYWORD2 +setUpdateInterval KEYWORD2 +setPoolServerName KEYWORD2