C++11提供了chrono库,这个库可以处理和时间相关的一些事情。这个库里面主要有3个类:时间间隔Duration、时钟Clocks和时间点Time point。

Duration

 template<class Rep, class Period = std::ratio<> > class duration;

概述

类模板 std::chrono::duration 表示时间间隔。

它由 Rep 类型的计次数和计次周期组成,其中计次周期是一个编译期有理数常量,表示从一个计次到下一个的秒数。

存储于 duration 的数据仅有 Rep 类型的计次数。若 Rep 是浮点数,则 duration 能表示小数的计次数。 Period 被包含为时长类型的一部分,且只在不同时长间转换时使用。

类型说明

rep Rep 表示计次数的算术类型

 template<std::intmax_t Num, std::intmax_t Denom = > class ratio;

period Period 表示每个时钟周期的秒数,其中第一个模板参数Num代表分子,Denom代表分母,分母默认为1,ratio代表的是一个分子除以分母的分数值

对于一些特定的时间值已经有了定义:

 std::chrono::nanoseconds     duration</*至少 64 位的有符号整数类型*/,std::nano>
std::chrono::microseconds duration</*至少 55 位的有符号整数类型*/,std::micro>
std::chrono::milliseconds duration</*至少 45 位的有符号整数类型*/,std::milli>
std::chrono::seconds duration</*至少 35 位的有符号整数类型*/>
std::chrono::minutes duration</*至少 29 位的有符号整数类型*/,std::ratio<>>
std::chrono::hours duration</*至少 23 位的有符号整数类型*/,std::ratio<>>

更直观一些的定义如下:

 typedef duration <Rep, ratio<,>> hours;
typedef duration <Rep, ratio<,>> minutes;
typedef duration <Rep, ratio<,>> seconds;
typedef duration <Rep, ratio<,>> milliseconds;
typedef duration <Rep, ratio<,>> microseconds;
typedef duration <Rep, ratio<,>> nanoseconds;

通过定义这些常用的时间间隔类型能方便在线程休眠这种场景下使用它们:

 std::this_thread::sleep_for(std::chrono::seconds()); //休眠三秒
std::this_thread::sleep_for(std::chrono:: milliseconds ()); //休眠100毫秒

成员函数

1、count:返回此 duration 的计次数,即获取时间间隔的时钟周期个数。如果感到晦涩可以看下面的例子1。

2、operator+、operator-、前/后++、前/后--、operator+=、operator-=、operator*=、operator/=、operator%=:做一些加减乘除的元素。

3、duration_cast:转换 std::chrono::duration 为不同类型 ToDuration 的时长。

例子1:

 #include <iostream>
#include <chrono> int main()
{
using shakes = std::chrono::duration<int, std::ratio<, >>;
using jiffies = std::chrono::duration<int, std::centi>;
using microfortnights = std::chrono::duration<float, std::ratio<,>>;
using nanocenturies = std::chrono::duration<float, std::ratio<,>>; std::chrono::seconds sec(); std::cout << "1 second is:\n"; // 无精度损失的整数尺度转换:无转型
std::cout << std::chrono::microseconds(sec).count() << " microseconds\n"
<< shakes(sec).count() << " shakes\n"
<< jiffies(sec).count() << " jiffies\n"; // 有精度损失的整数尺度转换:需要转型
std::cout << std::chrono::duration_cast<std::chrono::minutes>(sec).count()
<< " minutes\n"; // 浮点尺度转换:无转型
std::cout << microfortnights(sec).count() << " microfortnights\n"
<< nanocenturies(sec).count() << " nanocenturies\n";
}

运行结果:

1 second is:
1000000 microseconds
100000000 shakes
100 jiffies
0 minutes
0.82672 microfortnights
0.316957 nanocenturies

最后的2个比较怪异的时间是这样来的:

>>> 1000.0 / 3155 = 0.31695721077654515,就是说3155秒对应1000个microfortnights
>>> 10000.0 / 12096 = 0.8267195767195767,就是说12096秒对应10000个nanocenturies

例子2:

 #include <iostream>
#include <chrono>
#include <ratio>
#include <thread> using half_second = std::chrono::duration<float, std::ratio<, > >; int main()
{
std::chrono::seconds s{};
std::cout << "use second : " << s.count() << std::endl; half_second ss{};
std::cout << "use half_second : " << ss.count() << std::endl;
std::cout << "use half_second : " << half_second().count() << std::endl;
std::cout << "use half_second : " << half_second(s).count() << std::endl; std::chrono::minutes m{};
std::cout << "use half_second : " << half_second(m).count() << std::endl; std::chrono::system_clock::time_point tt1 = std::chrono::system_clock::now();
std::this_thread::sleep_for(std::chrono::seconds());
std::chrono::system_clock::time_point tt2 = std::chrono::system_clock::now();
std::cout << (tt2-tt1).count()<<" tick count" << "\t" << std::chrono::duration_cast<std::chrono::milliseconds>(tt2-tt1).count() <<std::endl; std::chrono::system_clock::time_point tt3 = std::chrono::system_clock::now();
std::this_thread::sleep_for(half_second());
std::chrono::system_clock::time_point tt4 = std::chrono::system_clock::now();
std::cout << (tt4-tt3).count()<<" tick count" << "\t" << std::chrono::duration_cast<std::chrono::milliseconds>(tt4-tt3).count() <<std::endl; std::chrono::minutes t1( );
std::chrono::seconds t2( );
std::chrono::seconds t3 = t1 - t2;
std::cout << t3.count() << " second" << std::endl; std::cout << std::chrono::duration_cast<std::chrono::minutes>( t3 ).count() << " minutes" << std::endl; return ;
}

运行结果:

use second : 1
use half_second : 1
use half_second : 4
use half_second : 2
use half_second : 120
1000063777 tick count 1000
500060683 tick count 500
540 second
9 minutes

关于自己定义的half_second计时,传入的是数字的话,count就是实际数字,只有传入了其他时间单位的话才能体现出half_second的“半秒”的作用。

还有就是注意sleep_for里面的half_second也是生效了。

本文参考自:

http://www.cnblogs.com/qicosmos/p/3642712.html

http://zh.cppreference.com/w/cpp/chrono

C++11时间操作的更多相关文章

  1. Java Calendar 类的时间操作

    Java Calendar 类的时间操作 标签: javaCalendar时间Date 2013-07-30 17:53 140401人阅读 评论(7) 收藏 举报 分类: 所有(165) Java ...

  2. Flex时间操作

    小弟是Flex新手,最近一段时间领导要求使用Flex开发B/S的一些项目,需要用到时间上的一些操作.上网查询一番好多人都说不好操作,有的甚至非常麻烦.基于此,小弟整理了一些关于Flex时间操作的经验, ...

  3. JAVA中的时间操作

    java中的时间操作不外乎这四种情况: 1.获取当前时间 2.获取某个时间的某种格式 3.设置时间 4.时间的运算 好,下面就针对这四种情况,一个一个搞定. 一.获取当前时间 有两种方式可以获得,第一 ...

  4. Mysql 时间操作

    Mysql 时间操作(当天,昨天,7天,30天,半年,全年,季度) 1 . 查看当天日期 select current_date(); 2. 查看当天时间 select current_time(); ...

  5. Python常用时间操作总结【取得当前时间、时间函数、应用等】转载

    Python常用时间操作总结[取得当前时间.时间函数.应用等] 转载  2017-05-11   作者:清风乐逍遥    我要评论 这篇文章主要介绍了Python常用时间操作,包括取得当前时间.时间函 ...

  6. java中常用的时间操作

    最近项目设计时间的转换和计算,长时间没用时间操作了,感觉手有点生,所以在这里记录一下: Date 常用的方法: getTime() .setTime(): SimpleDateFormate 常用的方 ...

  7. c++-字符串和时间操作

    C++ 字符串 C++ 提供了以下两种类型的字符串表示形式: C 风格字符串 C++ 引入的 string 类类型 C 风格字符串 C 风格的字符串起源于 C 语言,并在 C++ 中继续得到支持.字符 ...

  8. Python基础 | 日期时间操作

    目录 获取时间 时间映射 格式转换 字符串转日期 日期转字符串 unixtime 时间计算 时间偏移 时间差 "日期时间数据"作为三大基础数据类型之一,在数据分析中会经常遇到. 本 ...

  9. Hive 时间操作

    Hive 时间转换 UNIX时间戳概念:因为UNIX时间戳只是一个秒数,一个UNIX时间戳在不同时区看来,时间是不同的.如UNIX时间戳0,在0时区看来是1970-01-01 00:00:00,在东八 ...

随机推荐

  1. 【docker】【redis】2.docker上设置redis集群---Redis Cluster部署【集群服务】【解决在docker中redis启动后,状态为Restarting,日志报错:Configured to not listen anywhere, exiting.问题】【Waiting for the cluster to join...问题】

    参考地址:https://www.cnblogs.com/zhoujinyi/p/6477133.html https://www.cnblogs.com/cxbhakim/p/9151720.htm ...

  2. 降维工具箱drtool

    工具箱下载:http://leelab.googlecode.com/svn/trunk/apps/drtoolbox/ ——————————————————————————————————————— ...

  3. PostgreSQL配置文件--AUTOVACUUM参数

    8 AUTOVACUUM参数 AUTOVACUUM PARAMETERS 8.1 autovacuum 字符型 默认: autovacuum = on Enable autovacuum subpro ...

  4. 12.线程通信CyclicBarrier

    CountDownLatch 监听某个线程的初始化,等待初始化执行完毕后,通知主线程工作.延迟.阻塞的是主线程,在单个线程中. CyclicBarrier 针对多个线程.线程池,多个线程初始化准备之后 ...

  5. 关于TagHelper的那些事情——如何自定义TagHelper(TagHelper基类)

    写在开头 前面介绍了TagHelper的基本概念和内嵌的TagHelpers,想必大家对TagHelper都有一定的了解.TagHelper看上去有点像WebControl,但它不同于WebContr ...

  6. [转]SSIS包的调用方式

    本文转自:http://www.cnblogs.com/lijun4017/archive/2008/12/04/1347701.html 编写简单SSIS包光看MSDN应该就问题不大了,最近几天几个 ...

  7. python Parent.__init()和super(Child, self)的区别

    super函数做的事情 def supper(cls, inst): mro = inst.__class__.mro() return mro[mro.index(cls) + 1] inst生成父 ...

  8. MongoDB分片集群节点状态stateStr:RECOVERING解决

    1.关闭一直处于RECOVERING状态的mongodb server /opt/mongodb/mongodb-linux-x86_64-2.4.8/bin/mongo  127.0.0.1:220 ...

  9. scala类型系统 type关键字

    和c里的type有点像. scala里的类型,除了在定义class,trait,object时会产生类型,还可以通过type关键字来声明类型. type相当于声明一个类型别名: scala> t ...

  10. windows下进程管理常用命令

    1.查看当前正在运行的进程 tasklist 注: /im 后为映像名称参数:/f  为强行终止,可以通过 taskkill /? 查看更多帮助 2.强制杀死映像名称为imagename的进程,映像名 ...