c++11新特性 chrono库
文章目录
- chrono库
- 1 duration 模版类
- duration 算术运算
- 2 time_point
- 3 clock
- 3.1 system_clock
- 3.2 steady_clock
- 4 类型转换
- duration_cast \time_point_cost(规则和用法都一样)
chrono库
日期和时间相关的库,主要包含了三个类:时间间隔duration、时钟clocks、时间点time_point
1 duration 模版类
template<class Rep,
class Period = std::ratio<1>
> class duration
其中
-
Rep是整型或者浮点型,代表多少个时间周期
std::chrono::duration<int> sec(1); // 1 sec std::chrono::duration<double> min(2.5); // 2.5sec std::cout << "时间周期:" << min.count() << std::endl;
-
period代表一个周期有多长,也是个模板,定义如下
// 定义于头文件 <ratio> template< std::intmax_t Num, std::intmax_t Denom = 1 > class ratio;
-
ratio表示每个周期有多少秒
-
num是分子,denom是分母,分母默认为1
std::chrono::duration<int,std::ratio<60>> min(1); // 1 min
-
也可以指定分母来计算毫秒,微秒,等
std::chrono::duration<int, std::ratio<1, 1000>> msec(1); // 1 msec
-
duration 算术运算
duration<int, ratio<60,1>> m1(3);
duration<int, ratio<60,1>> m2(1);
auto d1 = m1 - m2; //auto == duration<int, ratio<60,1>>
duration<int, ratio<6,5>> m3(10);
duration<int, ratio<1,3>> m4(3);
auto d2 = m3 - m4; // auto == duration<int, ratio<1, 15>>
算术运算结果的时间单位周期为 ratio<分子的最大公约数,分母的最小数公倍数>
2 time_point
描述 操作 返回值
复合赋值(成员函数) operator+= tp += dtn *this
复合赋值(成员函数) operator-= tp -= dtn *this
算术运算符(非成员函数) operator+ tp + dtn a time_point value
算术运算符(非成员函数) operator+ dtn + tp atime_point value
算术运算符(非成员函数) operator- tp - dtn a time_point value
算术运算符(非成员函数) operator- tp - tp2 aduration value
关系操作符(非成员函数) operator== tp == tp2 a bool value
关系操作符(非成员函数) operator!= tp != tp2 a bool value
关系操作符(非成员函数) operator< tp < tp2 a bool value
关系操作符(非成员函数) operator> tp > tp2 a bool value
关系操作符(非成员函数) operator>= tp >= tp2 a bool value
关系操作符(非成员函数) operator<= tp <= tp2 a bool value
3 clock
- system_clock:系统时钟
- steady_clock:固定时钟,相当于秒表
3.1 system_clock
// 创建时间点
std::chrono::system_clock::time_point epoch; //纪元事件1970年一月一号8:00
// 获得当前时间
std::chrono::system_clock::time_point nowTime = std::chrono::system_clock::now();
// 这时候这个时间点是看不懂的,需要转化成time_t类型,time_t其实是整型数,代表着时间点参数到纪元时间的时间段
time_t tt = std::chrono::system_clock::to_time_t(nowTime);
std::cout << "现在的时间是:" << ctime(&tt) << std::endl;
几个静态成员函数
// 返回表示当前时间的时间点。
static std::chrono::time_point<std::chrono::system_clock> now() noexcept;
// 将 time_point 时间点类型转换为 std::time_t 类型
static std::time_t to_time_t( const time_point& t ) noexcept;
// 将 std::time_t 类型转换为 time_point 时间点类型
static std::chrono::system_clock::time_point from_time_t( std::time_t t ) noexcept;
-
to_time_t :从timepoint转化成time_t,然后可以使用ctime函数来转化成时间
-
from_time_t:从time_t转化到timepoint
3.2 steady_clock
一般用来计算程序的运行时间
auto begin = std::chrono::steady_clock::now();
func();
auto end = std::chrono::steady_clock::now();
auto runtime = end - begin;
std::cout << "程序运行了 " << runtime.count() << " 纳秒" << std::endl;
- 周期时间是一纳秒,系统时间的周期时间是一百纳秒
4 类型转换
duration_cast \time_point_cost(规则和用法都一样)
隐式类型转换的条件:
- 只对时间周期进行转换:原时间周期能整除目标时间周期
- 只对时间周期次数类型进行转换:从低精度到高精度
- 如果都要进行转换,只看次数类型,不看周期
// 第三点
// 精度和时间周期都不相同
// int 分钟 -> double 小时
std::chrono::duration<double, std::ratio<3600>> dh
= std::chrono::minutes (10);
std::cout << dh.count() << std::endl;
除此之外都需要进行显示的类型转换:
// 原时间周期不能整除目标时间周期 分钟到小时
std::chrono::hours hh = std::chrono::duration_cast<std::chrono::hours>(std::chrono::minutes(150));
std::cout << hh.count() << std::endl;
// 高精度到低精度
std::chrono::seconds ss = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::duration<double> (3.2));
std::cout << ss.count() << std::endl;