Skip to content

Commit fb31ab9

Browse files
committed
[rtc] localtime 改 localtime_r
1 parent c255c49 commit fb31ab9

File tree

1 file changed

+2
-20
lines changed
  • components/drivers/rtc

1 file changed

+2
-20
lines changed

components/drivers/rtc/rtc.c

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@
2020

2121
#ifdef RT_USING_RTC
2222

23-
24-
2523
/**
2624
* Set system date(time not modify, local timezone).
2725
*
@@ -30,27 +28,19 @@
3028
* @param rt_uint32_t day e.g: 31.
3129
*
3230
* @return rt_err_t if set success, return RT_EOK.
33-
*
3431
*/
3532
rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
3633
{
3734
time_t now;
38-
struct tm *p_tm;
3935
struct tm tm_new;
4036
rt_device_t device;
4137
rt_err_t ret = -RT_ERROR;
4238

4339
/* get current time */
4440
now = time(RT_NULL);
4541

46-
/* lock scheduler. */
47-
rt_enter_critical();
4842
/* converts calendar time into local time. */
49-
p_tm = localtime(&now);
50-
/* copy the statically located variable */
51-
rt_memcpy(&tm_new, p_tm, sizeof(struct tm));
52-
/* unlock scheduler. */
53-
rt_exit_critical();
43+
localtime_r(&now, &tm_new);
5444

5545
/* update date. */
5646
tm_new.tm_year = year - 1900;
@@ -80,27 +70,19 @@ rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day)
8070
* @param rt_uint32_t second e.g: 0~59.
8171
*
8272
* @return rt_err_t if set success, return RT_EOK.
83-
*
8473
*/
8574
rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second)
8675
{
8776
time_t now;
88-
struct tm *p_tm;
8977
struct tm tm_new;
9078
rt_device_t device;
9179
rt_err_t ret = -RT_ERROR;
9280

9381
/* get current time */
9482
now = time(RT_NULL);
9583

96-
/* lock scheduler. */
97-
rt_enter_critical();
9884
/* converts calendar time into local time. */
99-
p_tm = localtime(&now);
100-
/* copy the statically located variable */
101-
rt_memcpy(&tm_new, p_tm, sizeof(struct tm));
102-
/* unlock scheduler. */
103-
rt_exit_critical();
85+
localtime_r(&now, &tm_new);
10486

10587
/* update time. */
10688
tm_new.tm_hour = hour;

0 commit comments

Comments
 (0)