由于某些嵌入式标准库中的时间函数没有64位版本,所以需要自己实现。

这里的两个函数我在linux上与库函数作了对比测试,遍历了1970到2270的每一秒,结果无误。代码是从网上搜集修改而来,具体出处实在是忘记了,印象中都来源于glic库。

mktime64实现:

/***************************************************************************//*** \brief       Convert time struct to UTC seconds.* \param       year : [1900, ..., 2018, ...]* \param       mon  : [1, 12]* \param       day  : [1, 31]* \param       hour : [0, 23]* \param       min  : [0, 59]* \param       sec  : [0, 59]* \return      seconds from 1970-1-1 0:0:0* \author      glic* \note        timezone is ignored******************************************************************************/
long long mktime64 (unsigned int year, unsigned int mon,unsigned int day, unsigned int hour,unsigned int min, unsigned int sec)
{if (0 >= (int) (mon -= 2)) {    /* 1..12 -> 11,12,1..10 */mon += 12;      /* Puts Feb last since it has leap day */year -= 1;}return ((((long long) (year/4 - year/100 + year/400 + 367*mon/12 + day) +year*365 - 719499)*24 + hour /* now have hours */)*60 + min /* now have minutes */)*60 + sec; /* finally seconds */
}

gmtime64实现:

#define SECS_PER_HOUR        (60 * 60)
#define SECS_PER_DAY         (SECS_PER_HOUR * 24)
#define DIV(a, b)            ((a) / (b) - ((a) % (b) < 0))
#define LEAPS_THRU_END_OF(y) (DIV (y, 4) - DIV (y, 100) + DIV (y, 400))#define __isleap(year) \((year) % 4 == 0 && ((year) % 100 != 0 || (year) % 400 == 0))/***************************************************************************//*** \brief       Convert UTC seconds to tm struct.* \param       t : seconds from 1970-1-1 0:0:0* \param       tp : tm struct pointer* \return      0 : overflow error* \return      1 : success* \author      glic __offtime* \note        timezone is ignored*               struct tm*               {*                  int tm_sec;            Seconds. [0-60] (1 leap second) *                int tm_min;            Minutes. [0-59] *                int tm_hour;           Hours.   [0-23] *                int tm_mday;           Day.     [1-31] *                int tm_mon;            Month.   [0-11] *                int tm_year;           Year - 1900.  *                  int tm_wday;           Day of week. [0-6] *                 int tm_yday;           Days in year.[0-365] *               }******************************************************************************/
int gmtime64 (const long long *t, struct tm *tp)
{const unsigned short int __mon_yday[2][13] ={/* Normal years.  */{ 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 },/* Leap years.  */{ 0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366 }};long int days, rem, y;const unsigned short int *ip;days = *t / SECS_PER_DAY;rem = *t % SECS_PER_DAY;while (rem < 0){rem += SECS_PER_DAY;--days;}while (rem >= SECS_PER_DAY){rem -= SECS_PER_DAY;++days;}tp->tm_hour = rem / SECS_PER_HOUR;rem %= SECS_PER_HOUR;tp->tm_min = rem / 60;tp->tm_sec = rem % 60;/* January 1, 1970 was a Thursday.  */tp->tm_wday = (4 + days) % 7;if (tp->tm_wday < 0)tp->tm_wday += 7;y = 1970;while (days < 0 || days >= (__isleap (y) ? 366 : 365)){/* Guess a corrected year, assuming 365 days per year.  */long int yg = y + days / 365 - (days % 365 < 0);/* Adjust DAYS and Y to match the guessed year.  */days -= ((yg - y) * 365+ LEAPS_THRU_END_OF (yg - 1)- LEAPS_THRU_END_OF (y - 1));y = yg;}tp->tm_year = y - 1900;if (tp->tm_year != y - 1900){/* The year cannot be represented due to overflow.  *///__set_errno (EOVERFLOW);return 0;}tp->tm_yday = days;ip = __mon_yday[__isleap(y)];for (y = 11; days < (long int) ip[y]; --y)continue;days -= ip[y];tp->tm_mon = y;tp->tm_mday = days + 1;return 1;
}

mktime64和gmtime64实现相关推荐

  1. 移植UPnP CyberLink库到WinCE的问题与解决方法

    WinCE应用程序开发接口通Windows相同,主要分为分为MFC.ALT.C/C++ run-time library.Win32等4类. WinCE Application Frameworks ...

  2. localtime的c语言实现(gmtime64)

    是从MSVC的c runtime抄来的,my_gmtime64_s参数有变更,第二参数不用指针,而是直接传值. gmtime转的是UTC时间,时区偏移需要在传入前加入. #define _DAY_SE ...

  3. GPS Ublox配置

    UBLOX常用设置参数 ublox-config.c GPS原始RMC数据解析之DDMM.MMMM 解析GPS RMC语句 如何设置GPS模块只发送GGA和RMC数据 U-BLOX GPS 模块及GP ...

  4. jiffies 的使用

    jiffies 的使用 每一个技术点都是要靠自己对着书来一步步实践 #include <linux/init.h> #include <linux/module.h> #inc ...

最新文章

  1. Java虚拟机管理的内存运行时数据区域解释
  2. 万能的Entry,两个变量的Model/JavaBean再也不用写了!
  3. OC开发_整理笔记—— CoreLocation(1)基本使用
  4. greenfoot推箱子游戏_推箱子小游戏V2.0更新
  5. Linux Shell Record
  6. [译]CSV 注入:被人低估的巨大风险
  7. python中的可变序列有哪些_Python可变序列中的一些坑,记得多注意
  8. mysql查询两个表中的不同数据_如何实现大数据在多工作表中精准查询
  9. 虚拟机如何配置网络ip地址_木杉入门Elasticsearch(2):虚拟机IP地址配置
  10. 计算机专业职称入深户,最近深圳很火的职称入户,到底要选什么职称?
  11. 【压缩感知合集9】压缩感知的OMP算法(算法步骤分析、举例分析、说明总结和缺陷)
  12. GoEasy实现简单聊天室
  13. 《插件积累》页面音乐下载,付费音乐下载.
  14. 腰围2尺1,2,3,4,5,6,7,8寸各自等于是多少厘米/英寸(对比表)
  15. 微信小程序防止恶意点击、多次点击
  16. 对话Digital FUN和TEA社区创始人Totti#MiXTalk004
  17. 面试-android
  18. win10+ ubuntu16.04 双系统及无线、输入法、deepin-wineQQ微信等配置(亲测)
  19. 仙人掌之歌——坑,各种坑(2)
  20. 【架构】分布式服务架构与微服务架构

热门文章

  1. Mac 下如何查看 Homebrew 安装的软件位置
  2. 创业公司CEO:在公司猝死之前,我先猝死了!
  3. ROS2常见问题及解决方案
  4. 树莓派3b安装opencv简单方法
  5. 手写 vue-router
  6. Stacking方法详解
  7. java创建普通二叉树
  8. Flower Pots (CTU Open 2013 E题)
  9. Jquery插件jqprint-0.3.js实现打印
  10. Spring Boot(5)一个极简且完整的后台框架