1818 * 2021-02-12 Meco Man move all of the functions located in <clock_time.c> to this file
1919 * 2021-03-15 Meco Man fixed a bug of leaking memory in asctime()
2020 * 2021-05-01 Meco Man support fixed timezone
21+ * 2021-07-21 Meco Man implement that change/set timezone APIs
2122 */
2223
2324#include "sys/time.h"
3233#define DBG_LVL DBG_INFO
3334#include <rtdbg.h>
3435
35- #ifndef RT_LIBC_FIXED_TIMEZONE
36- #define RT_LIBC_FIXED_TIMEZONE 8 /* UTC+8 */
37- #endif
38-
3936/* seconds per day */
4037#define SPD 24*60*60
4138
@@ -202,7 +199,7 @@ struct tm *gmtime_r(const time_t *timep, struct tm *r)
202199 r -> tm_mon = i ;
203200 r -> tm_mday += work - __spm [i ];
204201
205- r -> tm_isdst = 0 ;
202+ r -> tm_isdst = rt_tz_is_dst () ;
206203 return r ;
207204}
208205RTM_EXPORT (gmtime_r );
@@ -218,7 +215,7 @@ struct tm* localtime_r(const time_t* t, struct tm* r)
218215{
219216 time_t local_tz ;
220217
221- local_tz = * t + RT_LIBC_FIXED_TIMEZONE * 3600 ;
218+ local_tz = * t + rt_tz_get () * 3600 ;
222219 return gmtime_r (& local_tz , r );
223220}
224221RTM_EXPORT (localtime_r );
@@ -235,7 +232,7 @@ time_t mktime(struct tm * const t)
235232 time_t timestamp ;
236233
237234 timestamp = timegm (t );
238- timestamp = timestamp - 3600 * RT_LIBC_FIXED_TIMEZONE ;
235+ timestamp = timestamp - 3600 * rt_tz_get () ;
239236 return timestamp ;
240237}
241238RTM_EXPORT (mktime );
@@ -426,7 +423,7 @@ int gettimeofday(struct timeval *tv, struct timezone *tz)
426423 if (tz != RT_NULL )
427424 {
428425 tz -> tz_dsttime = DST_NONE ;
429- tz -> tz_minuteswest = - (RT_LIBC_FIXED_TIMEZONE * 60 );
426+ tz -> tz_minuteswest = - (rt_tz_get () * 60 );
430427 }
431428
432429 if (tv != RT_NULL && get_timeval (tv ) == RT_EOK )
@@ -651,3 +648,31 @@ int clock_time_to_tick(const struct timespec *time)
651648RTM_EXPORT (clock_time_to_tick );
652649
653650#endif /* RT_USING_POSIX */
651+
652+
653+ /* timezone APIs (Not standard LIBC APIs) */
654+ #ifndef RT_LIBC_DEFAULT_TIMEZONE
655+ #define RT_LIBC_DEFAULT_TIMEZONE 8
656+ #endif
657+
658+ #include <rthw.h>
659+
660+ volatile static rt_int8_t rt_current_timezone = RT_LIBC_DEFAULT_TIMEZONE ;
661+
662+ void rt_tz_set (rt_int8_t tz )
663+ {
664+ register rt_base_t level ;
665+ level = rt_hw_interrupt_disable ();
666+ rt_current_timezone = tz ;
667+ rt_hw_interrupt_enable (level );
668+ }
669+
670+ rt_int8_t rt_tz_get (void )
671+ {
672+ return rt_current_timezone ;
673+ }
674+
675+ rt_int8_t rt_tz_is_dst (void )
676+ {
677+ return 0 ;
678+ }
0 commit comments