Skip to content

Commit 0f48449

Browse files
committed
[timezone] implement timezone
1 parent 6c5546c commit 0f48449

File tree

3 files changed

+42
-10
lines changed

3 files changed

+42
-10
lines changed

components/libc/Kconfig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,9 +59,9 @@ if RT_USING_LIBC != y
5959
default y
6060
endif
6161

62-
config RT_LIBC_FIXED_TIMEZONE
62+
config RT_LIBC_DEFAULT_TIMEZONE
6363
depends on (RT_LIBC_USING_TIME || RT_USING_LIBC)
64-
int "Manually set a fixed time zone (UTC+)"
64+
int "Set the default time zone (UTC+)"
6565
range -12 12
6666
default 8
6767

components/libc/compilers/common/sys/time.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#define _SYS_TIME_H_
1313

1414
#include <rtconfig.h>
15+
#include <rtdef.h>
1516
#include <time.h>
1617

1718
#ifdef __cplusplus
@@ -98,6 +99,12 @@ int clock_settime (clockid_t clockid, const struct timespec *tp);
9899
int clock_time_to_tick(const struct timespec *time);
99100
#endif /* RT_USING_POSIX */
100101

102+
103+
/* timezone APIs (Not standard LIBC APIs) */
104+
void rt_tz_set(rt_int8_t tz);
105+
rt_int8_t rt_tz_get(void);
106+
rt_int8_t rt_tz_is_dst(void);
107+
101108
#ifdef __cplusplus
102109
}
103110
#endif

components/libc/compilers/common/time.c

Lines changed: 33 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
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"
@@ -32,10 +33,6 @@
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
}
208205
RTM_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
}
224221
RTM_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
}
241238
RTM_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)
651648
RTM_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

Comments
 (0)