diff --git a/src/STM32RTC.cpp b/src/STM32RTC.cpp index 4a29928..32e24ba 100644 --- a/src/STM32RTC.cpp +++ b/src/STM32RTC.cpp @@ -69,8 +69,14 @@ void STM32RTC::begin(bool resetTime, Hour_Format format) , resetTime); _timeSet = !reinit; - syncTime(); syncDate(); + syncTime(); + + /* fix race condition where date may have changed between reading date and time */ + if (_seconds == 0 && _minutes == 0 && _hours == 0) { + syncDate(); + } + syncAlarmTime(); if (!IS_RTC_DATE(_alarmDay)) { // Use current time to init alarm members, @@ -1109,6 +1115,11 @@ time_t STM32RTC::getEpoch(uint32_t *subSeconds) syncDate(); syncTime(); + /* fix race condition where date may have changed between reading date and time */ + if (_seconds == 0 && _minutes == 0 && _hours == 0) { + syncDate(); + } + tm.tm_isdst = -1; /* * mktime ignores the values supplied by the caller in the