summaryrefslogtreecommitdiff
path: root/components/libc
diff options
context:
space:
mode:
authorMeco Man <[email protected]>2021-05-01 14:18:46 +0800
committerMeco Man <[email protected]>2021-05-01 14:18:46 +0800
commitf4015029d2f4682783fb26e9aa31588de4377783 (patch)
treed4898775ba84e989d98c7e476f58407d0fe5db2a /components/libc
parent6d57515316bce0698d2998d807e904554db2f978 (diff)
gettimeofday()函数支持时区;规范set_timeval/get_timeval函数返回值
Diffstat (limited to 'components/libc')
-rw-r--r--components/libc/compilers/common/time.c57
1 files 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 <[email protected]>
* 2021-02-12 Meco Man move all of the functions located in <clock_time.c> 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 <sys/time.h>
@@ -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;
}
}