c++11 Chrono时间库
c++11 Chrono时间库
http://en.cppreference.com/mwiki/index.php?title=Special%3ASearch&search=chrono
1. 持续时间 duration
template<class Rep, class Period = ratio<1>>
class duration;
class Rep: 滴答数
class Period: 滴答周期,默认1秒
1) 常用方法
count / zero / min / max
支持算术运算 + - * / % ++ -- += -= *= /= %= (不同的Period之间会自动转换)
2) 示例
duration<long, ratio<60>> d1(2); //2分钟
duration<long, ratio<1>> d2(30); //30秒
duration<double, ratio<60>> d3 = d1+d2; //2.5分
count << d3.count() << "minutes" << endl;
3) 标准定义
//X 64 bits表示:有符号整数类型,且位数至少为64位
typedef duration<X 64 bits, nano> nanoseconds; //纳秒
typedef duration<X 55 bits, micro> microseconds; //微秒
typedef duration<X 45 bits, milli> milliseconds; //毫秒
typedef duration<X 35 bits> seconds; //秒
typedef duration<X 29 bits, ratio<60>> minutes; //分
typedef duration<X 23 bits, ratio<3600>> hours; //时
//例
auto t = hours(1) + minutes(2) + seconds(45);
count << seconds(t).count() << endl;
2. 时钟 clock
- system_clock 系统实时时钟的真实时间
- steady_clock time_point绝不递减的时钟
- high_resolution_clock 高精度时钟
1) 常用接口
// 均为静态接口
now() //获取类型为time_point的当前时间点
to_time_t() //将time_point转换为time_t类型
from_time_t() //将time_t转换为time_point类型
localtime可将time_t类型转换为tm表示的本地时间
2) 示例
//获取当前时间
#include <time.h> // time_t, locaktime, tm
#include <iomanip> // put_time
system_clock::time_point tpoint = system_clock::now();
time_t tt = system_clock::to_time_t(tpoint);
tm* t = locaktime(&tt);
cout << put_time(t, "%H:%M:%S") << endl;
//计算代码的执行时间
auto start = system_clock::now();
// ... to do something
auto end = system_clock::now();
auto diff = end - start;
cout << duration<double, milli>(diff).count() << "ms" << endl;
3. 时点 time_point
- time_point是一个时间点,存储相对于纪元(1970/01/01)的一个duration。
- time_since_epoch() 返回当前时间点到纪元的duration。
- 每一个time_point都关联一个clock,创建时需指定clock作为模板参数。
1) 构造函数
time_point(); //通过duration::zero初始化表示关联clock的纪元
time_point(const duration& d); //通过duration初始化表示纪元+d
template<class Duration2>
time_point(const time_point<clock, Duration2>& t); //通过t.time_since_epoch初始化
2) 示例
time_point<steady_clock> tp1;
tp1 += minutes(10);
auto d1 = tp1.time_since_epoch();
duration<double> d2(d1);
cout << d2.count() << "seconds" << endl;
c++11 Chrono时间库的更多相关文章
- 你可能没听过的11个Python库
目前,网上已有成千上万个Python包,但几乎没有人能够全部知道它们.单单 PyPi上就有超过47000个包列表. 现在,越来越多的数据科学家开始使用Python,虽然他们从 pandas, scik ...
- 比特币源码分析--C++11和boost库的应用
比特币源码分析--C++11和boost库的应用 我们先停下探索比特币源码的步伐,来分析一下C++11和boost库在比特币源码中的应用.比特币是一个纯C++编写的项目,用到了C++11和bo ...
- moment太重? 那就试试miment--一个超轻量级的js时间库
介绍 Miment 是一个轻量级的时间库(打包压缩后只有1K),没有太多的方法,Miment的设计理念就是让你以几乎为零的成本快速上手,无需一遍一遍的撸文档 由来 首先 致敬一下Moment,非常好用 ...
- java时间库Joda-Time
虽然在java8里面有内置的最新的时间库,但是在java8之前的版本所有的时间操作都得自己写,未免有些繁琐,如果我们不自己封装的话可以用Joda-Time这个时间库,下面写下这个库的具体用法. git ...
- ⏰Day.js 2kB超轻量时间库 和Moment.js一样的API
Moment.js 是一个大而全的 JS 时间库,很大地方便了我们处理日期和时间.但是 Moment.js太重了(200k+ with locals),可能一般项目也只使用到了她几个常用的API.虽然 ...
- js非常强大的日历控件fullcalendar.js, 日期时间库: moment.js
日历控件: https://fullcalendar.io/docs/ https://fullcalendar.io/docs/event_data/events_function/ https:/ ...
- moment.js 时间库
一.概念: https://www.cnblogs.com/Jimc/p/10591580.html 或 http://momentjs.cn/(官网) 1.Moment.js是一个 ...
- Java8新特性探索之新日期时间库
一.为什么引入新的日期时间库 Java对日期,日历及时间的处理一直以来都饱受诟病,尤其是它决定将java.util.Date定义为可修改的以及将SimpleDateFormat实现成非线程安全的. 关 ...
- (原创)c++11中的日期和时间库
c++11提供了日期时间相关的库chrono,通过chrono相关的库我们可以很方便的处理日期和时间.c++11还提供了字符串的宽窄转换功能,也提供了字符串和数字的相互转换的库.有了这些库提供的便利的 ...
随机推荐
- hdu 1045 Fire Net(最小覆盖点+构图(缩点))
http://acm.hdu.edu.cn/showproblem.php?pid=1045 Fire Net Time Limit:1000MS Memory Limit:32768KB ...
- Cisco 防止SYN Flood 攻击原理
DoS(Denial of Service拒绝服务)和DDoS(Distributed Denial of Service分布式拒绝服务)攻击是大型网站和网络服务器的安全威胁之一.2000年2月,Ya ...
- TdxAlertWindowManager右下角HINT显示控件
带爱像的右下角HINT显示,自动隐藏 function alterInfo: TdxAlertWindowManager;begin if not Assigned(Falter) then be ...
- HDU 2256 Problem of Precision (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2256 最重要的是构建递推式,下面的图是盗来的.貌似这种叫共轭数. #include <iostr ...
- POJ3468 A Simple Problem with Integers(线段树延时标记)
题目地址http://poj.org/problem?id=3468 题目大意很简单,有两个操作,一个 Q a, b 查询区间[a, b]的和 C a, b, c让区间[a, b] 的每一个数+c 第 ...
- UVaLive 6697 Homework Evaluation (DP)
题意:给出一个长字符串,再给一个短字符串,进行匹配,如果第i个恰好匹配,则 +8,:如果不匹配,可以给长或短字符串添加-,先后匹配,这样-3, 连续的长字符串添加-,需要减去一个4:也可不给添加-,则 ...
- How Tomcat Works(三)
上文中描述的简单的服务器是不符合Servlet规范的,所以本文进一步描述一个简单的Servlet容器是怎么实现的 所以我们首先要明白Servlet接口规范,规范有不同版本,本人就先一视同仁了: pub ...
- 编译安装-PHP
一.编译配置选项2 配置帮助表:2 安装目录:2 交叉编译选项:2 特征选项:3 SAPI模块设置:3 普通参数设置:4 扩展参数:4 PEAR相关选项:9 ZEND相关选项:9 TSRM线程安全资源 ...
- java静态代理与动态代理简单分析
原创作品,可以转载,但是请标注出处地址http://www.cnblogs.com/V1haoge/p/5860749.html 1.动态代理(Dynamic Proxy) 代理分为静态代理和动态代理 ...
- 全代码实现ios-3
决定做ios开发的时候,看了很多本关于ios开发的书籍,其中有国内的人写的,也有根据外国的书翻译过来的. 很可惜,这些书里的例子没办法照搬过来,因为Xcode更新换代太快了,而这些书本的内容更新的速度 ...