Skip to content

Commit 3fb7658

Browse files
committed
Accounting for fractional seconds
- Optionally collect the fractional part of the NTP response, calculate the millisecond offset that it indicates and use that in subsequent epoch values. - While we do not return time with more precision than one second, the time returned is still more accurate.
1 parent 894b21d commit 3fb7658

File tree

2 files changed

+25
-1
lines changed

2 files changed

+25
-1
lines changed

NTPClient.cpp

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,18 @@ bool NTPClient::forceUpdate() {
112112
// this is NTP time (seconds since Jan 1 1900):
113113
unsigned long secsSince1900 = highWord << 16 | lowWord;
114114

115+
if (this->_fractionalTime) {
116+
highWord = word(this->_packetBuffer[44], this->_packetBuffer[45]);
117+
lowWord = word(this->_packetBuffer[46], this->_packetBuffer[47]);
118+
119+
unsigned long fracSecs = highWord << 16 | lowWord;
120+
121+
// Convert the fractional part into ms
122+
this->_fracOffset = (unsigned long)(((double)fracSecs / 4294967295.0) * 1000.0);
123+
} else {
124+
this->_fracOffset = 0;
125+
}
126+
115127
this->_currentEpoc = secsSince1900 - SEVENZYYEARS;
116128

117129
return true; // return true after successful update
@@ -129,7 +141,7 @@ bool NTPClient::update() {
129141
unsigned long NTPClient::getEpochTime() const {
130142
return this->_timeOffset + // User offset
131143
this->_currentEpoc + // Epoc returned by the NTP server
132-
((millis() - this->_lastUpdate) / 1000); // Time since last update
144+
((this->_fracOffset + millis() - this->_lastUpdate) / 1000); // Time since last update
133145
}
134146

135147
int NTPClient::getDay() const {
@@ -173,6 +185,10 @@ void NTPClient::setUpdateInterval(unsigned long updateInterval) {
173185
this->_updateInterval = updateInterval;
174186
}
175187

188+
void NTPClient::setFractionalTime(bool enable) {
189+
this->_fractionalTime = enable;
190+
}
191+
176192
void NTPClient::setPoolServerName(const char* poolServerName) {
177193
this->_poolServerName = poolServerName;
178194
}

NTPClient.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ class NTPClient {
1717
IPAddress _poolServerIP;
1818
int _port = NTP_DEFAULT_LOCAL_PORT;
1919
long _timeOffset = 0;
20+
bool _fractionalTime = false;
2021

2122
unsigned long _updateInterval = 60000; // In ms
2223

2324
unsigned long _currentEpoc = 0; // In s
2425
unsigned long _lastUpdate = 0; // In ms
26+
unsigned long _fracOffset = 0; // In ms
2527

2628
byte _packetBuffer[NTP_PACKET_SIZE];
2729

@@ -85,6 +87,12 @@ class NTPClient {
8587
*/
8688
void setUpdateInterval(unsigned long updateInterval);
8789

90+
/** Enable or disable tracking fractional seconds from the NTP response.
91+
* We don't return time with that precision, but we do account for it when
92+
* working out what the time now is.
93+
*/
94+
void setFractionalTime(bool enable);
95+
8896
/**
8997
* @return time formatted like `hh:mm:ss`
9098
*/

0 commit comments

Comments
 (0)