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 5510 Bazinga
http://acm.hdu.edu.cn/showproblem.php?pid=5510 Problem Description: Ladies and gentlemen, please sit ...
- JQuery点击收起,点击展开以及部分非空小验证
<tr> <td nowrap align="right" width="18%"> 解决方案: </td> <td ...
- zoj3591 Nim(Nim博弈)
ZOJ 3591 Nim(Nim博弈) 题目意思是说有n堆石子,Alice只能从中选出连续的几堆来玩Nim博弈,现在问Alice想要获胜有多少种方法(即有多少种选择方式). 方法是这样的,由于Nim博 ...
- UI进阶 文件管理器(NSFileManager)文件对接器(NSFileHandle)
一.文件管理器与文件连接器之间的区别 文件管理器(NSFileManager) 此类主要是对文件进行的操作(创建/删除/改名等)以及文件信息的获取. 文件连接器(NSFileHandle) 此类主要是 ...
- ASP.NET设置404页面返回302HTTP状态码的解决方法
在配置文件中配置404页面如下: .代码如下: <customErrors mode="On" defaultRedirect="404.aspx"> ...
- JedisPool使用原理和源代码
1,JedisPool的使用 <!-- 连接池的配置信息 --><beanid="jedisConfig"class="redis.clients.je ...
- erlang pool模块。
出自: http://blog.sina.com.cn/s/blog_96b8a154010168ti.html
- Linux下的TUN/TAP编程
linux下实现虚拟网卡我们在使用VMWARE的虚拟化软件时经常会发现它们能都能虚拟出一个网卡,貌似很神奇的技术,其实在Linux下很简单,有两种虚拟设 备,TUN时点对点的设备,tap表示以太网设备 ...
- 有关获取session属性时报nullPointException(空指针异常)的解决方案
一般我们在从session中获取数据时,需要先进行赋值,也就是必须先进行session.setAttribute(String,Object)方法进行赋值,然后我们才能从session中获取内容,但是 ...
- 【转】Android 属性动画(Property Animation) 完全解析 (上)
http://blog.csdn.net/lmj623565791/article/details/38067475 1.概述 Android提供了几种动画类型:View Animation .Dra ...