@@ -141,12 +141,21 @@ unsigned long TimeServiceClass::getTime()
141
141
unsigned long const current_tick = millis ();
142
142
bool const is_ntp_sync_timeout = (current_tick - _last_sync_tick) > _sync_interval_ms;
143
143
if (!_is_rtc_configured || is_ntp_sync_timeout) {
144
+ /* Try to sync time from NTP or connection handler */
144
145
sync ();
145
146
}
146
147
147
- /* Read time from RTC */
148
- unsigned long utc = getRTC ();
149
- return isTimeValid (utc) ? utc : EPOCH_AT_COMPILE_TIME;
148
+ /* Use RTC time if has been configured at least once */
149
+ if (_last_sync_tick) {
150
+ return getRTC ();
151
+ }
152
+
153
+ /* Return the epoch timestamp at compile time as a last line of defense
154
+ * trying to connect to the server. Otherwise the certificate expiration
155
+ * date is wrong and we'll be unable to establish a connection. Schedulers
156
+ * won't work correctly using this value.
157
+ */
158
+ return EPOCH_AT_COMPILE_TIME;
150
159
}
151
160
152
161
void TimeServiceClass::setTime (unsigned long time)
@@ -158,20 +167,20 @@ bool TimeServiceClass::sync()
158
167
{
159
168
_is_rtc_configured = false ;
160
169
161
- unsigned long utc = EPOCH_AT_COMPILE_TIME ;
170
+ unsigned long utc = EPOCH ;
162
171
if (_sync_func) {
163
172
utc = _sync_func ();
164
173
} else {
165
174
#if defined(HAS_NOTECARD) || defined(HAS_TCP)
166
175
utc = getRemoteTime ();
167
176
#elif defined(HAS_LORA)
168
- /* Just keep incrementing stored RTC value*/
177
+ /* Just keep incrementing stored RTC value starting from EPOCH_AT_COMPILE_TIME */
169
178
utc = getRTC ();
170
179
#endif
171
180
}
172
181
173
182
if (isTimeValid (utc)) {
174
- DEBUG_DEBUG (" TimeServiceClass::%s Drift: %d RTC value: %u" , __FUNCTION__, getRTC () - utc, utc);
183
+ DEBUG_DEBUG (" TimeServiceClass::%s done. Drift: %d RTC value: %u" , __FUNCTION__, getRTC () - utc, utc);
175
184
setRTC (utc);
176
185
_last_sync_tick = millis ();
177
186
_is_rtc_configured = true ;
@@ -299,6 +308,7 @@ unsigned long TimeServiceClass::getRemoteTime()
299
308
return ntp_time;
300
309
}
301
310
}
311
+ DEBUG_WARNING (" TimeServiceClass::%s cannot get time from NTP, fallback on connection handler" , __FUNCTION__);
302
312
#endif /* HAS_TCP */
303
313
304
314
/* As fallback if NTP request fails try to obtain the
@@ -308,21 +318,21 @@ unsigned long TimeServiceClass::getRemoteTime()
308
318
if (isTimeValid (connection_time)) {
309
319
return connection_time;
310
320
}
321
+ DEBUG_WARNING (" TimeServiceClass::%s cannot get time from connection handler" , __FUNCTION__);
311
322
}
312
323
313
- /* Return the epoch timestamp at compile time as a last
314
- * line of defense. Otherwise the certificate expiration
315
- * date is wrong and we'll be unable to establish a connection
316
- * to the server.
317
- */
318
- return EPOCH_AT_COMPILE_TIME;
324
+ /* Return known invalid value because we are not connected */
325
+ return EPOCH;
319
326
}
320
327
321
328
#endif /* HAS_NOTECARD || HAS_TCP */
322
329
323
330
bool TimeServiceClass::isTimeValid (unsigned long const time)
324
331
{
325
- return (time > EPOCH_AT_COMPILE_TIME);
332
+ /* EPOCH_AT_COMPILE_TIME is in local time, so we need to subtract the maximum
333
+ * possible timezone offset UTC+14 to make sure we are less then UTC time
334
+ */
335
+ return (time > (EPOCH_AT_COMPILE_TIME - (14 * 60 * 60 )));
326
336
}
327
337
328
338
bool TimeServiceClass::isTimeZoneOffsetValid (long const offset)
0 commit comments