Skip to content

Commit 7f76e91

Browse files
authored
Update .cpp : Fix the Epoch time
Fix the epoch time by removing the time offset (by user) Come from PR#43 : arduino-libraries#43
1 parent 59cfb70 commit 7f76e91

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

NTPClient.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -127,26 +127,29 @@ bool NTPClient::update() {
127127
}
128128

129129
unsigned long NTPClient::getEpochTime() const {
130-
return this->_timeOffset + // User offset
131-
this->_currentEpoc + // Epoc returned by the NTP server
130+
return this->_currentEpoc + // Epoc returned by the NTP server
132131
((millis() - this->_lastUpdate) / 1000); // Time since last update
133132
}
134133

135134
int NTPClient::getDay() const {
136-
return (((this->getEpochTime() / 86400L) + 4 ) % 7); //0 is Sunday
135+
unsigned long timeWithOffset = this->getEpochTime() + this->_timeOffset; //User offset
136+
return (((timeWithOffset / 86400L) + 4 ) % 7); //0 is Sunday
137137
}
138138
int NTPClient::getHours() const {
139-
return ((this->getEpochTime() % 86400L) / 3600);
139+
unsigned long timeWithOffset = this->getEpochTime() + this->_timeOffset; //User offset
140+
return ((timeWithOffset % 86400L) / 3600);
140141
}
141142
int NTPClient::getMinutes() const {
142-
return ((this->getEpochTime() % 3600) / 60);
143+
unsigned long timeWithOffset = this->getEpochTime() + this->_timeOffset; //User offset
144+
return ((timeWithOffset % 3600) / 60);
143145
}
144146
int NTPClient::getSeconds() const {
145-
return (this->getEpochTime() % 60);
147+
unsigned long timeWithOffset = this->getEpochTime() + this->_timeOffset; //User offset
148+
return (timeWithOffset % 60);
146149
}
147150

148151
String NTPClient::getFormattedTime() const {
149-
unsigned long rawTime = this->getEpochTime();
152+
unsigned long rawTime = this->getEpochTime() + this->_timeOffset; //User offset
150153
unsigned long hours = (rawTime % 86400L) / 3600;
151154
String hoursStr = hours < 10 ? "0" + String(hours) : String(hours);
152155

@@ -207,4 +210,4 @@ void NTPClient::sendNTPPacket() {
207210
void NTPClient::setRandomPort(unsigned int minValue, unsigned int maxValue) {
208211
randomSeed(analogRead(0));
209212
this->_port = random(minValue, maxValue);
210-
}
213+
}

0 commit comments

Comments
 (0)