Skip to content

Commit 5a7245f

Browse files
authored
Merge pull request #51 from onelife/thread-safe
Replace "gmtime()" with its thread-safe version, "gmtime_r()"
2 parents ce9df0c + d014224 commit 5a7245f

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

src/RTCZero.cpp

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -404,10 +404,11 @@ void RTCZero::setAlarmEpoch(uint32_t ts)
404404
}
405405

406406
time_t t = ts;
407-
struct tm* tmp = gmtime(&t);
407+
struct tm tmp;
408408

409-
setAlarmDate(tmp->tm_mday, tmp->tm_mon + 1, tmp->tm_year - EPOCH_TIME_YEAR_OFF);
410-
setAlarmTime(tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
409+
gmtime_r(&t, &tmp);
410+
setAlarmDate(tmp.tm_mday, tmp.tm_mon + 1, tmp.tm_year - EPOCH_TIME_YEAR_OFF);
411+
setAlarmTime(tmp.tm_hour, tmp.tm_min, tmp.tm_sec);
411412
}
412413
}
413414

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

421422
time_t t = ts;
422-
struct tm* tmp = gmtime(&t);
423+
struct tm tmp;
424+
425+
gmtime_r(&t, &tmp);
423426

424427
RTC_MODE2_CLOCK_Type clockTime;
425428

426-
clockTime.bit.YEAR = tmp->tm_year - EPOCH_TIME_YEAR_OFF;
427-
clockTime.bit.MONTH = tmp->tm_mon + 1;
428-
clockTime.bit.DAY = tmp->tm_mday;
429-
clockTime.bit.HOUR = tmp->tm_hour;
430-
clockTime.bit.MINUTE = tmp->tm_min;
431-
clockTime.bit.SECOND = tmp->tm_sec;
429+
clockTime.bit.YEAR = tmp.tm_year - EPOCH_TIME_YEAR_OFF;
430+
clockTime.bit.MONTH = tmp.tm_mon + 1;
431+
clockTime.bit.DAY = tmp.tm_mday;
432+
clockTime.bit.HOUR = tmp.tm_hour;
433+
clockTime.bit.MINUTE = tmp.tm_min;
434+
clockTime.bit.SECOND = tmp.tm_sec;
432435

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

0 commit comments

Comments
 (0)