From addaf856e527ce306f334f46b06d9a2ba3f6fbdb Mon Sep 17 00:00:00 2001 From: 2EEEB <192235@vutbr.cz> Date: Fri, 19 Jun 2020 13:00:11 +0200 Subject: [PATCH] added option to configure parameters --- NTPClient.cpp | 59 ++++++++++++++++++++++++++++----------------------- NTPClient.h | 5 ++++- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/NTPClient.cpp b/NTPClient.cpp index 858d36a..ec4da3a 100644 --- a/NTPClient.cpp +++ b/NTPClient.cpp @@ -48,6 +48,11 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int u this->_updateInterval = updateInterval; } +void NTPClient::configure(const char* poolServerName, int timeOffset) { + this->_timeOffset = timeOffset; + this->_poolServerName = poolServerName; +} + void NTPClient::begin() { this->begin(NTP_DEFAULT_LOCAL_PORT); } @@ -120,33 +125,6 @@ int NTPClient::getSeconds() { return (this->getEpochTime() % 60); } -int NTPClient::getYear() { - time_t rawtime = this->getEpochTime(); - struct tm * ti; - ti = localtime (&rawtime); - int year = ti->tm_year + 1900; - - return year; -} - -int NTPClient::getMonth() { - time_t rawtime = this->getEpochTime(); - struct tm * ti; - ti = localtime (&rawtime); - int month = (ti->tm_mon + 1) < 10 ? 0 + (ti->tm_mon + 1) : (ti->tm_mon + 1); - - return month; -} - -int NTPClient::getDate() { - time_t rawtime = this->getEpochTime(); - struct tm * ti; - ti = localtime (&rawtime); - int day = (ti->tm_mday) < 10 ? 0 + (ti->tm_mday) : (ti->tm_mday); - - return day; -} - /* String NTPClient::getFormattedTime() { unsigned long rawTime = this->getEpochTime(); @@ -208,6 +186,33 @@ String NTPClient::getFullFormattedTime() { } */ +int NTPClient::getYear() { + time_t rawtime = this->getEpochTime(); + struct tm * ti; + ti = localtime (&rawtime); + int year = ti->tm_year + 1900; + + return year; +} + +int NTPClient::getMonth() { + time_t rawtime = this->getEpochTime(); + struct tm * ti; + ti = localtime (&rawtime); + int month = (ti->tm_mon + 1) < 10 ? 0 + (ti->tm_mon + 1) : (ti->tm_mon + 1); + + return month; +} + +int NTPClient::getDate() { + time_t rawtime = this->getEpochTime(); + struct tm * ti; + ti = localtime (&rawtime); + int day = (ti->tm_mday) < 10 ? 0 + (ti->tm_mday) : (ti->tm_mday); + + return day; +} + void NTPClient::end() { this->_udp->stop(); diff --git a/NTPClient.h b/NTPClient.h index 1cd7811..b9c9b44 100644 --- a/NTPClient.h +++ b/NTPClient.h @@ -33,7 +33,10 @@ class NTPClient { NTPClient(UDP& udp, const char* poolServerName); NTPClient(UDP& udp, const char* poolServerName, int timeOffset); NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval); - + /** + * Allow adding parameters outside of constructor + */ + void configure(const char* poolServerName, int timeOffset); /** * Starts the underlying UDP client with the default local port */