#ifndef__RTC_MODULE_H__#define__RTC_MODULE_H__#include"time.h"#include"stdbool.h"#include"stdint.h"#include"sdk_config.h"#include"nrf_atomic.h"#pragmaanon_unions#ifdef__cplusplusextern"C"{#endif#defineINIT_UTC(1609502400)#defineONE_DAY_UTC86400typedefstruct{int tm_sec;/* seconds after the minute, 0 to 60
(0 - 60 allows for the occasional leap second) */int tm_min;/* minutes after the hour, 0 to 59 */int tm_hour;/* hours since midnight, 0 to 23 */int tm_mday;/* day of the month, 1 to 31 */int tm_mon;/* months since January, 0 to 11 */int tm_year;/* years since 1900 */int tm_wday;/* days since Sunday, 0 to 6 */int tm_yday;/* days since January 1, 0 to 365 */int tm_isdst;/* Daylight Savings Time flag */union{/* ABI-required extra fields, in a variety of types */struct{int __extra_1, __extra_2;};struct{long __extra_1_long, __extra_2_long;};struct{char*__extra_1_cptr,*__extra_2_cptr;};struct{void*__extra_1_vptr,*__extra_2_vptr;};};}rtc_time_t;externint8_t time_zone;externuint8_t g_sync_utc_flag;uint32_tutc_inc_one(void);uint32_trtcm_get_sys_utc(void);voidrtcm_set_sys_utc(uint32_t tmp_utc);voidrtcm_read_sys_time(rtc_time_t*tmp_rtc);voidrtcm_secs_to_date_time(uint32_t t,rtc_time_t*p_time);voidutc_to_local_time(uint32_t utc,rtc_time_t*pTime);voidlocal_time_to_utc(uint32_t*utc,rtc_time_t*pTime);#ifdef__cplusplus}#endif#endif
#include"rtc_module.h"#include"main.h"#include"nrfx.h"/***********************************************************************
* macro define
************************************************************************
*/#defineNRF_LOG_MODULE_NAMErtc_module#ifANT_HRM_LOG_ENABLED#defineNRF_LOG_LEVEL3#defineNRF_LOG_INFO_COLOR3#else// ANT_HRM_LOG_ENABLED#defineNRF_LOG_LEVEL0#endif// ANT_HRM_LOG_ENABLED#include"nrf_log.h"NRF_LOG_MODULE_REGISTER();uint8_t g_sync_utc_flag;int8_t time_zone =8;// 时区staticnrf_atomic_u32_t sys_utc = INIT_UTC;/*************************************************************************
** function name: rtcm_get_sys_utc
** description: get the system utc
** input para:
**
** return: return the system utc
**************************************************************************/uint32_trtcm_get_sys_utc(void){uint32_t utc =nrf_atomic_u32_fetch_or(&sys_utc,0);return utc;}/**
* @brieaf utc原子操作加1
*/uint32_tutc_inc_one(void){returnnrf_atomic_u32_add(&sys_utc,1);}/*************************************************************************
** function name: rtcm_set_sys_utc
** description: set the system utc
** input para:
**
** return:
**************************************************************************/voidrtcm_set_sys_utc(uint32_t tmp_utc){uint32_t utc =nrf_atomic_u32_store(&sys_utc,tmp_utc);NRF_LOG_INFO("=========Set_time=%d",sys_utc);}/*************************************************************************
** function name: rtcm_read_sys_time
** description: read the system rtc
** input para: tmp_rtc--the rtc you want to read
**
** return:
**************************************************************************/voidrtcm_read_sys_time(rtc_time_t*tmp_rtc){//rtc_secs_to_date_time(sys_utc+(u32_t)8*3600, tmp_rtc);uint32_t utc =nrf_atomic_u32_fetch_or(&sys_utc,0);utc_to_local_time(utc, tmp_rtc);}/**
* @brieaf 本地时间转UTC
*/voidlocal_time_to_utc(uint32_t*utc,rtc_time_t*pTime){structtm p;memcpy(&p,pTime,sizeof(structtm));
p.tm_year -=1900;
p.tm_mon -=1;*utc =mktime(&p);}/**
* @brieaf utc转本地时间
*/voidutc_to_local_time(uint32_t utc,rtc_time_t*pTime){structtm*p;
p =localtime(&utc);
p->tm_year +=1900;
p->tm_mon++;memcpy(pTime,p,sizeof(structtm));}