Skip to content

Commit

Permalink
Merge pull request #51 from onelife/thread-safe
Browse files Browse the repository at this point in the history
Replace "gmtime()" with its thread-safe version, "gmtime_r()"
  • Loading branch information
aentinger committed Dec 9, 2020
2 parents ce9df0c + d014224 commit 5a7245f
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions src/RTCZero.cpp
Expand Up @@ -404,10 +404,11 @@ void RTCZero::setAlarmEpoch(uint32_t ts)
}

time_t t = ts;
struct tm* tmp = gmtime(&t);
struct tm tmp;

setAlarmDate(tmp->tm_mday, tmp->tm_mon + 1, tmp->tm_year - EPOCH_TIME_YEAR_OFF);
setAlarmTime(tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
gmtime_r(&t, &tmp);
setAlarmDate(tmp.tm_mday, tmp.tm_mon + 1, tmp.tm_year - EPOCH_TIME_YEAR_OFF);
setAlarmTime(tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
}
}

Expand All @@ -419,16 +420,18 @@ void RTCZero::setEpoch(uint32_t ts)
}

time_t t = ts;
struct tm* tmp = gmtime(&t);
struct tm tmp;

gmtime_r(&t, &tmp);

RTC_MODE2_CLOCK_Type clockTime;

clockTime.bit.YEAR = tmp->tm_year - EPOCH_TIME_YEAR_OFF;
clockTime.bit.MONTH = tmp->tm_mon + 1;
clockTime.bit.DAY = tmp->tm_mday;
clockTime.bit.HOUR = tmp->tm_hour;
clockTime.bit.MINUTE = tmp->tm_min;
clockTime.bit.SECOND = tmp->tm_sec;
clockTime.bit.YEAR = tmp.tm_year - EPOCH_TIME_YEAR_OFF;
clockTime.bit.MONTH = tmp.tm_mon + 1;
clockTime.bit.DAY = tmp.tm_mday;
clockTime.bit.HOUR = tmp.tm_hour;
clockTime.bit.MINUTE = tmp.tm_min;
clockTime.bit.SECOND = tmp.tm_sec;

RTC->MODE2.CLOCK.reg = clockTime.reg;

Expand Down

0 comments on commit 5a7245f

Please sign in to comment.