Skip to content

Commit 72c4504

Browse files
RCSNRbb666
authored andcommitted
components: drivers: rtc: add the alarm using local time for calculation
- add the alarm using local time for calculation Signed-off-by: Runcheng Lu <runcheng.lu@hpmicro.com>
1 parent f5bff56 commit 72c4504

File tree

2 files changed

+42
-7
lines changed

2 files changed

+42
-7
lines changed

components/drivers/rtc/Kconfig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ config RT_USING_RTC
1919
config RT_ALARM_PRIORITY
2020
int "priority for alarm thread"
2121
default 10
22+
23+
config RT_ALARM_USING_LOCAL_TIME
24+
bool "Using local time for the alarm calculation"
25+
default n
26+
depends on RT_USING_ALARM
2227
endif
2328

2429
config RT_USING_SOFT_RTC

components/drivers/rtc/dev_alarm.c

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2006-2024 RT-Thread Development Team
2+
* Copyright (c) 2006-2025 RT-Thread Development Team
33
*
44
* SPDX-License-Identifier: Apache-2.0
55
*
@@ -10,6 +10,7 @@
1010
* 2020-10-15 zhangsz add alarm flags hour minute second.
1111
* 2020-11-09 zhangsz fix alarm set when modify rtc time.
1212
* 2024-09-29 milo make internal thread's attributes configurable.
13+
* 2025-6-4 RCSN support the alarm using local time for calculation
1314
*/
1415

1516
#include <rtthread.h>
@@ -108,8 +109,13 @@ static void alarm_wakeup(struct rt_alarm *alarm, struct tm *now)
108109
{
109110
case RT_ALARM_ONESHOT:
110111
{
112+
#ifdef RT_ALARM_USING_LOCAL_TIME
113+
sec_alarm = mktime(&alarm->wktime);
114+
sec_now = mktime(now);
115+
#else
111116
sec_alarm = timegm(&alarm->wktime);
112117
sec_now = timegm(now);
118+
#endif
113119
if (((sec_now - sec_alarm) <= RT_ALARM_DELAY) && (sec_now >= sec_alarm))
114120
{
115121
/* stop alarm */
@@ -239,7 +245,11 @@ static void alarm_update(rt_uint32_t event)
239245
{
240246
/* get time of now */
241247
get_timestamp(&timestamp);
248+
#ifdef RT_ALARM_USING_LOCAL_TIME
249+
localtime_r(&timestamp, &now);
250+
#else
242251
gmtime_r(&timestamp, &now);
252+
#endif
243253

244254
for (next = _container.head.next; next != &_container.head; next = next->next)
245255
{
@@ -250,7 +260,11 @@ static void alarm_update(rt_uint32_t event)
250260

251261
/* get time of now */
252262
get_timestamp(&timestamp);
263+
#ifdef RT_ALARM_USING_LOCAL_TIME
264+
localtime_r(&timestamp, &now);
265+
#else
253266
gmtime_r(&timestamp, &now);
267+
#endif
254268
sec_now = alarm_mkdaysec(&now);
255269

256270
for (next = _container.head.next; next != &_container.head; next = next->next)
@@ -359,7 +373,11 @@ static rt_err_t alarm_setup(rt_alarm_t alarm, struct tm *wktime)
359373
*setup = *wktime;
360374
/* get time of now */
361375
get_timestamp(&timestamp);
376+
#ifdef RT_ALARM_USING_LOCAL_TIME
377+
localtime_r(&timestamp, &now);
378+
#else
362379
gmtime_r(&timestamp, &now);
380+
#endif
363381

364382
/* if these are a "don't care" value,we set them to now*/
365383
if ((setup->tm_sec > 59) || (setup->tm_sec < 0))
@@ -574,7 +592,11 @@ rt_err_t rt_alarm_start(rt_alarm_t alarm)
574592

575593
/* get time of now */
576594
get_timestamp(&timestamp);
595+
#ifdef RT_ALARM_USING_LOCAL_TIME
596+
localtime_r(&timestamp, &now);
597+
#else
577598
gmtime_r(&timestamp, &now);
599+
#endif
578600

579601
alarm->flag |= RT_ALARM_STATE_START;
580602

@@ -768,18 +790,26 @@ void rt_alarm_dump(void)
768790
{
769791
rt_list_t *next;
770792
rt_alarm_t alarm;
771-
772-
rt_kprintf("| hh:mm:ss | week | flag | en |\n");
773-
rt_kprintf("+----------+------+------+----+\n");
793+
int32_t tz_offset_sec = 0;
794+
uint32_t abs_tz_offset_sec = 0U;
795+
#ifdef RT_ALARM_USING_LOCAL_TIME
796+
#if defined(RT_LIBC_USING_LIGHT_TZ_DST)
797+
tz_offset_sec = rt_tz_get();
798+
#endif /* RT_LIBC_USING_LIGHT_TZ_DST */
799+
abs_tz_offset_sec = tz_offset_sec > 0 ? tz_offset_sec : -tz_offset_sec;
800+
#endif
801+
rt_kprintf("| hh:mm:ss | week | flag | en | timezone |\n");
802+
rt_kprintf("+----------+------+------+----+--------------+\n");
774803
for (next = _container.head.next; next != &_container.head; next = next->next)
775804
{
776805
alarm = rt_list_entry(next, struct rt_alarm, list);
777806
rt_uint8_t flag_index = get_alarm_flag_index(alarm->flag);
778-
rt_kprintf("| %02d:%02d:%02d | %2d | %2s | %2d |\n",
807+
rt_kprintf("| %02d:%02d:%02d | %2d | %2s | %2d | UTC%c%02d:%02d:%02d |\n",
779808
alarm->wktime.tm_hour, alarm->wktime.tm_min, alarm->wktime.tm_sec,
780-
alarm->wktime.tm_wday, _alarm_flag_tbl[flag_index].name, alarm->flag & RT_ALARM_STATE_START);
809+
alarm->wktime.tm_wday, _alarm_flag_tbl[flag_index].name, alarm->flag & RT_ALARM_STATE_START,
810+
tz_offset_sec > 0 ? '+' : '-', abs_tz_offset_sec / 3600U, abs_tz_offset_sec % 3600U / 60U, abs_tz_offset_sec % 3600U % 60U);
781811
}
782-
rt_kprintf("+----------+------+------+----+\n");
812+
rt_kprintf("+----------+------+------+----+--------------+\n");
783813
}
784814

785815
MSH_CMD_EXPORT_ALIAS(rt_alarm_dump, list_alarm, list alarm info);

0 commit comments

Comments
 (0)