1
1
/*
2
- * Copyright (c) 2006-2024 RT-Thread Development Team
2
+ * Copyright (c) 2006-2025 RT-Thread Development Team
3
3
*
4
4
* SPDX-License-Identifier: Apache-2.0
5
5
*
10
10
* 2020-10-15 zhangsz add alarm flags hour minute second.
11
11
* 2020-11-09 zhangsz fix alarm set when modify rtc time.
12
12
* 2024-09-29 milo make internal thread's attributes configurable.
13
+ * 2025-6-4 RCSN support the alarm using local time for calculation
13
14
*/
14
15
15
16
#include <rtthread.h>
@@ -108,8 +109,13 @@ static void alarm_wakeup(struct rt_alarm *alarm, struct tm *now)
108
109
{
109
110
case RT_ALARM_ONESHOT :
110
111
{
112
+ #ifdef RT_ALARM_USING_LOCAL_TIME
113
+ sec_alarm = mktime (& alarm -> wktime );
114
+ sec_now = mktime (now );
115
+ #else
111
116
sec_alarm = timegm (& alarm -> wktime );
112
117
sec_now = timegm (now );
118
+ #endif
113
119
if (((sec_now - sec_alarm ) <= RT_ALARM_DELAY ) && (sec_now >= sec_alarm ))
114
120
{
115
121
/* stop alarm */
@@ -239,7 +245,11 @@ static void alarm_update(rt_uint32_t event)
239
245
{
240
246
/* get time of now */
241
247
get_timestamp (& timestamp );
248
+ #ifdef RT_ALARM_USING_LOCAL_TIME
249
+ localtime_r (& timestamp , & now );
250
+ #else
242
251
gmtime_r (& timestamp , & now );
252
+ #endif
243
253
244
254
for (next = _container .head .next ; next != & _container .head ; next = next -> next )
245
255
{
@@ -250,7 +260,11 @@ static void alarm_update(rt_uint32_t event)
250
260
251
261
/* get time of now */
252
262
get_timestamp (& timestamp );
263
+ #ifdef RT_ALARM_USING_LOCAL_TIME
264
+ localtime_r (& timestamp , & now );
265
+ #else
253
266
gmtime_r (& timestamp , & now );
267
+ #endif
254
268
sec_now = alarm_mkdaysec (& now );
255
269
256
270
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)
359
373
* setup = * wktime ;
360
374
/* get time of now */
361
375
get_timestamp (& timestamp );
376
+ #ifdef RT_ALARM_USING_LOCAL_TIME
377
+ localtime_r (& timestamp , & now );
378
+ #else
362
379
gmtime_r (& timestamp , & now );
380
+ #endif
363
381
364
382
/* if these are a "don't care" value,we set them to now*/
365
383
if ((setup -> tm_sec > 59 ) || (setup -> tm_sec < 0 ))
@@ -574,7 +592,11 @@ rt_err_t rt_alarm_start(rt_alarm_t alarm)
574
592
575
593
/* get time of now */
576
594
get_timestamp (& timestamp );
595
+ #ifdef RT_ALARM_USING_LOCAL_TIME
596
+ localtime_r (& timestamp , & now );
597
+ #else
577
598
gmtime_r (& timestamp , & now );
599
+ #endif
578
600
579
601
alarm -> flag |= RT_ALARM_STATE_START ;
580
602
@@ -768,18 +790,26 @@ void rt_alarm_dump(void)
768
790
{
769
791
rt_list_t * next ;
770
792
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" );
774
803
for (next = _container .head .next ; next != & _container .head ; next = next -> next )
775
804
{
776
805
alarm = rt_list_entry (next , struct rt_alarm , list );
777
806
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" ,
779
808
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 );
781
811
}
782
- rt_kprintf ("+----------+------+------+----+\n" );
812
+ rt_kprintf ("+----------+------+------+----+--------------+ \n" );
783
813
}
784
814
785
815
MSH_CMD_EXPORT_ALIAS (rt_alarm_dump , list_alarm , list alarm info );
0 commit comments