From edf5310d811a877405fd1d4a3395f2c218ee647e Mon Sep 17 00:00:00 2001 From: Klaus K Wilting Date: Sat, 26 Jan 2019 13:44:16 +0100 Subject: [PATCH] rtctime.cpp: bugfix time sync at device startup --- src/rtctime.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/rtctime.cpp b/src/rtctime.cpp index 50ee9fb4..4fe5ca47 100644 --- a/src/rtctime.cpp +++ b/src/rtctime.cpp @@ -97,14 +97,16 @@ time_t get_rtctime(void) { } // get_rtctime() void sync_rtctime(void) { - if (timeStatus() != timeSet) { - ESP_LOGE(TAG, "Unable to sync with the RTC"); - } else { - time_t t = now(); - setTime(t); - ESP_LOGI(TAG, "RTC has set system time to %02d/%02d/%d %02d:%02d:%02d", - month(t), day(t), year(t), hour(t), minute(t), second(t)); + if (timeStatus() != timeSet) { // do we need time sync? + time_t t = get_rtctime(); + if (t) { // do we have valid time by RTC? + setTime(t); + ESP_LOGI(TAG, "RTC has set system time to %02d/%02d/%d %02d:%02d:%02d", + month(t), day(t), year(t), hour(t), minute(t), second(t)); + } else + ESP_LOGE(TAG, "RTC has no confident time, not synced"); } + #ifdef TIME_SYNC_INTERVAL_RTC setSyncProvider(&get_rtctime); setSyncInterval(TIME_SYNC_INTERVAL_RTC);