Skip to content

Commit 5ed1f93

Browse files
committed
[rtc] use gmtime_r to replace gmtime
1 parent 04a17d4 commit 5ed1f93

File tree

23 files changed

+172
-206
lines changed

23 files changed

+172
-206
lines changed

bsp/acm32/acm32f0x0-nucleo/drivers/drv_rtc.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,21 @@ static rt_err_t set_rtc_time_stamp(time_t time_stamp)
4949
{
5050
RTC_TimeTypeDef RTC_TimeStruct = {0};
5151
RTC_DateTypeDef RTC_DateStruct = {0};
52-
struct tm *p_tm;
52+
struct tm now;
5353

54-
p_tm = gmtime(&time_stamp);
55-
if (p_tm->tm_year < 100)
54+
gmtime_r(&time_stamp, &now);
55+
if (now.tm_year < 100)
5656
{
5757
return -RT_ERROR;
5858
}
5959

60-
RTC_TimeStruct.u8_Seconds = dec2hex(p_tm->tm_sec);
61-
RTC_TimeStruct.u8_Minutes = dec2hex(p_tm->tm_min);
62-
RTC_TimeStruct.u8_Hours = dec2hex(p_tm->tm_hour);
63-
RTC_DateStruct.u8_Date = dec2hex(p_tm->tm_mday);
64-
RTC_DateStruct.u8_Month = dec2hex(p_tm->tm_mon + 1);
65-
RTC_DateStruct.u8_Year = dec2hex(p_tm->tm_year - 100);
66-
RTC_DateStruct.u8_WeekDay = dec2hex(p_tm->tm_wday) + 1;
60+
RTC_TimeStruct.u8_Seconds = dec2hex(now.tm_sec);
61+
RTC_TimeStruct.u8_Minutes = dec2hex(now.tm_min);
62+
RTC_TimeStruct.u8_Hours = dec2hex(now.tm_hour);
63+
RTC_DateStruct.u8_Date = dec2hex(now.tm_mday);
64+
RTC_DateStruct.u8_Month = dec2hex(now.tm_mon + 1);
65+
RTC_DateStruct.u8_Year = dec2hex(now.tm_year - 100);
66+
RTC_DateStruct.u8_WeekDay = dec2hex(now.tm_wday) + 1;
6767

6868
HAL_RTC_SetTime(&RTC_TimeStruct);
6969
HAL_RTC_SetDate(&RTC_DateStruct);

bsp/apollo2/board/rtc.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
3838
{
3939
time_t *time;
4040
struct tm time_temp;
41-
struct tm* time_new;
41+
struct tm time_new;
4242
am_hal_rtc_time_t hal_time;
4343

4444
RT_ASSERT(dev != RT_NULL);
@@ -71,16 +71,16 @@ static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
7171

7272
case RT_DEVICE_CTRL_RTC_SET_TIME:
7373
time = (time_t *)args;
74-
time_new = gmtime(time);
74+
gmtime_r(time, &time_new);
7575

76-
hal_time.ui32Hour = time_new->tm_hour;
77-
hal_time.ui32Minute = time_new->tm_min;
78-
hal_time.ui32Second = time_new->tm_sec;
76+
hal_time.ui32Hour = time_new.tm_hour;
77+
hal_time.ui32Minute = time_new.tm_min;
78+
hal_time.ui32Second = time_new.tm_sec;
7979
hal_time.ui32Hundredths = 00;
80-
hal_time.ui32Weekday = time_new->tm_wday;
81-
hal_time.ui32DayOfMonth = time_new->tm_mday;
82-
hal_time.ui32Month = time_new->tm_mon + 1;
83-
hal_time.ui32Year = time_new->tm_year + 1900 - 2000;
80+
hal_time.ui32Weekday = time_new.tm_wday;
81+
hal_time.ui32DayOfMonth = time_new.tm_mday;
82+
hal_time.ui32Month = time_new.tm_mon + 1;
83+
hal_time.ui32Year = time_new.tm_year + 1900 - 2000;
8484
hal_time.ui32Century = 0;
8585

8686
am_hal_rtc_time_set(&hal_time);

bsp/at32/libraries/rt_drivers/drv_rtc.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,22 +49,22 @@ static rt_err_t set_rtc_time_stamp(time_t time_stamp)
4949
{
5050
#if defined (SOC_SERIES_AT32F435) || defined (SOC_SERIES_AT32F437) || \
5151
defined (SOC_SERIES_AT32F415)
52-
struct tm *p_tm;
52+
struct tm now;
5353

54-
p_tm = gmtime(&time_stamp);
55-
if (p_tm->tm_year < 100)
54+
gmtime_r(&time_stamp, &now);
55+
if (now.tm_year < 100)
5656
{
5757
return -RT_ERROR;
5858
}
5959

6060
/* set time */
61-
if(ertc_time_set(p_tm->tm_hour, p_tm->tm_min, p_tm->tm_sec, ERTC_AM) != SUCCESS)
61+
if(ertc_time_set(now.tm_hour, now.tm_min, now.tm_sec, ERTC_AM) != SUCCESS)
6262
{
6363
return -RT_ERROR;
6464
}
6565

6666
/* set date */
67-
if(ertc_date_set(p_tm->tm_year - 100, p_tm->tm_mon + 1, p_tm->tm_mday, p_tm->tm_wday + 1) != SUCCESS)
67+
if(ertc_date_set(now.tm_year - 100, now.tm_mon + 1, now.tm_mday, now.tm_wday + 1) != SUCCESS)
6868
{
6969
return -RT_ERROR;
7070
}

bsp/essemi/es32f0654/drivers/drv_rtc.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,7 @@ static void __rtc_init(rtc_init_t *init)
5555
static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
5656
{
5757
rt_err_t result = RT_EOK;
58-
5958
struct tm time_temp;
60-
struct tm *pNow;
6159
rtc_date_t date;
6260
rtc_time_t time;
6361

@@ -76,15 +74,7 @@ static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
7674
break;
7775

7876
case RT_DEVICE_CTRL_RTC_SET_TIME:
79-
80-
rt_enter_critical();
81-
/* converts calendar time time into local time. */
82-
pNow = gmtime((const time_t *)args);
83-
/* copy the statically located variable */
84-
memcpy(&time_temp, pNow, sizeof(struct tm));
85-
/* unlock scheduler. */
86-
rt_exit_critical();
87-
77+
gmtime_r((const time_t *)args, &time_temp);
8878
time.hour = time_temp.tm_hour;
8979
time.minute = time_temp.tm_min;
9080
time.second = time_temp.tm_sec;

bsp/essemi/es32f369x/drivers/drv_rtc.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,7 @@ static void __rtc_init(rtc_init_t *init)
5454
static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
5555
{
5656
rt_err_t result = RT_EOK;
57-
5857
struct tm time_temp;
59-
struct tm *pNow;
6058
rtc_date_t date;
6159
rtc_time_t time;
6260

@@ -75,15 +73,7 @@ static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
7573
break;
7674

7775
case RT_DEVICE_CTRL_RTC_SET_TIME:
78-
79-
rt_enter_critical();
80-
/* converts calendar time time into local time. */
81-
pNow = gmtime((const time_t *)args);
82-
/* copy the statically located variable */
83-
memcpy(&time_temp, pNow, sizeof(struct tm));
84-
/* unlock scheduler. */
85-
rt_exit_critical();
86-
76+
gmtime_r((const time_t *)args, &time_temp);
8777
time.hour = time_temp.tm_hour;
8878
time.minute = time_temp.tm_min;
8979
time.second = time_temp.tm_sec;

bsp/hc32/libraries/hc32_drivers/drv_rtc.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,21 @@ static rt_err_t hc32_rtc_set_time_stamp(time_t time_stamp)
4343
{
4444
stc_rtc_time_t stcRtcTime = {0};
4545
stc_rtc_date_t stcRtcDate = {0};
46-
struct tm *p_tm;
46+
struct tm now;
4747

48-
p_tm = gmtime(&time_stamp);
49-
if (p_tm->tm_year < 100)
48+
gmtime_r(&time_stamp, &now);
49+
if (now.tm_year < 100)
5050
{
5151
return -RT_ERROR;
5252
}
5353

54-
stcRtcTime.u8Second = p_tm->tm_sec ;
55-
stcRtcTime.u8Minute = p_tm->tm_min ;
56-
stcRtcTime.u8Hour = p_tm->tm_hour;
57-
stcRtcDate.u8Day = p_tm->tm_mday;
58-
stcRtcDate.u8Month = p_tm->tm_mon + 1 ;
59-
stcRtcDate.u8Year = p_tm->tm_year - 100;
60-
stcRtcDate.u8Weekday = p_tm->tm_wday;
54+
stcRtcTime.u8Second = now.tm_sec ;
55+
stcRtcTime.u8Minute = now.tm_min ;
56+
stcRtcTime.u8Hour = now.tm_hour;
57+
stcRtcDate.u8Day = now.tm_mday;
58+
stcRtcDate.u8Month = now.tm_mon + 1 ;
59+
stcRtcDate.u8Year = now.tm_year - 100;
60+
stcRtcDate.u8Weekday = now.tm_wday;
6161

6262
if (LL_OK != RTC_SetTime(RTC_DATA_FMT_DEC, &stcRtcTime))
6363
{

bsp/hc32f460/drivers/drv_rtc.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,21 @@ static time_t hc32_rtc_get_time_stamp(void)
4040
static rt_err_t hc32_rtc_set_time_stamp(time_t time_stamp)
4141
{
4242
stc_rtc_date_time_t stcRtcDateTime = {0};
43-
struct tm *p_tm;
43+
struct tm now;
4444

45-
p_tm = gmtime(&time_stamp);
46-
if (p_tm->tm_year < 100)
45+
gmtime_r(&time_stamp, &now);
46+
if (now.tm_year < 100)
4747
{
4848
return -RT_ERROR;
4949
}
5050

51-
stcRtcDateTime.u8Second = p_tm->tm_sec ;
52-
stcRtcDateTime.u8Minute = p_tm->tm_min ;
53-
stcRtcDateTime.u8Hour = p_tm->tm_hour;
54-
stcRtcDateTime.u8Day = p_tm->tm_mday;
55-
stcRtcDateTime.u8Month = p_tm->tm_mon + 1 ;
56-
stcRtcDateTime.u8Year = p_tm->tm_year - 100;
57-
stcRtcDateTime.u8Weekday = p_tm->tm_wday;
51+
stcRtcDateTime.u8Second = now.tm_sec ;
52+
stcRtcDateTime.u8Minute = now.tm_min ;
53+
stcRtcDateTime.u8Hour = now.tm_hour;
54+
stcRtcDateTime.u8Day = now.tm_mday;
55+
stcRtcDateTime.u8Month = now.tm_mon + 1 ;
56+
stcRtcDateTime.u8Year = now.tm_year - 100;
57+
stcRtcDateTime.u8Weekday = now.tm_wday;
5858

5959
if (Ok != RTC_SetDateTime(RtcDataFormatDec, &stcRtcDateTime, Enable, Enable))
6060
{

bsp/imxrt/libraries/drivers/drv_rtc.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -45,18 +45,18 @@ static time_t get_timestamp(void)
4545

4646
static int set_timestamp(time_t timestamp)
4747
{
48-
struct tm *p_tm;
48+
struct tm now;
4949
snvs_hp_rtc_datetime_t rtcDate = {0};
5050

51-
p_tm = gmtime(&timestamp);
51+
gmtime_r(&timestamp, &now);
5252

53-
rtcDate.second = p_tm->tm_sec ;
54-
rtcDate.minute = p_tm->tm_min ;
55-
rtcDate.hour = p_tm->tm_hour;
53+
rtcDate.second = now.tm_sec ;
54+
rtcDate.minute = now.tm_min ;
55+
rtcDate.hour = now.tm_hour;
5656

57-
rtcDate.day = p_tm->tm_mday;
58-
rtcDate.month = p_tm->tm_mon + 1;
59-
rtcDate.year = p_tm->tm_year + 1900;
57+
rtcDate.day = now.tm_mday;
58+
rtcDate.month = now.tm_mon + 1;
59+
rtcDate.year = now.tm_year + 1900;
6060

6161
if (SNVS_HP_RTC_SetDatetime(SNVS, &rtcDate) != kStatus_Success)
6262
{

bsp/loongson/ls1cdev/drivers/drv_rtc.c

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,20 +50,20 @@ static time_t get_timestamp(void)
5050

5151
static int set_timestamp(time_t timestamp)
5252
{
53-
struct tm *p_tm;
54-
RTC_TimeTypeDef rtcDate;
53+
struct tm now;
54+
RTC_TimeTypeDef rtcDate;
5555

56-
p_tm = gmtime(&timestamp);
56+
gmtime_r(&timestamp, &now);
5757

58-
rtcDate.Seconds= p_tm->tm_sec ;
59-
rtcDate.Minutes= p_tm->tm_min ;
60-
rtcDate.Hours= p_tm->tm_hour;
58+
rtcDate.Seconds= now.tm_sec ;
59+
rtcDate.Minutes= now.tm_min ;
60+
rtcDate.Hours= now.tm_hour;
6161

62-
rtcDate.Date= p_tm->tm_mday;
63-
rtcDate.Month= p_tm->tm_mon + 1;
64-
rtcDate.Year= p_tm->tm_year + 1900 - 2000;
62+
rtcDate.Date= now.tm_mday;
63+
rtcDate.Month= now.tm_mon + 1;
64+
rtcDate.Year= now.tm_year + 1900 - 2000;
6565

66-
RTC_SetTime(RTC_Handler, &rtcDate);
66+
RTC_SetTime(RTC_Handler, &rtcDate);
6767
rt_kprintf("\r\nrtcDate is %d.%d.%d - %d:%d:%d",rtcDate.Year, rtcDate.Month, rtcDate.Date, rtcDate.Hours, rtcDate.Minutes, rtcDate.Seconds);
6868
return RT_EOK;
6969
}

bsp/loongson/ls2kdev/drivers/drv_rtc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,6 @@ static rt_err_t rt_rtc_ioctl(rt_device_t dev, int cmd, void *args)
128128
hw_rtc = dev->user_data;
129129

130130
t = (time_t *)args;
131-
time = *gmtime(t);
132131

133132
rtctm.sys_toyread0 = hw_rtc->sys_toyread0;
134133
rtctm.sys_toyread1 = hw_rtc->sys_toyread1;
@@ -141,6 +140,7 @@ static rt_err_t rt_rtc_ioctl(rt_device_t dev, int cmd, void *args)
141140
*t = timegm(&tmptime);
142141
break;
143142
case RT_DEVICE_CTRL_RTC_SET_TIME:
143+
gmtime_r(t, &time);
144144
tmptime.tm_hour = time.tm_hour;
145145
tmptime.tm_min = time.tm_min;
146146
tmptime.tm_sec = time.tm_sec;

bsp/lpc55sxx/Libraries/drivers/drv_rtc.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@ static time_t get_timestamp(void)
4343

4444
static int set_timestamp(time_t timestamp)
4545
{
46-
struct tm *p_tm;
46+
struct tm now;
4747
rtc_datetime_t rtcDate;
4848

49-
p_tm = gmtime(&timestamp);
49+
gmtime_r(&timestamp, &now);
5050

51-
rtcDate.second = p_tm->tm_sec ;
52-
rtcDate.minute = p_tm->tm_min ;
53-
rtcDate.hour = p_tm->tm_hour;
51+
rtcDate.second = now.tm_sec ;
52+
rtcDate.minute = now.tm_min ;
53+
rtcDate.hour = now.tm_hour;
5454

55-
rtcDate.day = p_tm->tm_mday;
56-
rtcDate.month = p_tm->tm_mon + 1;
57-
rtcDate.year = p_tm->tm_year + 1900;
55+
rtcDate.day = now.tm_mday;
56+
rtcDate.month = now.tm_mon + 1;
57+
rtcDate.year = now.tm_year + 1900;
5858

5959
/* RTC time counter has to be stopped before setting the date & time in the TSR register */
6060
RTC_StopTimer(RTC);

bsp/nuvoton/libraries/m031/rtt_port/drv_rtc.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ static rt_err_t nu_rtc_is_date_valid(const time_t t)
200200
/* Register rt-thread device.control() entry. */
201201
static rt_err_t nu_rtc_control(rt_device_t dev, int cmd, void *args)
202202
{
203-
struct tm tm_out, *tm_in;
203+
struct tm tm_out, tm_in;
204204
time_t *time;
205205
S_RTC_TIME_DATA_T hw_time;
206206

@@ -236,13 +236,13 @@ static rt_err_t nu_rtc_control(rt_device_t dev, int cmd, void *args)
236236
if (nu_rtc_is_date_valid(*time) != RT_EOK)
237237
return -(RT_ERROR);
238238

239-
tm_in = gmtime(time);
240-
hw_time.u32Year = CONV_FROM_TM_YEAR(tm_in->tm_year);
241-
hw_time.u32Month = CONV_FROM_TM_MON(tm_in->tm_mon);
242-
hw_time.u32Day = tm_in->tm_mday;
243-
hw_time.u32Hour = tm_in->tm_hour;
244-
hw_time.u32Minute = tm_in->tm_min;
245-
hw_time.u32Second = tm_in->tm_sec;
239+
gmtime_r(time, &tm_in);
240+
hw_time.u32Year = CONV_FROM_TM_YEAR(tm_in.tm_year);
241+
hw_time.u32Month = CONV_FROM_TM_MON(tm_in.tm_mon);
242+
hw_time.u32Day = tm_in.tm_mday;
243+
hw_time.u32Hour = tm_in.tm_hour;
244+
hw_time.u32Minute = tm_in.tm_min;
245+
hw_time.u32Second = tm_in.tm_sec;
246246
hw_time.u32TimeScale = RTC_CLOCK_24;
247247
hw_time.u32AmPm = 0;
248248

bsp/nuvoton/libraries/m2354/rtt_port/drv_rtc.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ static rt_err_t nu_rtc_is_date_valid(const time_t t)
203203
/* Register rt-thread device.control() entry. */
204204
static rt_err_t nu_rtc_control(rt_device_t dev, int cmd, void *args)
205205
{
206-
struct tm tm_out, *tm_in;
206+
struct tm tm_out, tm_in;
207207
time_t *time;
208208
S_RTC_TIME_DATA_T hw_time;
209209

@@ -239,13 +239,13 @@ static rt_err_t nu_rtc_control(rt_device_t dev, int cmd, void *args)
239239
if (nu_rtc_is_date_valid(*time) != RT_EOK)
240240
return -(RT_ERROR);
241241

242-
tm_in = gmtime(time);
243-
hw_time.u32Year = CONV_FROM_TM_YEAR(tm_in->tm_year);
244-
hw_time.u32Month = CONV_FROM_TM_MON(tm_in->tm_mon);
245-
hw_time.u32Day = tm_in->tm_mday;
246-
hw_time.u32Hour = tm_in->tm_hour;
247-
hw_time.u32Minute = tm_in->tm_min;
248-
hw_time.u32Second = tm_in->tm_sec;
242+
gmtime_r(time, &tm_in);
243+
hw_time.u32Year = CONV_FROM_TM_YEAR(tm_in.tm_year);
244+
hw_time.u32Month = CONV_FROM_TM_MON(tm_in.tm_mon);
245+
hw_time.u32Day = tm_in.tm_mday;
246+
hw_time.u32Hour = tm_in.tm_hour;
247+
hw_time.u32Minute = tm_in.tm_min;
248+
hw_time.u32Second = tm_in.tm_sec;
249249
hw_time.u32TimeScale = RTC_CLOCK_24;
250250
hw_time.u32AmPm = 0;
251251

0 commit comments

Comments
 (0)