wow-time文件说明
- 项目地址:https://gitee.com/wow-iot/wow-iot7
- 本文件的功能主要用于处理时间操作,主要涉及时间信息获取(普通格式与cp56格式)、设置时间、格式转换、获取时间戳、获取毫秒数;
获取时间信息
int wow_time_get_cp56(CP56Time2a_T *ptInfo)
{
int ret = -1;
time_t utc;
struct timeval tv;
struct timezone tz;
struct tm *now;
ret = time(&utc);
CHECK_RET_VAL_P_A(ret != -1,-1,"time faild\n");
now = localtime(&utc);
CHECK_RET_VAL_P_A(now,-1,"localtime faild\n");
//获取毫秒
ret = gettimeofday(&tv, &tz);
CHECK_RET_VAL_P_A(ret == 0,-1,"gettimeofday faild\n");
ptInfo->year = now->tm_year + 1900-2000;
ptInfo->mon = now->tm_mon + 1;
ptInfo->mon_day = now->tm_mday;
ptInfo->hour = now->tm_hour;
ptInfo->min = now->tm_min;
time_t msec = now->tm_sec*1000 + tv.tv_usec/1000;
ptInfo->milli_sec_h = (msec >> 0x08) & 0x00FF;
ptInfo->milli_sec_l = msec & 0x00FF;
return 0;
}
设置时间
int wow_time_set_cp56(CP56Time2a_T *ptInfo)
{
time_t timep;
struct tm tm;
struct timeval tv;
time_t msec = (ptInfo->milli_sec_h << 8) + ptInfo->milli_sec_l;
tm.tm_sec = msec/1000;
tm.tm_min = ptInfo->min;
tm.tm_hour = ptInfo->hour;
tm.tm_mday = ptInfo->mon_day;
tm.tm_mon = ptInfo->mon - 1; // 月份从0开始,所以需要减去1
tm.tm_year = ptInfo->year + 2000 - 1900;// 年份需要减去1900
tm.tm_isdst = -1; // 表示不考虑夏令时
timep = mktime(&tm);
tv.tv_sec = timep;
tv.tv_usec = 0;
int ret = settimeofday(&tv, NULL);
CHECK_RET_VAL_P_A(ret == 0,-1,"settimeofday faild\n");
return 0;
}
将毫秒转换为固定格式
int wow_time_msec_to_cp56(uint64_t nMsec,CP56Time2a_T* ptInfo)
{
int i = 0;
CHECK_RET_VAL_P(ptInfo,-PARAM_INPUT_STRUCT_IS_NULL,"param input struct invalid!\n");
memset(ptInfo,0,sizeof(CP56Time2a_T));
ptInfo->milli_sec_l = (uint8_t)(nMsec%60000);
ptInfo->milli_sec_h = (uint8_t)((nMsec%60000)>>8);
uint64_t sec_time = nMsec/60000;
ptInfo->min=(uint8_t)(sec_time % 60);//计算当分前钟数
sec_time = sec_time/60;
ptInfo->hour=(uint8_t)(sec_time % 24);//计算当前小时数
sec_time = sec_time/24;
//以4年为基准计算
ptInfo->year = (uint16_t)(sec_time/1461L)*4 + 1970 - 2000;
sec_time = sec_time%1461;
//校正闰年影响的年份
while(1){
int days = 365;
if ((ptInfo->year & 3) == 0) days = 366;
if (sec_time < days) break;
ptInfo->year++; //计算当前年份
sec_time -= days;
}
sec_time = sec_time+1;
//ptInfo->week = (uint8_t)((sec_time+4)%7);
//校正闰年的月份
if((ptInfo->year & 3) == 0) {
for(i = 12;i > 0;i--){
if(gs_mon_days[1][i-1] < sec_time){
ptInfo->mon = (uint8_t)i; //计算当前月份
ptInfo->mon_day = (uint8_t)(sec_time - gs_mon_days[1][i-1]); //计算当前日份
break;
}
}
}else{
for(i = 12;i > 0;i--){
if(gs_mon_days[0][i-1] < sec_time){
ptInfo->mon = (uint8_t)i; //计算当前月份
ptInfo->mon_day = (uint8_t)(sec_time - gs_mon_days[0][i-1]); //计算当前日份
break;
}
}
}
return 0;
}
void wow_time_msec_to_stamp(uint64_t pMsec,char pcBuff[20])
{
struct tm now_tm;
time_t now_sec = pMsec/1000;
localtime_r(&now_sec, &now_tm);
strftime(pcBuff, 20, "%Y-%m-%d %H:%M:%S", &now_tm);
}
将固定格式转换为毫秒
int wow_time_cp56_to_msec(CP56Time2a_T* ptInfo,uint64_t* pMsec)
{
int i = 0;
CHECK_RET_VAL_P(ptInfo,-PARAM_INPUT_STRUCT_IS_NULL,"param input struct invalid!\n");
uint64_t msec = 0;
// 计算当前年秒数
msec = (ptInfo->year +2000 - 1970) * 365 * 24 * 3600;
for(i = 1970; i < ptInfo->year +2000; i++)
{
if(rtc_data_leap(i)) {
msec += 24 * 3600;
}
}
//计算当前月秒数
msec += gs_mon_days[rtc_data_leap(ptInfo->year)][ptInfo->mon-1]* 24 * 3600;
//计算当前日秒数
msec += (ptInfo->mon_day - 1) * 24 * 3600;
//计算当前时间段秒数
msec += ptInfo->hour * 3600 + ptInfo->min * 60;
///< !!!根据需求添加
//msec -= SEC_TIME_ZONE;
*pMsec= msec*1000 + ptInfo->milli_sec_h*256 +ptInfo->milli_sec_l;
return 0;
}
获取当前毫秒数
int64_t wow_time_get_msec()
{
struct timeval tv = {0};
gettimeofday(&tv, NULL);
int64_t sec = tv.tv_sec;
int64_t msec = sec * 1000 + tv.tv_usec / 1000;
return msec;
}