Skip to content

Commit 37de365

Browse files
committed
Time setting code will now use the modem time if it is up to 10s behind the RTC to allow for a fast running RTC.
1 parent 4f1af65 commit 37de365

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

firmware/wombat/src/Utils.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -395,7 +395,6 @@ bool get_network_time(struct timeval & tv) {
395395

396396
tm.tm_mon -= 1; // tm.tm_month is months since January, so from 0 - 11.
397397

398-
399398
// No microseconds in the calculated time.
400399
tv.tv_usec = 0;
401400

@@ -519,17 +518,21 @@ bool connect_to_internet(void) {
519518

520519
if (get_network_time(tv)) {
521520
// tv.tv_sec is a time_t so difftime can be used to compare it with 'now' and get the difference in seconds.
522-
// If the time from the ESP32 RTC is later than the time from the modem (tv.tv_sec) then timedelta will be
523-
// negative, so assume the modem is giving the wrong time and don't set the RTC from the modem time.
521+
// If the time from the ESP32 RTC is later than the time from the modem then timedelta will be
522+
// negative.
523+
//
524+
// If the modem appears to be up to 10 seconds behind the RTC, assume clock drift is making the RTC run fast
525+
// and use the modem time. If the modem appears to be in the future, also use the modem time assuming either
526+
// the RTC is still in 1970 after booting or it is running slow.
524527
time_t now = time(nullptr);
525528
double timedelta = difftime(tv.tv_sec, now);
526529
ESP_LOGI(TAG, "timedelta = %f", timedelta);
527-
if (timedelta > 0.0) {
530+
if (timedelta > -10.0) {
528531
// Now set the system clock.
529532
settimeofday(&tv, nullptr);
530533
time_set = true;
531534
} else {
532-
ESP_LOGW(TAG, "timedelta < 0");
535+
ESP_LOGW(TAG, "modem time greater than 10 seconds in the past");
533536
}
534537
}
535538

0 commit comments

Comments
 (0)