@@ -141,12 +141,21 @@ unsigned long TimeServiceClass::getTime()
141141 unsigned long const current_tick = millis ();
142142 bool const is_ntp_sync_timeout = (current_tick - _last_sync_tick) > _sync_interval_ms;
143143 if (!_is_rtc_configured || is_ntp_sync_timeout) {
144+ /* Try to sync time from NTP or connection handler */
144145 sync ();
145146 }
146147
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;
150159}
151160
152161void TimeServiceClass::setTime (unsigned long time)
@@ -158,20 +167,20 @@ bool TimeServiceClass::sync()
158167{
159168 _is_rtc_configured = false ;
160169
161- unsigned long utc = EPOCH_AT_COMPILE_TIME ;
170+ unsigned long utc = EPOCH ;
162171 if (_sync_func) {
163172 utc = _sync_func ();
164173 } else {
165174#if defined(HAS_NOTECARD) || defined(HAS_TCP)
166175 utc = getRemoteTime ();
167176#elif defined(HAS_LORA)
168- /* Just keep incrementing stored RTC value*/
177+ /* Just keep incrementing stored RTC value starting from EPOCH_AT_COMPILE_TIME */
169178 utc = getRTC ();
170179#endif
171180 }
172181
173182 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);
175184 setRTC (utc);
176185 _last_sync_tick = millis ();
177186 _is_rtc_configured = true ;
@@ -299,6 +308,7 @@ unsigned long TimeServiceClass::getRemoteTime()
299308 return ntp_time;
300309 }
301310 }
311+ DEBUG_WARNING (" TimeServiceClass::%s cannot get time from NTP, fallback on connection handler" , __FUNCTION__);
302312#endif /* HAS_TCP */
303313
304314 /* As fallback if NTP request fails try to obtain the
@@ -308,21 +318,21 @@ unsigned long TimeServiceClass::getRemoteTime()
308318 if (isTimeValid (connection_time)) {
309319 return connection_time;
310320 }
321+ DEBUG_WARNING (" TimeServiceClass::%s cannot get time from connection handler" , __FUNCTION__);
311322 }
312323
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;
319326}
320327
321328#endif /* HAS_NOTECARD || HAS_TCP */
322329
323330bool TimeServiceClass::isTimeValid (unsigned long const time)
324331{
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 )));
326336}
327337
328338bool TimeServiceClass::isTimeZoneOffsetValid (long const offset)
0 commit comments