Skip to content

Commit

Permalink
fix reading time after date race
Browse files Browse the repository at this point in the history
  • Loading branch information
ant32t authored and fpistm committed Dec 21, 2024
1 parent a3036e0 commit 97adafa
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/STM32RTC.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 97adafa

Please sign in to comment.