Skip to content

Commit 06dac90

Browse files
committed
fix reading time after date race
1 parent a3036e0 commit 06dac90

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/STM32RTC.cpp

+12-1
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,14 @@ void STM32RTC::begin(bool resetTime, Hour_Format format)
6969
, resetTime);
7070
_timeSet = !reinit;
7171

72-
syncTime();
7372
syncDate();
73+
syncTime();
74+
75+
/* fix race condition where date may have changed between reading date and time */
76+
if (_seconds == 0 && _minutes == 0 && _hours == 0) {
77+
syncDate();
78+
}
79+
7480
syncAlarmTime();
7581
if (!IS_RTC_DATE(_alarmDay)) {
7682
// Use current time to init alarm members,
@@ -1109,6 +1115,11 @@ time_t STM32RTC::getEpoch(uint32_t *subSeconds)
11091115
syncDate();
11101116
syncTime();
11111117

1118+
/* fix race condition where date may have changed between reading date and time */
1119+
if (_seconds == 0 && _minutes == 0 && _hours == 0) {
1120+
syncDate();
1121+
}
1122+
11121123
tm.tm_isdst = -1;
11131124
/*
11141125
* mktime ignores the values supplied by the caller in the

0 commit comments

Comments
 (0)