time.h中的几个常用函数
time.h头文件中有如下几个常用函数:gmtime(),localtime(),ctime(),asctime(),mktime(),difftime(),time(),_mkgmtime()
- double difftime(time_t time1, time_t time0);
Finds the difference between two times.
获得两个time_t 时间的差(time1-time0),返回的是秒数。 - time_t mktime(struct tm * timeptr);
Convert the local time to a calendar value.
把一个时间(默认是本地时间)转换为UTC时间。(使用这个函数之后,它会根据当前时区是否使用夏令时,把tm结构体中的tm_isdst的值改变,当前为夏令时值为1,否则为0)。
也就是说比如本地时区是北京即+8时区,就算你的tm时间是UTC时间,转换结果也会比原来时间少8小时。 - time_t time(time_t * timer);
Return the time as seconds elapsed since midnight, January 1, 1970(UTC).
获得系统时间即(UTC时间)。 - 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 - 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). - struct tm * localtime(const time_t * timer);
Convert a time value and correct for the local time zone.
把一个time_t 时间转化为tm结构体时间同时调整为本地时间。 - 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 - 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. 注意事项:
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中的几个常用函数相关推荐
- php 与时间有关的函数,php中与时间相关的常用函数有哪些
php中与时间相关的常用函数有:date_default_timezone_set().date_create().date_diff().date_timestamp_get().strtotime ...
- Python中numpy.linalg库常用函数
Python中numpy.linalg库常用函数 numpy.linalg Python中numpy.linalg库常用函数 简单记录所遇到的numpy库内置函数 矩阵与向量积 ①np.linalg. ...
- 习题 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 ...
- [Dart] Flutter开发中的几个常用函数
几个Flutter开发中的常用函数 /** 返回当前时间戳 */static int currentTimeMillis() {return new DateTime.now().millisecon ...
- scala学习之scala中一些集合的常用函数
scala学习 集合常用函数 集合的基本属性与常用操作 长度.大小.循环遍历.迭代器.生成字符串.是否有包含等 object TestSetFunction {def main(args: Array ...
- Python中处理字符串的常用函数汇总【文末送书】
正式的Python专栏第23篇,同学站住,别错过这个从0开始的文章! 今天我们说了字符串的基础,格式化,这次我们讲解字符串的常用函数,不要错过! (文本送书,评论区抽取一位送书) 前两篇都在本文同个专 ...
- C++ algorithm库中的几个常用函数(swap,reverse,sort)
C++中的algorithm库中有几个常用的模板函数,写算法题时经常用到,以下将其归纳总结一下(swap,reverse,sort): swap() template <class T> ...
- JAVA中String的一些常用函数用法总结
最常用的就是Length()函数了, String s=""; int i=s.length(); i结果为0. 如果是String s=null; int i=s.length( ...
- ctype库中关于字符串的常用函数汇总
测试代码: // File name: ctype_test // Last modified Date: 2021年10月29日10点27分 // Last Version: V1.0 // Des ...
最新文章
- android layout组件,Android UI学习 - Linear Layout, RelativeLayout
- if something reaches the top
- 两个列向量相乘怎么计算_机器学习 线性代数基础 | 1.4 矩阵乘向量的新视角:变换基底...
- 前端学习(2334):angular之内置属性指令ngclass
- 安卓系统按键映射修改
- spring boot项目Intellij 打包
- 大年初一连夜带娃改bug:CTO把代码写成这鬼样子,被害惨了!
- 使用H-lua框架制作魔兽争霸地图(7-物编-物品合成篇)
- 下周出发去印度:直觉之旅,发现自己
- 解决MySQL报ValueError(“Could not process parameters“)错误
- pcb二次钻孔_pcb钻孔的注意事项
- 基于SSM或SpringBoot的JavaWeb项目——写作分析系统
- 信息系统项目管理师必背核心考点(四十九)合同法
- org.json.JSONException: Value [{“id“:10,“userId“:6,“adminId“:5,“content“:“7777“,“state“:-1,“image1“:
- 网络编程9_线程-条件,定时器,队列,线程池, 协程
- bp神经网络是用来干嘛的,BP神经网络什么意思
- CSS:css减肥瘦身工具
- Centos7安装kvm服务器
- 物流ERP软件测试行业,简单说说ERP测试
- java设计按月每天签到_签到功能java实现
热门文章
- Mac office ppt无法正常输入文字的问题解决方案
- 火山引擎入选国内首个《边缘计算产业全景图》
- 条形码组件Spire.Barcode 教程:在Java中扫描条形码
- 随机生成10个含有32位数字或者字符的密码
- 大数据时代,小数据中心
- [win7] window 7 home basic - administrator帐号的激活
- 使用计算机时的烦恼,电脑,我的烦恼(节选)
- 从零玩转七牛云之CDN-qiniuyunzhicdn
- CentOS yum 安装 EFK 7.17
- Android仿微信视频群聊,Android 仿钉钉、微信 群聊组合头像