From 91e9d26e9170c3bf688418da540e08bb8bd9207e Mon Sep 17 00:00:00 2001 From: pennam Date: Wed, 6 Jul 2022 16:51:58 +0200 Subject: [PATCH] Use NTP as primary time source and connection handler as fallback --- src/utility/time/TimeService.cpp | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/utility/time/TimeService.cpp b/src/utility/time/TimeService.cpp index 0b0b982fc..a9536caa0 100644 --- a/src/utility/time/TimeService.cpp +++ b/src/utility/time/TimeService.cpp @@ -279,24 +279,24 @@ bool TimeServiceClass::connected() unsigned long TimeServiceClass::getRemoteTime() { if(connected()) { - /* At first try to see if a valid time can be obtained - * using the network time available via the connection - * handler. - */ - unsigned long const connection_time = _con_hdl->getTime(); - if(isTimeValid(connection_time)) { - return connection_time; - } - #ifndef __AVR__ - /* If no valid network time is available try to obtain the - * time via NTP next. + /* At first try to obtain a valid time via NTP. + * This is the most reliable time source and it will + * ensure a correct behaviour of the library. */ unsigned long const ntp_time = NTPUtils::getTime(_con_hdl->getUDP()); if(isTimeValid(ntp_time)) { return ntp_time; } #endif + + /* As fallback if NTP request fails try to obtain the + * network time using the connection handler. + */ + unsigned long const connection_time = _con_hdl->getTime(); + if(isTimeValid(connection_time)) { + return connection_time; + } } /* Return the epoch timestamp at compile time as a last