time.h头文件中有如下几个常用函数:gmtime(),localtime(),ctime(),asctime(),mktime(),difftime(),time(),_mkgmtime()

  1. double difftime(time_t time1, time_t time0);
    Finds the difference between two times.
    获得两个time_t 时间的差(time1-time0),返回的是秒数。
  2. time_t mktime(struct tm * timeptr); 
    Convert the local time to a calendar value.
    把一个时间(默认是本地时间)转换为UTC时间。(使用这个函数之后,它会根据当前时区是否使用夏令时,把tm结构体中的tm_isdst的值改变,当前为夏令时值为1,否则为0)。
    也就是说比如本地时区是北京即+8时区,就算你的tm时间是UTC时间,转换结果也会比原来时间少8小时。
  3. time_t time(time_t * timer);
    Return the time as seconds elapsed since midnight, January 1, 1970(UTC).
    获得系统时间即(UTC时间)。
  4. char * asctime(const struct tm * timeptr);
    Convert a tm time structure to a character string.
    将tm结构体时间转换成一个字符串,格式如:Wen Jan 02 02:03:55 1980\n\0
  5. struct tm * gmtime(const time_t *timer); 
    Convert a time value to a structure.
    把一个time_t 时间转化为tm结构体时间
    当需要用转换之后的tm结构体中的tm_year时,注意这值是当前年减去1900,
    tm_year       Year (current year minus 1900).
  6. struct tm * localtime(const time_t * timer);  
    Convert a time value and correct for the local time zone.
    把一个time_t 时间转化为tm结构体时间同时调整为本地时间。
  7. char * ctime(const time_t *timer); 
    Convert a time value to a string and adjust for local time zone settings.
    把一个time_t 调整为本地时间,并转化为字符串形式,格式如:Wen Jan 02 02:03:55 1980\n\0
  8. time_t _mkgmtime(struct tm * timeptr); 
    Converts a UTC time represented by a tm struct to a UTC time represented by a time_t type.
  9. 注意事项:

    1、gmtime(),localtime(),mktime(),mkgmtime(),使用这几个函数时,共用的是一个静态分配的tm结构体内存,也就是说调用任一函数时
    会覆盖此内存中的tm结构体值,所以在使用时最好不要用指针指向这个空间。
    //Both the 32-bit and 64-bit versions of
    //gmtime, mktime, mkgmtime, and localtime all 
    //use a single tm structure per thread for the conversion.
    //Each call to one of these routines destroys the result of the previous call.

    比如下面的程序的运行结果:

    (1)、

    time_t t;
    time(&t);
    struct tm *tm1;
    struct tm *tm2;

    tm1 = gmtime(&t);
    tm2 = localtime(&t);
    cout<<"tm1: "<<asctime(tm1)<<endl;
    cout<<"tm2: "<<asctime(tm2)<<endl;

    结果:

    (2)、

    time_t t;
    time(&t);
    struct tm tm1;
    struct tm tm2;

    tm1 = *gmtime(&t);
    tm2 = *localtime(&t);
    cout<<"tm1: "<<asctime(&tm1)<<endl;
    cout<<"tm2: "<<asctime(&tm2)<<endl;

    结果:

    这是你就能看到差距了,第一个结果一样是因为调用localtime函数时,覆盖了共用静态内存空间的值,所以输出的时候用的是
    指向此内存空间的指针取值,所以结果一样。第二个,由于调用函数之后,马上将此静态存储空间的值赋值给一个tm变量,所以输出结果相差8小时。

    2、asctime和ctime函数共用一个字符串buffer。

    time_t t1;
    char* ctm;
    char* asctm;
    struct tm tm1;
    time(&t1);
    cout<<"t1: "<<t1<<endl;
    ctm = ctime(&t1);
    cout<<"ctm立即输出: "<<ctm<<endl;
    tm1 = *gmtime(&t1);
    asctm = asctime(&tm1);
    cout<<"asctm立即输出: "<<asctm<<endl;
    cout<<"ctm在asctime函数调用之后输出: "<<ctm<<endl;

    结果:

    结果显示,立即输出的cmt和在调用asctime函数之后输出cmt结果相差8小时。

    A call to ctime modifies the single statically allocated buffer used by thegmtime andlocaltime functions. Each call to one of these routines destroys the result of the previous call.ctime shares a static buffer with theasctime function. Thus, a call toctime destroys the results of any previous call toasctime,localtime, orgmtime.

    3、mktime和_mkgmtime的区别

    time_t t1;
    time_t t2;
    time_t t3;
    struct tm tm1;
    time(&t1);
    cout<<"t1: "<<t1<<endl;
    tm1 = *gmtime(&t1);
    t2 = mktime(&tm1);
    cout<<"t2: "<<t2<<endl;
    t3 = _mkgmtime(&tm1);
    cout<<"t3: "<<t3<<endl;

    结果:

    从上面可以看出,t2时间比t1和t3时间相差的秒数刚好是8小时的秒数。注意其中_mkgmtime函数为C函数,而在C++中没有mkgmtime函数。

time.h中的几个常用函数相关推荐

  1. php 与时间有关的函数,php中与时间相关的常用函数有哪些

    php中与时间相关的常用函数有:date_default_timezone_set().date_create().date_diff().date_timestamp_get().strtotime ...

  2. Python中numpy.linalg库常用函数

    Python中numpy.linalg库常用函数 numpy.linalg Python中numpy.linalg库常用函数 简单记录所遇到的numpy库内置函数 矩阵与向量积 ①np.linalg. ...

  3. 习题 8.5 将本章的例8.4改写为一个多文件的程序:1.将类定义放在头文件arraymax.h中;2.将成员函数定义放在源文件arraymax.cpp中;3.主函数放在源文件file1.cpp中。

    C++程序设计(第三版) 谭浩强 习题8.5 个人设计 习题 8.5 将本章的例8.4改写为一个多文件的程序: 1.将类定义放在头文件arraymax.h中: 2.将成员函数定义放在源文件arraym ...

  4. [Dart] Flutter开发中的几个常用函数

    几个Flutter开发中的常用函数 /** 返回当前时间戳 */static int currentTimeMillis() {return new DateTime.now().millisecon ...

  5. scala学习之scala中一些集合的常用函数

    scala学习 集合常用函数 集合的基本属性与常用操作 长度.大小.循环遍历.迭代器.生成字符串.是否有包含等 object TestSetFunction {def main(args: Array ...

  6. Python中处理字符串的常用函数汇总【文末送书】

    正式的Python专栏第23篇,同学站住,别错过这个从0开始的文章! 今天我们说了字符串的基础,格式化,这次我们讲解字符串的常用函数,不要错过! (文本送书,评论区抽取一位送书) 前两篇都在本文同个专 ...

  7. C++ algorithm库中的几个常用函数(swap,reverse,sort)

    C++中的algorithm库中有几个常用的模板函数,写算法题时经常用到,以下将其归纳总结一下(swap,reverse,sort): swap() template <class T> ...

  8. JAVA中String的一些常用函数用法总结

    最常用的就是Length()函数了, String s=""; int i=s.length(); i结果为0. 如果是String s=null; int i=s.length( ...

  9. ctype库中关于字符串的常用函数汇总

    测试代码: // File name: ctype_test // Last modified Date: 2021年10月29日10点27分 // Last Version: V1.0 // Des ...

最新文章

  1. zoj 1698 Easier Done Than Said?
  2. 高德地图 JS API - 根据地名实现标记定位
  3. Linux之特殊权限(SUID/SGID/SBIT)
  4. 获取dbgrid的行索引
  5. 问题小结(一)——servlet生命周期、get和post请求、内置对象、单例模式等
  6. 同步一张表、复制过滤设置
  7. Power BI:M与DAX以及度量与计算列
  8. 24--反转字符串中的单词 III
  9. Cocos2dx实现象棋之布局
  10. linux tty驱动名称,Linux下TTY驱动程序分析
  11. UE4官方文档UI学习:6. UMG 使用菜单锚显示弹出菜单
  12. hist 和imhist的区别
  13. VLAN间路由(笔记)
  14. 05 js面向对象(属性操作符,创建对象)
  15. 【bzoj2959】长跑【LCT+并查集】
  16. 计算机开机会跳过自检嘛,开机怎么样跳过主板自检 电脑开机不自检
  17. 第十一章 第三将 项目风险管理
  18. 碎片化NFT研究报告:COSONFT提高NFT流动性的探索和实践
  19. Nginx proxy、rewrite、alias配置
  20. 浅谈framework之PowerManagerService

热门文章

  1. 3dsmaxC4DbodypainterPS画贴图七、3dsmaxC4DbodypainterPS联动画贴图。
  2. 合宙Luat | VSCode全新LuatIDE插件版本发布,行业高效开发再添利器
  3. 专栏《乔新亮的CTO成长复盘》读书笔记(技术架构篇)
  4. android 传感器 apk,Android实现接近传感器
  5. 基于MATLAB实现WSN(无线传感器网络)的LEACH(低能耗自适应集群层次结构)(Matlab代码实现)
  6. 【面经】百度校招面试经历 - 2020.08.01
  7. IOS原声二维码条形码扫描实现
  8. 一、音视频相关的基本概念
  9. 华为云工业互联网的思与行
  10. 详尽基础:基于PyTorch的超分重建