|
20 | 20 |
|
21 | 21 | #ifdef RT_USING_RTC |
22 | 22 |
|
23 | | - |
24 | | - |
25 | 23 | /** |
26 | 24 | * Set system date(time not modify, local timezone). |
27 | 25 | * |
|
30 | 28 | * @param rt_uint32_t day e.g: 31. |
31 | 29 | * |
32 | 30 | * @return rt_err_t if set success, return RT_EOK. |
33 | | - * |
34 | 31 | */ |
35 | 32 | rt_err_t set_date(rt_uint32_t year, rt_uint32_t month, rt_uint32_t day) |
36 | 33 | { |
37 | 34 | time_t now; |
38 | | - struct tm *p_tm; |
39 | 35 | struct tm tm_new; |
40 | 36 | rt_device_t device; |
41 | 37 | rt_err_t ret = -RT_ERROR; |
42 | 38 |
|
43 | 39 | /* get current time */ |
44 | 40 | now = time(RT_NULL); |
45 | 41 |
|
46 | | - /* lock scheduler. */ |
47 | | - rt_enter_critical(); |
48 | 42 | /* 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); |
54 | 44 |
|
55 | 45 | /* update date. */ |
56 | 46 | 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) |
80 | 70 | * @param rt_uint32_t second e.g: 0~59. |
81 | 71 | * |
82 | 72 | * @return rt_err_t if set success, return RT_EOK. |
83 | | - * |
84 | 73 | */ |
85 | 74 | rt_err_t set_time(rt_uint32_t hour, rt_uint32_t minute, rt_uint32_t second) |
86 | 75 | { |
87 | 76 | time_t now; |
88 | | - struct tm *p_tm; |
89 | 77 | struct tm tm_new; |
90 | 78 | rt_device_t device; |
91 | 79 | rt_err_t ret = -RT_ERROR; |
92 | 80 |
|
93 | 81 | /* get current time */ |
94 | 82 | now = time(RT_NULL); |
95 | 83 |
|
96 | | - /* lock scheduler. */ |
97 | | - rt_enter_critical(); |
98 | 84 | /* 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); |
104 | 86 |
|
105 | 87 | /* update time. */ |
106 | 88 | tm_new.tm_hour = hour; |
|
0 commit comments