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时间库的更多相关文章

  1. 你可能没听过的11个Python库

    目前,网上已有成千上万个Python包,但几乎没有人能够全部知道它们.单单 PyPi上就有超过47000个包列表. 现在,越来越多的数据科学家开始使用Python,虽然他们从 pandas, scik ...

  2. 比特币源码分析--C++11和boost库的应用

    比特币源码分析--C++11和boost库的应用     我们先停下探索比特币源码的步伐,来分析一下C++11和boost库在比特币源码中的应用.比特币是一个纯C++编写的项目,用到了C++11和bo ...

  3. moment太重? 那就试试miment--一个超轻量级的js时间库

    介绍 Miment 是一个轻量级的时间库(打包压缩后只有1K),没有太多的方法,Miment的设计理念就是让你以几乎为零的成本快速上手,无需一遍一遍的撸文档 由来 首先 致敬一下Moment,非常好用 ...

  4. java时间库Joda-Time

    虽然在java8里面有内置的最新的时间库,但是在java8之前的版本所有的时间操作都得自己写,未免有些繁琐,如果我们不自己封装的话可以用Joda-Time这个时间库,下面写下这个库的具体用法. git ...

  5. ⏰Day.js 2kB超轻量时间库 和Moment.js一样的API

    Moment.js 是一个大而全的 JS 时间库,很大地方便了我们处理日期和时间.但是 Moment.js太重了(200k+ with locals),可能一般项目也只使用到了她几个常用的API.虽然 ...

  6. js非常强大的日历控件fullcalendar.js, 日期时间库: moment.js

    日历控件: https://fullcalendar.io/docs/ https://fullcalendar.io/docs/event_data/events_function/ https:/ ...

  7. moment.js 时间库

    一.概念:    https://www.cnblogs.com/Jimc/p/10591580.html    或    http://momentjs.cn/(官网) 1.Moment.js是一个 ...

  8. Java8新特性探索之新日期时间库

    一.为什么引入新的日期时间库 Java对日期,日历及时间的处理一直以来都饱受诟病,尤其是它决定将java.util.Date定义为可修改的以及将SimpleDateFormat实现成非线程安全的. 关 ...

  9. (原创)c++11中的日期和时间库

    c++11提供了日期时间相关的库chrono,通过chrono相关的库我们可以很方便的处理日期和时间.c++11还提供了字符串的宽窄转换功能,也提供了字符串和数字的相互转换的库.有了这些库提供的便利的 ...

随机推荐

  1. 网易及新浪IP查询接口

    通过IP地址获取对应的地区信息通常有两种方法:1)自己写程序,解析IP对应的地区信息,需要数据库.2)根据第三方提供的API查询获取地区信息. 第一种方法,参见文本<通过纯真IP数据库获取IP地 ...

  2. ASP.NET的分页方法(一)

    要做一个关于分页写法的专题,这是今天的第一讲,自制分页,可能有些代码需要优化,希望大家给出一些中肯的建议 前台使用的repeater绑定的数据: <form id="form1&quo ...

  3. 通过ajax获得json数据后格式的转换

    在有些情况下获取到的json数据可能是string类型的,需要把其格式化为json对象才方便解析. a)原生js通过ajax获取到的json 此时返回的数据默认是string型的,所以需要用eval( ...

  4. ref和out的使用与区别

    out的使用 ————————————————————————————————————————————————— class Program    {        static void Main( ...

  5. android手机ping不通linux的ip地址

    我的linux是装载虚拟机里的,修改虚拟机的网络连接方式为桥接模式即可.

  6. Android ndk下用AssetManager读取assets的资源

    转自:http://www.cppblog.com/johndragon/archive/2012/12/28/196754.html 在使用 cocos2dx 在 Android 上进行游戏开发时, ...

  7. Windows Self Signed Driver

    In particular, Microsoft® instituted a device driver certification process for its Windows® desktop ...

  8. CarDAQ-Plus

    Overview CarDAQ-Plus is the most validated and accepted J2534 device in the world. It has been on th ...

  9. Animated Scroll to Top

    Due to a number of requests, I'm writing a detail tutorial on how to create an animated scroll to to ...

  10. Parallel.ForEach , ThreadPool.QueueUserWorkItem

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...