C/C++中的ctime用法总结
头文件time.h为C语言当中的头文件,被C++继承过来。可以结合chrono进行使用。
如果想要了解chrono格式与ctime格式的转换,或者需要学习chrono时间,可以点击链接进行学习。
1. 时间的数据类型
头文件time.h中,总共有四个时间类型clock_t、size_t、time_t和struct tm。下面分别进行介绍。
(1)struct tm
自1900年至今经过的时间:
Member | Type | Meaning | Range |
---|---|---|---|
tm_sec | int | seconds after the minute | 0-60* |
tm_min | int | minutes after the hour | 0-59 |
tm_hour | int | hours since midnight | 0-23 |
tm_mday | int | day of the month | 1-31 |
tm_mon | int | months since January | 0-11 |
tm_year | int | years since 1900 | |
tm_wday | int | days since Sunday | 0-6 |
tm_yday | int | days since January 1 | 0-365 |
tm_isdst | int | Daylight Saving Time flag |
(2)clock_t
- 表示时钟的滴答,可以用ticks 表示。实际是long类型。
- 1秒的clicks为CLOCKS_PER_SEC个ticks,定义为1000个。
(3)time_t
从1900年1月1日0点UTC时间开始的时间, 实际是一个long类型。单位秒。
(4)size_t
size_t类型很多地方都可以使用,实际上是一个unsigned int类型。
总结:最常用的为struct tm和time_t类型。clock_t可以用于精确计算。
2. 时间转换函数
5个时间转换函数功能如下:
函数 | 说明 |
---|---|
asctime | tm 转 string |
ctime | time_t 转 string |
gmtime | UTC时间的time_t 转 tm |
localtime | 本地时间的time_t 转 tm |
strftime | 格式化为string |
mktime | tm 转time_t |
举例说明:
#include <iostream>
#include <time.h> /* time_t, struct tm, difftime, time, mktime */int main ()
{time_t now;time(&now);//获取现在的时间tm to_tm = *localtime(&now);//将刚刚获取的时间,转换为tm格式time_t to_time_t = mktime(&to_tm);//tm 转time_t;std::cout<<"to_time_t: "<<to_time_t<<std::endl;//time_t转tmstd::cout <<"to_tm: "<<to_tm.tm_year<<std::endl;//从1990年经历了多少年std::string thisTime = ctime(&to_time_t);//time_t转string,需要传地址std::cout<<thisTime<<std::endl;thisTime = asctime(&to_tm);//tm转stringstd::cout<<thisTime<<std::endl;return 0;
}
3. 时间操作的函数
有4种处理时间的函数:
(1) time():
- 获取当地时间,返回time_t类型。
- 返回的时间为UTC格林尼治时间1970年1月1日00:00:00到当前时刻的时长,时长单位是秒。
#include <iostream>
#include <time.h>int main ()
{time_t timer;time(&timer); //或者timer = time(NULL),两种方式获取当地时间std::cout << timer <<std::endl;return 0;
}
(2) clock()
获取时钟的clicks,返回clock_t类型。
#include <iostream>
#include <time.h> /* clock_t, clock, CLOCKS_PER_SEC */int main ()
{clock_t t;int f;t = clock();//获取现在的clicksfor (int i=0; i<=10000; ++i)for (int j=0;j<10000;++j){int x = i + j;}t = clock() - t;//获取ticks差std::cout << "Time diff: " << t << " ticks" << std::endl;std::cout << "Time diff: " << ((float)t/CLOCKS_PER_SEC) << " s" <<std::endl;return 0;
}
(3) difftime()
获取时间差的函数,返回double类型。其实可以直接做减法,如下:
#include <iostream>
#include <time.h> /* time_t, struct tm, difftime, time, mktime */
#include <string>int main ()
{time_t t1;time_t t2;struct tm tm1, tm2;double seconds;time(&t1);//获取现在的时间for (int i=0; i<=100000; ++i)for (int j=0;j<100000;++j){int x = i + j;}time(&t2);//获取现在的时间seconds = difftime(t2,t1);//返回double类型int diffTime = t2 - t1;//因为time_t为long类型std::cout << "difftime(): " << seconds <<std::endl;std::cout << "sub: " << diffTime <<std::endl;return 0;
}
参考:http://www.cplusplus.com/reference/ctime/
C/C++中的ctime用法总结相关推荐
- mysql中change用法,mysql 中alter的用法以及一些步骤
mysql 中alter的用法以及一些方法 在用到alter的时间,查到了这个文章,就copy下来了,详细进入下面链接: MySQL之alter语句用法总结 1:删除列 ALTER TABLE [表名 ...
- mysql中正则表达式的用法_Mysql中正则表达式Regexp常见用法
Mysql中Regexp常见用法 模糊匹配,包含特定字符串 # 查找content字段中包含"车友俱乐部"的记录 select * from club_content where ...
- 【C++】C/C++ 中 static 的用法全局变量与局部变量
C/C++ 中 static 的用法全局变量与局部变量 目录 1. 什么是static? 1.1 static 的引入 1.2 静态数据的存储 2. 在 C/C++ 中static的作用 2.1 总的 ...
- php foreach嵌套foreach,php中foreach怎么嵌套foreach PHP中foreach函数用法?
foreach的使用方法小编不是很明确,分享达人指教一下.foreach (array_expressforeach($array as $key) { if(xxxx) { break; //bre ...
- python中str的index什么意思_python中index的用法是什么
index() 一般用处是在序列中检索参数并返回第一次出现的索引,没找到就会报错,比如:>>> t=tuple('Allen') >>> t ('A', 'l', ...
- php foreach是什么,php中foreach的用法是什么
php中foreach的用法是:[foreach ($array as $value) {要执行代码;}],每进行一次循环,当前数组元素的值就会被赋值给$value变量,在进行下一次循环时,将得到数组 ...
- java中的基本用法
java中的基本用法 关键字:专门用途的字符串 所有java关键字都是小写英文 标识符 java常量 java变量 ■ 作用域:起作用的区域■ 使用前必须先声明,在赋值.使用变量名访问这块区域 jav ...
- Java中getResourceAsStream的用法
Java中getResourceAsStream的用法 首先,Java中的getResourceAsStream有以下几种: 1. Class.getResourceAsStream(String p ...
- javascript 中 console 的用法
javascript 中 console 的用法 视频 https://www.bilibili.com/video/BV1g7411L751?from=search&seid=1567656 ...
最新文章
- 在.NET环境下发送邮件
- C/C++面试题目集锦
- java课程实战培训,终获offer
- SAP Spartacus OccEndpointsService调用getBaseEndpoint的一些场景
- Unknown column 'password_lifetime' in 'field list';创建数据库时创建用户,修改用户时报错
- 初探下一代SIEM核心技术发展趋势
- iOS:实现邮件和短信发送的简单示例
- 【VS2010学习笔记】【函数学习】二(SetTimer()函数)
- 数据库接口实验--php实现--
- 网页爬虫实例一(网页截屏)
- 加固机箱设计资料第289篇:基于3U VPX的 5槽加固机箱
- libevent实现TCP 客户端
- Hive建表语句Location与HDFS创建目录
- xposed框架在分机安装失败_pokemon go 和 虚拟gps
- 面阵激光雷达产品调研——Livox Avia
- php pdo 方法,PHP之pdo操作数据库
- 【RMAN】RMAN跨版本恢复(中)
- 数电大作业—简易抢答器设计
- CTF之Avatar
- 分析APP上的缓存与刷新
热门文章
- 一直在路上 | 多位顶尖技术大咖入驻腾讯云最具价值专家
- Java的主要应用领域有哪些?
- Apache中RewriteRule和RewriteCond规则参数的详细介绍
- Linux系统 黏着位(SBIT) 详解
- Windows 10任务栏中托盘区(通知区域)图标消失的解决方法
- 鼠标移到到图片,图片向上滑动预览效果
- 计算机软件的层次结构图,用Word2007制作出精美的层次结构图
- 致远项目管理SPM系统进度控制之进度对比分析
- 打印机显示扫描到当前计算机不可用,打印时显示当前打印机不可用
- sizzle.js学习笔记利用闭包模拟实现数据结构:字典(Map)