From f4015029d2f4682783fb26e9aa31588de4377783 Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Sat, 1 May 2021 14:18:46 +0800 Subject: [PATCH] =?UTF-8?q?gettimeofday()=E5=87=BD=E6=95=B0=E6=94=AF?= =?UTF-8?q?=E6=8C=81=E6=97=B6=E5=8C=BA=EF=BC=9B=E8=A7=84=E8=8C=83set=5Ftim?= =?UTF-8?q?eval/get=5Ftimeval=E5=87=BD=E6=95=B0=E8=BF=94=E5=9B=9E=E5=80=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/libc/compilers/common/time.c | 57 ++++++++++++++----------- 1 file changed, 32 insertions(+), 25 deletions(-) diff --git a/components/libc/compilers/common/time.c b/components/libc/compilers/common/time.c index deda18a560..6a2b5620ad 100644 --- a/components/libc/compilers/common/time.c +++ b/components/libc/compilers/common/time.c @@ -17,6 +17,7 @@ * which found by Rob * 2021-02-12 Meco Man move all of the functions located in to this file * 2021-03-15 Meco Man fixed a bug of leaking memory in asctime() + * 2021-05-01 Meco Man support fixed timezone */ #include @@ -73,18 +74,18 @@ static void num2str(char *c, int i) } /** - * Get time from RTC device (without timezone) + * Get time from RTC device (without timezone, UTC+0) * @param tv: struct timeval - * @return -1 failure; 1 success + * @return the operation status, RT_EOK on successful */ -static int get_timeval(struct timeval *tv) +static rt_err_t get_timeval(struct timeval *tv) { #ifdef RT_USING_RTC static rt_device_t device = RT_NULL; rt_err_t rst = -RT_ERROR; if (tv == RT_NULL) - return -1; + return -RT_EINVAL; /* default is 0 */ tv->tv_sec = 0; @@ -110,22 +111,22 @@ static int get_timeval(struct timeval *tv) { /* LOG_W will cause a recursive printing if ulog timestamp function is enabled */ rt_kprintf("Cannot find a RTC device to provide time!\r\n"); - return -1; + return -RT_ENOSYS; } - return (rst < 0) ? -1 : 1; + return rst; #else /* LOG_W will cause a recursive printing if ulog timestamp function is enabled */ rt_kprintf("Cannot find a RTC device to provide time!\r\n"); - return -1; + return -RT_ENOSYS; #endif /* RT_USING_RTC */ } /** * Set time to RTC device (without timezone) * @param tv: struct timeval - * @return -1 failure; 1 success + * @return the operation status, RT_EOK on successful */ static int set_timeval(struct timeval *tv) { @@ -134,7 +135,7 @@ static int set_timeval(struct timeval *tv) rt_err_t rst = -RT_ERROR; if (tv == RT_NULL) - return -1; + return -RT_EINVAL; /* optimization: find rtc device only first */ if (device == RT_NULL) @@ -155,14 +156,14 @@ static int set_timeval(struct timeval *tv) else { LOG_W("Cannot find a RTC device to provide time!"); - return -1; + return -RT_ENOSYS; } - return (rst < 0) ? -1 : 1; + return rst; #else LOG_W("Cannot find a RTC device to provide time!"); - return -1; + return -RT_ENOSYS; #endif /* RT_USING_RTC */ } @@ -294,7 +295,7 @@ RT_WEAK time_t time(time_t *t) { struct timeval now; - if(get_timeval(&now) > 0) + if(get_timeval(&now) == RT_EOK) { if (t) { @@ -304,7 +305,7 @@ RT_WEAK time_t time(time_t *t) } else { - errno = EFAULT; + rt_set_errno(EFAULT); return ((time_t)-1); } } @@ -322,18 +323,18 @@ int stime(const time_t *t) if (!t) { - errno = EFAULT; + rt_set_errno(EFAULT); return -1; } tv.tv_sec = *t; - if (set_timeval(&tv) > 0) + if (set_timeval(&tv) == RT_EOK) { return 0; } else { - errno = EFAULT; + rt_set_errno(EFAULT); return -1; } } @@ -414,47 +415,53 @@ time_t timegm(struct tm * const t) } RTM_EXPORT(timegm); -/* TODO: timezone */ +/* TODO: Daylight Saving Time */ int gettimeofday(struct timeval *tv, struct timezone *tz) { - if (tv != RT_NULL && get_timeval(tv) > 0) + if(tz != RT_NULL) + { + tz->tz_dsttime = 0; + tz->tz_minuteswest = -(RT_LIBC_FIXED_TIMEZONE * 60); + } + + if (tv != RT_NULL && get_timeval(tv) == RT_EOK) { return 0; } else { - errno = EFAULT; + rt_set_errno(EFAULT); return -1; } } RTM_EXPORT(gettimeofday); -/* TODO: timezone */ +/* TODO: Daylight Saving Time */ int settimeofday(const struct timeval *tv, const struct timezone *tz) { if (tv != RT_NULL) { if(tv->tv_sec >= 0 && tv->tv_usec >= 0) { - if(set_timeval((struct timeval *)tv) > 0) + if(set_timeval((struct timeval *)tv) == RT_EOK) { return 0; } else { - errno = EFAULT; + rt_set_errno(EFAULT); return -1; } } else { - errno = EINVAL; + rt_set_errno(EINVAL); return -1; } } else { - errno = EFAULT; + rt_set_errno(EFAULT); return -1; } }