所谓的详解只不过是参考www.cplusplus.com的说明整理了一下,因为没发现别人有详细讲解。

  chrono是一个time library, 源于boost,现在已经是C++标准。话说今年似乎又要出新标准了,好期待啊!

  要使用chrono库,需要#include<chrono>,其所有实现均在std::chrono namespace下。注意标准库里面的每个命名空间代表了一个独立的概念。所以下文中的概念均以命名空间的名字表示! chrono是一个模版库,使用简单,功能强大,只需要理解三个概念:duration、time_point、clock

 
1.Durations
std::chrono::duration 表示一段时间,比如两个小时,12.88秒,半个时辰,一炷香的时间等等,只要能换算成秒即可。
 template <class Rep, class Period = ratio<> > class duration;
其中
Rep表示一种数值类型,用来表示Period的数量,比如int float double
Period是ratio类型,用来表示【用秒表示的时间单位】比如second milisecond
常用的duration<Rep,Period>已经定义好了,在std::chrono::duration下:
ratio<3600, 1>                hours
ratio<60, 1>                    minutes
ratio<1, 1>                      seconds
ratio<1, 1000>               microseconds
ratio<1, 1000000>         microseconds
ratio<1, 1000000000>    nanosecons
 
这里需要说明一下ratio这个类模版的原型:
 template <intmax_t N, intmax_t D = > class ratio;
N代表分子,D代表分母,所以ratio表示一个分数值。
注意,我们自己可以定义Period,比如ratio<1, -2>表示单位时间是-0.5秒。
 
由于各种duration表示不同,chrono库提供了duration_cast类型转换函数。
 template <class ToDuration, class Rep, class Period>
constexpr ToDuration duration_cast (const duration<Rep,Period>& dtn);
典型的用法是表示一段时间:
 
 // duration constructor
#include <iostream>
#include <ratio>
#include <chrono> int main ()
{
typedef std::chrono::duration<int> seconds_type;
typedef std::chrono::duration<int,std::milli> milliseconds_type;
typedef std::chrono::duration<int,std::ratio<*>> hours_type; hours_type h_oneday (); // 24h
seconds_type s_oneday (**); // 86400s
milliseconds_type ms_oneday (s_oneday); // 86400000ms seconds_type s_onehour (*); // 3600s
//hours_type h_onehour (s_onehour); // NOT VALID (type truncates), use:
hours_type h_onehour (std::chrono::duration_cast<hours_type>(s_onehour));
milliseconds_type ms_onehour (s_onehour); // 3600000ms (ok, no type truncation) std::cout << ms_onehour.count() << "ms in 1h" << std::endl; return ;
} duration还有一个成员函数count()返回Rep类型的Period数量,看代码: // duration::count
#include <iostream> // std::cout
#include <chrono> // std::chrono::seconds, std::chrono::milliseconds
// std::chrono::duration_cast int main ()
{
using namespace std::chrono;
// std::chrono::milliseconds is an instatiation of std::chrono::duration:
milliseconds foo (); // 1 second
foo*=; std::cout << "duration (in periods): ";
std::cout << foo.count() << " milliseconds.\n"; std::cout << "duration (in seconds): ";
std::cout << foo.count() * milliseconds::period::num / milliseconds::period::den;
std::cout << " seconds.\n"; return ;
}
 
2.Time points
std::chrono::time_point 表示一个具体时间,如上个世纪80年代、你的生日、今天下午、火车出发时间等,只要它能用计算机时钟表示。鉴于我们使用时间的情景不同,这个time point具体到什么程度,由选用的单位决定。一个time point必须有一个clock计时。参见clock的说明。
 
 template <class Clock, class Duration = typename Clock::duration>  class time_point;
 
下面是构造使用time_point的例子:
 // time_point constructors
#include <iostream>
#include <chrono>
#include <ctime> int main ()
{
using namespace std::chrono; system_clock::time_point tp_epoch; // epoch value time_point <system_clock,duration<int>> tp_seconds (duration<int>()); system_clock::time_point tp (tp_seconds); std::cout << "1 second since system_clock epoch = ";
std::cout << tp.time_since_epoch().count();
std::cout << " system_clock periods." << std::endl; // display time_point:
std::time_t tt = system_clock::to_time_t(tp);
std::cout << "time_point tp is: " << ctime(&tt); return ;
}
time_point有一个函数time_from_eproch()用来获得1970年1月1日到time_point时间经过的duration。
举个例子,如果timepoint以天为单位,函数返回的duration就以天为单位。
 
由于各种time_point表示方式不同,chrono也提供了相应的转换函数 time_point_cast。
 template <class ToDuration, class Clock, class Duration>
time_point<Clock,ToDuration> time_point_cast (const time_point<Clock,Duration>& tp);
比如计算
/

 / time_point_cast
#include <iostream>
#include <ratio>
#include <chrono> int main ()
{
using namespace std::chrono; typedef duration<int,std::ratio<**>> days_type; time_point<system_clock,days_type> today = time_point_cast<days_type>(system_clock::now()); std::cout << today.time_since_epoch().count() << " days since epoch" << std::endl; return ;
}
3.Clocks
 
std::chrono::system_clock 它表示当前的系统时钟,系统中运行的所有进程使用now()得到的时间是一致的。
每一个clock类中都有确定的time_point, duration, Rep, Period类型。
操作有:
now() 当前时间time_point
to_time_t() time_point转换成time_t秒
from_time_t() 从time_t转换成time_point
典型的应用是计算时间日期:

 // system_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono> int main ()
{
using std::chrono::system_clock; std::chrono::duration<int,std::ratio<**> > one_day (); system_clock::time_point today = system_clock::now();
system_clock::time_point tomorrow = today + one_day; std::time_t tt; tt = system_clock::to_time_t ( today );
std::cout << "today is: " << ctime(&tt); tt = system_clock::to_time_t ( tomorrow );
std::cout << "tomorrow will be: " << ctime(&tt); return ;
}
std::chrono::steady_clock 为了表示稳定的时间间隔,后一次调用now()得到的时间总是比前一次的值大(这句话的意思其实是,如果中途修改了系统时间,也不影响now()的结果),每次tick都保证过了稳定的时间间隔。
操作有:
now() 获取当前时钟
典型的应用是给算法计时:
 // steady_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono> int main ()
{
using namespace std::chrono; steady_clock::time_point t1 = steady_clock::now(); std::cout << "printing out 1000 stars...\n";
for (int i=; i<; ++i) std::cout << "*";
std::cout << std::endl; steady_clock::time_point t2 = steady_clock::now(); duration<double> time_span = duration_cast<duration<double>>(t2 - t1); std::cout << "It took me " << time_span.count() << " seconds.";
std::cout << std::endl; return ;
}
最后一个时钟,std::chrono::high_resolution_clock 顾名思义,这是系统可用的最高精度的时钟。实际上high_resolution_clock只不过是system_clock或者steady_clock的typedef。
操作有:
now() 获取当前时钟。

chrono库还有几个小特性,但是像这种工具库,本着够用则已的态度,就不求全责备了。
(全文完)
 
 
 
 
 
 
 
 
 
 
 

C++11 std::chrono库详解的更多相关文章

  1. std::thread线程库详解(2)

    目录 目录 简介 最基本的锁 std::mutex 使用 方法和属性 递归锁 std::recursive_mutex 共享锁 std::shared_mutex (C++17) 带超时的锁 总结 简 ...

  2. Struts标签库详解【3】

    struts2标签库详解 要在jsp中使用Struts2的标志,先要指明标志的引入.通过jsp的代码的顶部加入以下的代码: <%@taglib prefix="s" uri= ...

  3. STM32固件库详解

    STM32固件库详解   emouse原创文章,转载请注明出处http://www.cnblogs.com/emouse/ 应部分网友要求,最新加入固件库以及开发环境使用入门视频教程,同时提供例程模板 ...

  4. Python爬虫系列-Urllib库详解

    Urllib库详解 Python内置的Http请求库: * urllib.request 请求模块 * urllib.error 异常处理模块 * urllib.parse url解析模块 * url ...

  5. Lua的协程和协程库详解

    我们首先介绍一下什么是协程.然后详细介绍一下coroutine库,然后介绍一下协程的简单用法,最后介绍一下协程的复杂用法. 一.协程是什么? (1)线程 首先复习一下多线程.我们都知道线程——Thre ...

  6. Python--urllib3库详解1

    Python--urllib3库详解1 Urllib3是一个功能强大,条理清晰,用于HTTP客户端的Python库,许多Python的原生系统已经开始使用urllib3.Urllib3提供了很多pyt ...

  7. MySQL5.6的4个自带库详解

    MySQL5.6的4个自带库详解 1.information_schema详细介绍: information_schema数据库是MySQL自带的,它提供了访问数据库元数据的方式.什么是元数据呢?元数 ...

  8. php中的PDO函数库详解

    PHP中的PDO函数库详解 PDO是一个“数据库访问抽象层”,作用是统一各种数据库的访问接口,与mysql和mysqli的函数库相比,PDO让跨数据库的使用更具有亲和力:与ADODB和MDB2相比,P ...

  9. STM32 HAL库详解 及 手动移植

    源: STM32 HAL库详解 及 手动移植

随机推荐

  1. linq lanbda表达式的用法

    1. 查询Student表中的所有记录的Sname.Ssex和Class列.select sname,ssex,class from studentLinq:    from s in Student ...

  2. rsync.conf详解

    1.在服务端编辑配置文件 [root@game_intf ~]# more /etc/rsyncd.conf port=8730 log file=/var/log/rsync.log pid fil ...

  3. IOS-Uikit框架介绍

    •UIKit可识别三种类型的输入事件: –触摸事件 –运动(加速计)事件 –远程控制事件 IKit框架将触击信息封装为一个UIEvent对象,并派发给恰当的视图(有关UIKit如何将事件递送给您的视图 ...

  4. html与js传json值给php

    //一段js代码 var data = {}, act = [], list = []; $('.set').find('input, textarea').each(function() { act ...

  5. 22中编程语言的HelloWorld

    C:printf("HelloWorld"); C++ : cout<<"HelloWorld"; QBasic : Print "Hel ...

  6. JavaWeb学习--Servlet认识

    Servlet开发 用户在浏览器中输入一个网址并回车,浏览器会向服务器发送一个HTTP请求.服务器端程序接受这个请求,并对请求进行处理,然后发送一个回应.浏览器收到回应,再把回应的内容显示出来.这种请 ...

  7. Yslow网站性能优化工具

    Yslow是一款网站性能优化的插件:

  8. 【转载】有哪些省时小技巧,是每个Linux用户都应该知道的

    http://www.cnblogs.com/amberly/p/4352682.html

  9. 【tornado】系列项目(一)之基于领域驱动模型架构设计的京东用户管理后台

    本博文将一步步揭秘京东等大型网站的领域驱动模型,致力于让读者完全掌握这种网络架构中的“高富帅”. 一.预备知识: 1.接口: python中并没有类似java等其它语言中的接口类型,但是python中 ...

  10. BZOJ1004 [HNOI2008]Cards(Polya计数)

    枚举每个置换,求在每个置换下着色不变的方法数,先求出每个循环的大小,再动态规划求得使用给定的颜色时对应的方法数. dp[i][j][k]表示处理到当前圈时R,B,G使用量为i,j,k时的方法数,背包思 ...