@@ -126,10 +126,47 @@ bool NTPClient::update() {
126
126
return false ; // return false if update does not occur
127
127
}
128
128
129
+ int NTPClient::asyncUpdate () {
130
+ if ((millis () - this ->_sendTime >= this ->_updateInterval ) // Update after _updateInterval
131
+ || this ->_sendTime == 0 ) { // Update if there was no update yet.
132
+ _sendTime = millis ();
133
+ if (!this ->_udpSetup || this ->_port != NTP_DEFAULT_LOCAL_PORT) this ->begin (this ->_port ); // setup the UDP client if needed
134
+ this ->sendNTPPacket ();
135
+
136
+ int code = 2 ;
137
+ if (_needUpdate) code = -1 ; // timeout
138
+ _needUpdate = true ;
139
+ return code;
140
+ }
141
+
142
+ if (_needUpdate) {
143
+ int cb = this ->_udp ->parsePacket ();
144
+ if (cb == 0 ) return 2 ;
145
+ this ->_udp ->read (this ->_packetBuffer , NTP_PACKET_SIZE);
146
+
147
+ unsigned long highWord = word (this ->_packetBuffer [40 ], this ->_packetBuffer [41 ]);
148
+ unsigned long lowWord = word (this ->_packetBuffer [42 ], this ->_packetBuffer [43 ]);
149
+ // combine the four bytes (two words) into a long integer
150
+ // this is NTP time (seconds since Jan 1 1900):
151
+ unsigned long secsSince1900 = highWord << 16 | lowWord;
152
+
153
+ this ->_currentEpoc = secsSince1900 - SEVENZYYEARS;
154
+ this ->_lastUpdate = millis ();
155
+ _needUpdate = false ;
156
+ return 0 ;
157
+ }
158
+
159
+ return 1 ; // return false if update does not occur
160
+ }
161
+
129
162
bool NTPClient::isTimeSet () const {
130
163
return (this ->_lastUpdate != 0 ); // returns true if the time has been set, else false
131
164
}
132
165
166
+ long long NTPClient::getEpochTimeMillis () const {
167
+ return this ->_currentEpoc * 1000LL + millis () - this ->_lastUpdate ;
168
+ }
169
+
133
170
unsigned long NTPClient::getEpochTime () const {
134
171
return this ->_timeOffset + // User offset
135
172
this ->_currentEpoc + // Epoch returned by the NTP server
0 commit comments