当前位置: 首页 > news >正文

wow-time时间操作说明

wow-time文件说明

  • 项目地址:https://github.com/wow-iot3/wow_linux_eval
  • 本文件的功能主要用于处理时间操作,主要涉及时间信息获取(普通格式与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; }
http://www.jsqmd.com/news/478609/

相关文章:

  • Agentic插件系统:扩展平台功能的终极架构设计指南
  • M3U8 在线调试神器!m3u8live.cn让 HLS 流测试更高效
  • HLS 开发必备!详解m3u8live.cn在线播放器的使用与价值
  • 【Index to Lectures or Courses】
  • 如何用代码定义架构:深入探索LikeC4项目
  • WebRTC系列-网络之带宽估计和码率估计(2)接收端带宽估计
  • 如何在Linux终端使用sc-im?新手入门的完整指南
  • mmdetection目标检测API封装:Python SDK开发全攻略
  • 终极Geocoder安全指南:保护API密钥与高效管理服务配额的完整方法
  • wow-byte-array数组操作说明
  • ffmpeg将mp4转换为swf、视频格式、m3u8等
  • 从零开始学习DeepSeek-Prover-V1.5-SFT:面向数学爱好者的入门教程
  • 如何在 React 项目中快速集成 Google Map React:10分钟上手教程
  • 如何快速上手LedisDB:高性能NoSQL数据库的完整指南
  • 如何构建团队密码管理系统:gopass的设计哲学与架构深度解析
  • 随心所欲画草神器:3DMAX种草画笔GrassPainter
  • 电子商务专业毕业生职业发展路径与核心能力构建研究
  • 如何使用CoreRT:.NET Core终极AOT编译优化指南
  • 目前最全的计算机视觉公开数据集汇总 持续更新 400+数据集
  • WHAT - 浏览器缓存机制系列(二)强缓存、协商缓存和启发式缓存
  • CausalML高级技巧:特征选择与因果效应异质性分析
  • ROS以及工控机环境配置
  • Gorilla技术播客系列:与AI先驱探讨函数调用的未来
  • 去毛刺机设计(机械毕业设计)
  • 为什么我的电脑不能升级Windows 11?终极兼容性检测工具深度解析
  • OCRmyPDF内存优化:处理大型PDF文件的内存管理技巧
  • Leetcode_155. 最小栈
  • 软考中级--数据库系统工程师 备考建议和考试注意事项
  • 电脑CPU速度很快,为什么3dMax还会出现卡顿的情况?
  • 牛客_JZ31 栈的压入、弹出序列