Merge pull request #4314 from mysterywolf/mktime1

[bug][localtime]解决底层驱动格林威治时间与当地时间混乱使用的问题
This commit is contained in:
Bernard Xiong 2021-02-15 09:26:52 +08:00 committed by GitHub
commit 7d72bdf303
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
24 changed files with 119 additions and 101 deletions

View File

@ -25,6 +25,7 @@
#include <rtthread.h>
#include <rtdevice.h>
#include "am_mcu_apollo.h"
#include <sys/time.h>
#define XT 1
#define LFRC 2
@ -78,13 +79,13 @@ static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
/* Seconds 0-59 : the 0-59 range */
time_temp.tm_sec = hal_time.ui32Second;
*time = mktime(&time_temp);
*time = timegm(&time_temp);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
time = (time_t *)args;
time_new = localtime(time);
time_new = gmtime(time);
hal_time.ui32Hour = time_new->tm_hour;
hal_time.ui32Minute = time_new->tm_min;

View File

@ -10,6 +10,7 @@
#include "board.h"
#include <rtthread.h>
#include <sys/time.h>
#ifdef BSP_USING_RTC
@ -42,7 +43,7 @@ static time_t get_rtc_timestamp(void)
tm_new.tm_year = ERTC_DateStruct.ERTC_Year + 100;
LOG_D("get rtc time.");
return mktime(&tm_new);
return timegm(&tm_new);
#else
return RTC_GetCounter();
#endif
@ -56,7 +57,7 @@ static rt_err_t set_rtc_time_stamp(time_t time_stamp)
struct tm *p_tm;
p_tm = localtime(&time_stamp);
p_tm = gmtime(&time_stamp);
if (p_tm->tm_year < 100)
{
return -RT_ERROR;

View File

@ -10,6 +10,7 @@
#include "board.h"
#include <time.h>
#include <sys/time.h>
#ifdef BSP_USING_ONCHIP_RTC
@ -26,7 +27,7 @@ static struct rt_device rtc;
uint8_t get_weekday(struct tm *const _tm)
{
uint8_t weekday;
time_t secs = mktime(_tm);
time_t secs = timegm(_tm);
weekday = (secs / 86400 + 4) % 7;
return weekday;
@ -115,7 +116,7 @@ void hal_rtc_init(void)
tm_new.tm_mday = 29;
tm_new.tm_mon = 1 - 1;
tm_new.tm_year = 2021 - 1900;
sec = mktime(&tm_new);
sec = timegm(&tm_new);
irtc_time_write(RTCCNT_CMD, sec);
}

View File

@ -12,6 +12,7 @@
#include <rtthread.h>
#include <rtdevice.h>
#include <string.h>
#include <sys/time.h>
#include "board.h"
#include "drv_rtc.h"
@ -96,14 +97,14 @@ static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
time_temp.tm_mday = date->day;
time_temp.tm_mon = date->month - 1;
time_temp.tm_year = date->year - 1900 + 2000;
*((time_t *)args) = mktime(&time_temp);
*((time_t *)args) = timegm(&time_temp);
break;
}
case RT_DEVICE_CTRL_RTC_SET_TIME:
{
rt_enter_critical();
/* converts calendar time time into local time. */
pNow = localtime((const time_t *)args);
pNow = gmtime((const time_t *)args);
/* copy the statically located variable */
memcpy(&time_temp, pNow, sizeof(struct tm));
/* unlock scheduler. */

View File

@ -12,6 +12,7 @@
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#include <string.h>
#include "board.h"
#include "drv_rtc.h"
@ -59,14 +60,14 @@ static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
time_temp.tm_mday = date.day;
time_temp.tm_mon = date.month - 1;
time_temp.tm_year = date.year - 1900 + 2000;
*((time_t *)args) = mktime(&time_temp);
*((time_t *)args) = timegm(&time_temp);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
rt_enter_critical();
/* converts calendar time time into local time. */
pNow = localtime((const time_t *)args);
pNow = gmtime((const time_t *)args);
/* copy the statically located variable */
memcpy(&time_temp, pNow, sizeof(struct tm));
/* unlock scheduler. */

View File

@ -12,6 +12,7 @@
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#include <string.h>
#include "board.h"
#include "drv_rtc.h"
@ -59,14 +60,14 @@ static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
time_temp.tm_mday = date.day;
time_temp.tm_mon = date.month - 1;
time_temp.tm_year = date.year - 1900 + 2000;
*((time_t *)args) = mktime(&time_temp);
*((time_t *)args) = timegm(&time_temp);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
rt_enter_critical();
/* converts calendar time time into local time. */
pNow = localtime((const time_t *)args);
pNow = gmtime((const time_t *)args);
/* copy the statically located variable */
memcpy(&time_temp, pNow, sizeof(struct tm));
/* unlock scheduler. */

View File

@ -12,6 +12,7 @@
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#include <string.h>
#include "board.h"
#include "drv_rtc.h"
@ -59,14 +60,14 @@ static rt_err_t es32f0_rtc_control(rt_device_t dev, int cmd, void *args)
time_temp.tm_mday = date.day;
time_temp.tm_mon = date.month - 1;
time_temp.tm_year = date.year - 1900 + 2000;
*((time_t *)args) = mktime(&time_temp);
*((time_t *)args) = timegm(&time_temp);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
rt_enter_critical();
/* converts calendar time time into local time. */
pNow = localtime((const time_t *)args);
pNow = gmtime((const time_t *)args);
/* copy the statically located variable */
memcpy(&time_temp, pNow, sizeof(struct tm));
/* unlock scheduler. */

View File

@ -8,8 +8,11 @@
* 2020-10-30 CDT first version
*/
#include "board.h"
#include <board.h>
#include <rtdbg.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#ifdef BSP_USING_RTC
@ -33,7 +36,7 @@ static time_t hc32_rtc_get_time_stamp(void)
tm_new.tm_wday = stcRtcDate.u8Weekday;
LOG_D("get rtc time.");
return mktime(&tm_new);
return timegm(&tm_new);
}
static rt_err_t hc32_rtc_set_time_stamp(time_t time_stamp)
@ -42,7 +45,7 @@ static rt_err_t hc32_rtc_set_time_stamp(time_t time_stamp)
stc_rtc_date_t stcRtcDate = {0};
struct tm *p_tm;
p_tm = localtime(&time_stamp);
p_tm = gmtime(&time_stamp);
if (p_tm->tm_year < 100)
{
return -RT_ERROR;

View File

@ -10,6 +10,8 @@
*
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#ifdef BSP_USING_RTC
@ -39,7 +41,7 @@ static time_t get_timestamp(void)
tm_new.tm_mon = rtcDate.month - 1;
tm_new.tm_year = rtcDate.year - 1900;
return mktime(&tm_new);
return timegm(&tm_new);
}
static int set_timestamp(time_t timestamp)
@ -47,7 +49,7 @@ static int set_timestamp(time_t timestamp)
struct tm *p_tm;
snvs_hp_rtc_datetime_t rtcDate = {0};
p_tm = localtime(&timestamp);
p_tm = gmtime(&timestamp);
rtcDate.second = p_tm->tm_sec ;
rtcDate.minute = p_tm->tm_min ;

View File

@ -8,12 +8,13 @@
* 2018-03-15 Liuguang the first version.
* 2019-07-19 Magicoe The first version for LPC55S6x
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#include "drv_rtc.h"
#include "fsl_common.h"
#include "fsl_rtc.h"
#include <time.h>
#ifdef RT_USING_RTC
@ -37,7 +38,7 @@ static time_t get_timestamp(void)
tm_new.tm_mon = rtcDate.month - 1;
tm_new.tm_year = rtcDate.year - 1900;
return mktime(&tm_new);
return timegm(&tm_new);
}
static int set_timestamp(time_t timestamp)
@ -45,7 +46,7 @@ static int set_timestamp(time_t timestamp)
struct tm *p_tm;
rtc_datetime_t rtcDate;
p_tm = localtime(&timestamp);
p_tm = gmtime(&timestamp);
rtcDate.second = p_tm->tm_sec ;
rtcDate.minute = p_tm->tm_min ;

View File

@ -12,6 +12,7 @@
#include "board.h"
#include "drv_rtc.h"
#include <rtdevice.h>
#include <sys/time.h>
#include "../libraries/ls1c_regs.h"
#include "../libraries/ls1c_rtc.h"
@ -44,7 +45,7 @@ static time_t get_timestamp(void)
tm_new.tm_mon = rtcDate.Month- 1;
tm_new.tm_year = rtcDate.Year + 2000 - 1900;
return mktime(&tm_new);
return timegm(&tm_new);
}
static int set_timestamp(time_t timestamp)
@ -52,7 +53,7 @@ static int set_timestamp(time_t timestamp)
struct tm *p_tm;
RTC_TimeTypeDef rtcDate;
p_tm = localtime(&timestamp);
p_tm = gmtime(&timestamp);
rtcDate.Seconds= p_tm->tm_sec ;
rtcDate.Minutes= p_tm->tm_min ;

View File

@ -14,7 +14,7 @@
#include <rthw.h>
#include <rtthread.h>
#include <rtdevice.h>
#include <rtthread.h>
#include <sys/time.h>
#include "ls2k1000.h"
#ifdef RT_USING_RTC
@ -128,7 +128,7 @@ static rt_err_t rt_rtc_ioctl(rt_device_t dev, int cmd, void *args)
hw_rtc = dev->user_data;
t = (time_t *)args;
time = *localtime(t);
time = *gmtime(t);
rtctm.sys_toyread0 = hw_rtc->sys_toyread0;
rtctm.sys_toyread1 = hw_rtc->sys_toyread1;
@ -137,29 +137,29 @@ static rt_err_t rt_rtc_ioctl(rt_device_t dev, int cmd, void *args)
switch (cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
*t = mktime(&tmptime);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
tmptime.tm_hour = time.tm_hour;
tmptime.tm_min = time.tm_min;
tmptime.tm_sec = time.tm_sec;
case RT_DEVICE_CTRL_RTC_GET_TIME:
*t = timegm(&tmptime);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
tmptime.tm_hour = time.tm_hour;
tmptime.tm_min = time.tm_min;
tmptime.tm_sec = time.tm_sec;
tmptime.tm_year = time.tm_year;
tmptime.tm_mon = time.tm_mon;
tmptime.tm_mday = time.tm_mday;
tmptime.tm_year = time.tm_year;
tmptime.tm_mon = time.tm_mon;
tmptime.tm_mday = time.tm_mday;
rtctm = mkrtctime(&tmptime);
/* write to hw RTC */
hw_rtc->sys_toywrite0 = rtctm.sys_toyread0;
hw_rtc->sys_toywrite1 = rtctm.sys_toyread1;
break;
case RT_DEVICE_CTRL_RTC_GET_ALARM:
break;
case RT_DEVICE_CTRL_RTC_SET_ALARM:
break;
default:
break;
rtctm = mkrtctime(&tmptime);
/* write to hw RTC */
hw_rtc->sys_toywrite0 = rtctm.sys_toyread0;
hw_rtc->sys_toywrite1 = rtctm.sys_toyread1;
break;
case RT_DEVICE_CTRL_RTC_GET_ALARM:
break;
case RT_DEVICE_CTRL_RTC_SET_ALARM:
break;
default:
break;
}
return RT_EOK;

View File

@ -11,7 +11,7 @@
#include "board.h"
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#include <nrfx_rtc.h>
#include <nrfx_clock.h>
@ -118,7 +118,7 @@ static rt_err_t rt_hw_rtc_register(rt_device_t device, const char *name, rt_uint
RT_ASSERT(device != RT_NULL);
init_time = mktime(&time_new);
init_time = timegm(&time_new);
if (rt_rtc_config(device) != RT_EOK)
{
return -RT_ERROR;

View File

@ -14,6 +14,7 @@
#if defined (BSP_USING_RTC)
#include <rtdevice.h>
#include <sys/time.h>
#include <NuMicro.h>
/* Private define ---------------------------------------------------------------*/
@ -183,8 +184,8 @@ static rt_err_t nu_rtc_is_date_valid(const time_t *const t)
if (!initialised)
{
t_upper = mktime((struct tm *)&tm_upper);
t_lower = mktime((struct tm *)&tm_lower);
t_upper = timegm((struct tm *)&tm_upper);
t_lower = timegm((struct tm *)&tm_lower);
initialised = RT_TRUE;
}
@ -225,13 +226,13 @@ static rt_err_t nu_rtc_control(rt_device_t dev, int cmd, void *args)
tm_out.tm_hour = hw_time.u32Hour;
tm_out.tm_min = hw_time.u32Minute;
tm_out.tm_sec = hw_time.u32Second;
*time = mktime(&tm_out);
*time = timegm(&tm_out);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
time = (time_t *) args;
tm_in = localtime(time);
tm_in = gmtime(time);
if (nu_rtc_is_date_valid(time) != RT_EOK)
return RT_ERROR;

View File

@ -14,6 +14,7 @@
#if defined (BSP_USING_RTC)
#include <rtdevice.h>
#include <sys/time.h>
#include <NuMicro.h>
#include <drv_sys.h>
@ -184,8 +185,8 @@ static rt_err_t nu_rtc_is_date_valid(const time_t *const t)
if (!initialised)
{
t_upper = mktime((struct tm *)&tm_upper);
t_lower = mktime((struct tm *)&tm_lower);
t_upper = timegm((struct tm *)&tm_upper);
t_lower = timegm((struct tm *)&tm_lower);
initialised = RT_TRUE;
}
@ -226,13 +227,13 @@ static rt_err_t nu_rtc_control(rt_device_t dev, int cmd, void *args)
tm_out.tm_hour = hw_time.u32Hour;
tm_out.tm_min = hw_time.u32Minute;
tm_out.tm_sec = hw_time.u32Second;
*time = mktime(&tm_out);
*time = timegm(&tm_out);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
time = (time_t *) args;
tm_in = localtime(time);
tm_in = gmtime(time);
if (nu_rtc_is_date_valid(time) != RT_EOK)
return -(RT_ERROR);

View File

@ -8,12 +8,15 @@
* 2019-07-29 zdzn first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#include "drv_rtc.h"
#ifdef BSP_USING_RTC
#define RTC_I2C_BUS_NAME "i2c0"
#define RTC_ADDR 0x68
#define RTC_ADDR 0x68
static struct rt_device rtc_device;
static struct rt_i2c_bus_device *i2c_bus = RT_NULL;
@ -185,13 +188,13 @@ static time_t raspi_get_timestamp(void)
tm_new.tm_min = ((buf[1] & 0x7F) / 16 + 0x30) + (buf[1] & 0x7F) % 16+ 0x30;
tm_new.tm_sec = ((buf[0] & 0x7F) / 16 + 0x30) + (buf[0] & 0x7F) % 16+ 0x30;
return mktime(&tm_new);
return timegm(&tm_new);
}
static int raspi_set_timestamp(time_t timestamp)
{
struct tm *tblock;
tblock = localtime(&timestamp);
tblock = gmtime(&timestamp);
buf[0] = 0;
buf[1] = tblock->tm_sec;
buf[2] = tblock->tm_min;

View File

@ -8,7 +8,9 @@
* 2019-07-29 zdzn first version
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <sys/time.h>
#include "drv_rtc.h"
#ifdef BSP_USING_RTC
@ -33,13 +35,13 @@ static time_t raspi_get_timestamp(void)
tm_new.tm_min = ((buf[1] & 0x7F) / 16 + 0x30) + (buf[1] & 0x7F) % 16+ 0x30;
tm_new.tm_sec = ((buf[0] & 0x7F) / 16 + 0x30) + (buf[0] & 0x7F) % 16+ 0x30;
return mktime(&tm_new);
return timegm(&tm_new);
}
static int raspi_set_timestamp(time_t timestamp)
{
struct tm *tblock;
tblock = localtime(&timestamp);
tblock = gmtime(&timestamp);
buf[0] = 0;
buf[1] = tblock->tm_sec;
buf[2] = tblock->tm_min;

View File

@ -115,7 +115,7 @@ static rt_err_t rt_rtc_control(rt_device_t dev, int cmd, void *args)
/* Change the current time */
//RTC_SetCounter(*time);
to = localtime(time);
to = gmtime(time);
RTC_TimeStructure.RTC_Seconds = to->tm_sec;
RTC_TimeStructure.RTC_Minutes = to->tm_min;
RTC_TimeStructure.RTC_Hours = to->tm_hour;

View File

@ -13,7 +13,7 @@
#include <rtdevice.h>
#include <board.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
/**
* This function will get the weed day from a date.
@ -92,12 +92,12 @@ static rt_err_t swm320_rtc_control(rt_device_t dev, int cmd, void *args)
time_temp.tm_mday = dateTime.Date;
time_temp.tm_mon = dateTime.Month - 1;
time_temp.tm_year = dateTime.Year - 1900;
*((time_t *)args) = mktime(&time_temp);
*((time_t *)args) = timegm(&time_temp);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
rt_enter_critical();
/* converts calendar time time into local time. */
pNow = localtime((const time_t *)args);
pNow = gmtime((const time_t *)args);
/* copy the statically located variable */
memcpy(&time_temp, pNow, sizeof(struct tm));
/* unlock scheduler. */

View File

@ -11,7 +11,7 @@
#include <rtdevice.h>
#include <rtthread.h>
#include <time.h>
#include <sys/time.h>
#include "wm_regs.h"
#include "wm_irq.h"
#include "tls_common.h"
@ -42,7 +42,7 @@ static time_t wm_get_timestamp(void)
tm_new.tm_min = (ctrl1 & 0x00003f00) >> 8;
tm_new.tm_sec = ctrl1 & 0x0000003f;
return mktime(&tm_new);
return timegm(&tm_new);
}
static int wm_set_timestamp(time_t timestamp)
@ -52,7 +52,7 @@ static int wm_set_timestamp(time_t timestamp)
struct tm *tblock;
tblock = localtime(&timestamp);
tblock = gmtime(&timestamp);
ctrl2 = tls_reg_read32(HR_PMU_RTC_CTRL2); /* disable */
ctrl2 &= ~(1 << 16);
@ -84,7 +84,7 @@ static int wm_alarm_set_timestamp(struct rt_rtc_wkalarm *wkalarm)
time_t timestamp = 0;
timestamp = wm_get_timestamp();
tblock = localtime(&timestamp);
tblock = gmtime(&timestamp);
tls_irq_enable(PMU_RTC_INT);

View File

@ -21,7 +21,7 @@
#include "ffconf.h"
#include "ff.h"
#include <string.h>
#include <time.h>
#include <sys/time.h>
/* ELM FatFs provide a DIR struct */
#define HAVE_DIR_STRUCTURE
@ -800,7 +800,7 @@ int dfs_elm_stat(struct dfs_filesystem *fs, const char *path, struct stat *st)
tm_file.tm_min = min; /* Minutes: 0-59 */
tm_file.tm_sec = sec; /* Seconds: 0-59 */
st->st_mtime = mktime(&tm_file);
st->st_mtime = timegm(&tm_file);
} /* get st_mtime. */
}
@ -956,7 +956,7 @@ DWORD get_fattime(void)
/* lock scheduler. */
rt_enter_critical();
/* converts calendar time time into local time. */
p_tm = localtime(&now);
p_tm = gmtime(&now);
/* copy the statically located variable */
memcpy(&tm_now, p_tm, sizeof(struct tm));
/* unlock scheduler. */

View File

@ -13,10 +13,7 @@
#include <rtthread.h>
#include <rtdevice.h>
#ifndef _WIN32
#include <sys/time.h>
#endif
#define RT_RTC_YEARS_MAX 137
#ifdef RT_USING_SOFT_RTC
@ -98,8 +95,8 @@ static void alarm_wakeup(struct rt_alarm *alarm, struct tm *now)
{
case RT_ALARM_ONESHOT:
{
sec_alarm = mktime(&alarm->wktime);
sec_now = mktime(now);
sec_alarm = timegm(&alarm->wktime);
sec_now = timegm(now);
if (((sec_now - sec_alarm) <= RT_ALARM_DELAY) && (sec_now >= sec_alarm))
{
/* stop alarm */

View File

@ -8,7 +8,7 @@
* 2018-01-30 armink the first version
*/
#include <time.h>
#include <sys/time.h>
#include <string.h>
#include <rtthread.h>
#include <rtdevice.h>
@ -126,7 +126,7 @@ int rt_soft_rtc_init(void)
#endif
init_tick = rt_tick_get();
init_time = mktime(&time_new);
init_time = timegm(&time_new);
soft_rtc_dev.type = RT_Device_Class_RTC;

View File

@ -12,7 +12,7 @@
*/
#include <rtthread.h>
#include <time.h>
#include <sys/time.h>
#include <s3c24x0.h>
// #define RTC_DEBUG
@ -125,27 +125,27 @@ static rt_size_t rtc_read(rt_device_t dev, rt_off_t pos, void* buffer, rt_size_t
static rt_err_t rtc_control(rt_device_t dev, int cmd, void *args)
{
struct tm tm, *tm_ptr;
struct tm tm, *tm_ptr;
time_t *time;
RT_ASSERT(dev != RT_NULL);
RT_ASSERT(dev != RT_NULL);
time = (time_t *)args;
switch (cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
/* read device */
rt_hw_rtc_get(&tm);
*((rt_time_t *)args) = mktime(&tm);
break;
time = (time_t *)args;
switch (cmd)
{
case RT_DEVICE_CTRL_RTC_GET_TIME:
/* read device */
rt_hw_rtc_get(&tm);
*((rt_time_t *)args) = timegm(&tm);
break;
case RT_DEVICE_CTRL_RTC_SET_TIME:
tm_ptr = localtime(time);
/* write device */
rt_hw_rtc_set(tm_ptr);
break;
}
case RT_DEVICE_CTRL_RTC_SET_TIME:
/* write device */
tm_ptr = gmtime(time);
rt_hw_rtc_set(tm_ptr);
break;
}
return RT_EOK;
return RT_EOK;
}
void rt_hw_rtc_init(void)