added option to configure parameters

This commit is contained in:
2EEEB 2020-06-19 13:00:11 +02:00
parent 105f127913
commit addaf856e5
Signed by: 2EEEB
GPG Key ID: 8F12BD6D9D435DB2
2 changed files with 36 additions and 28 deletions

View File

@ -48,6 +48,11 @@ NTPClient::NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int u
this->_updateInterval = updateInterval; this->_updateInterval = updateInterval;
} }
void NTPClient::configure(const char* poolServerName, int timeOffset) {
this->_timeOffset = timeOffset;
this->_poolServerName = poolServerName;
}
void NTPClient::begin() { void NTPClient::begin() {
this->begin(NTP_DEFAULT_LOCAL_PORT); this->begin(NTP_DEFAULT_LOCAL_PORT);
} }
@ -120,33 +125,6 @@ int NTPClient::getSeconds() {
return (this->getEpochTime() % 60); 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() { String NTPClient::getFormattedTime() {
unsigned long rawTime = this->getEpochTime(); 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() { void NTPClient::end() {
this->_udp->stop(); this->_udp->stop();

View File

@ -33,7 +33,10 @@ class NTPClient {
NTPClient(UDP& udp, const char* poolServerName); NTPClient(UDP& udp, const char* poolServerName);
NTPClient(UDP& udp, const char* poolServerName, int timeOffset); NTPClient(UDP& udp, const char* poolServerName, int timeOffset);
NTPClient(UDP& udp, const char* poolServerName, int timeOffset, int updateInterval); 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 * Starts the underlying UDP client with the default local port
*/ */