Merge pull request #4654 from mysterywolf/timezone
[libc][time]优化gettimeofday/settimeofday; 规范set_timeval/get_timeval函数返回值
This commit is contained in:
commit
c415410498
|
@ -18,6 +18,18 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define DST_NONE 0 /* not on dst */
|
||||
#define DST_USA 1 /* USA style dst */
|
||||
#define DST_AUST 2 /* Australian style dst */
|
||||
#define DST_WET 3 /* Western European dst */
|
||||
#define DST_MET 4 /* Middle European dst */
|
||||
#define DST_EET 5 /* Eastern European dst */
|
||||
#define DST_CAN 6 /* Canada */
|
||||
#define DST_GB 7 /* Great Britain and Eire */
|
||||
#define DST_RUM 8 /* Rumania */
|
||||
#define DST_TUR 9 /* Turkey */
|
||||
#define DST_AUSTALT 10 /* Australian style with shift in 1986 */
|
||||
|
||||
#ifndef _TIMEVAL_DEFINED
|
||||
#define _TIMEVAL_DEFINED
|
||||
/*
|
||||
|
|
|
@ -17,9 +17,10 @@
|
|||
* which found by Rob <rdent@iinet.net.au>
|
||||
* 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>
|
||||
#include "sys/time.h"
|
||||
#include <rtthread.h>
|
||||
|
||||
#ifdef RT_USING_DEVICE
|
||||
|
@ -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,48 @@ time_t timegm(struct tm * const t)
|
|||
}
|
||||
RTM_EXPORT(timegm);
|
||||
|
||||
/* TODO: timezone */
|
||||
int gettimeofday(struct timeval *tv, struct timezone *tz)
|
||||
{
|
||||
if (tv != RT_NULL && get_timeval(tv) > 0)
|
||||
/* The use of the timezone structure is obsolete;
|
||||
* the tz argument should normally be specified as NULL.
|
||||
* The tz_dsttime field has never been used under Linux.
|
||||
* Thus, the following is purely of historic interest.
|
||||
*/
|
||||
if(tz != RT_NULL)
|
||||
{
|
||||
tz->tz_dsttime = DST_NONE;
|
||||
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 */
|
||||
int settimeofday(const struct timeval *tv, const struct timezone *tz)
|
||||
{
|
||||
if (tv != RT_NULL)
|
||||
/* The use of the timezone structure is obsolete;
|
||||
* the tz argument should normally be specified as NULL.
|
||||
* The tz_dsttime field has never been used under Linux.
|
||||
* Thus, the following is purely of historic interest.
|
||||
*/
|
||||
if (tv != RT_NULL
|
||||
&& tv->tv_sec >= 0
|
||||
&& tv->tv_usec >= 0
|
||||
&& set_timeval((struct timeval *)tv) == RT_EOK)
|
||||
{
|
||||
if(tv->tv_sec >= 0 && tv->tv_usec >= 0)
|
||||
{
|
||||
if(set_timeval((struct timeval *)tv) > 0)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
errno = EFAULT;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
errno = EINVAL;
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
else
|
||||
{
|
||||
errno = EFAULT;
|
||||
rt_set_errno(EINVAL);
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue