理解:chrono & 定时器
作者:互联网
文章目录
理解
c++目前没有自己的定时器,需要借助其他工具例如:QT、Boost、ROS或者系统自带方式,实现方式见其他文件夹
chrono是在std下的,它的头文件问#include,是一个处理时间的元素。
主要包括三个概念:
-
Durantions:时间段
-
Time points:时间点
-
Clocks:包括system_clock(系统时钟),steady_clock(稳定时钟);high_resolution_clock(高精度时钟);
Durations
template <class Rep, class Period = ratio<1> > class duration;//Rep表示Period的数量,比如int,float,double//Period表示时间单位,为ratio类型,有默认类型
ratio类型(默认:秒)
template <intmax_t N, intmax_t D = 1> class ratio;
//ratio表示分数值,N表示分子,D表示分母;
//定义自己的period,比如ratio<1,-2>,表示的单位是-0.5秒;
ratio<3600, 1>
hoursratio<60, 1>
minutesratio<1, 1>
secondsratio<1, 1000>
millisecondsratio<1, 1000000>
microsecondsratio<1, 1000000000> nanosecons
Durations&ratio举例
// 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<60*60>> hours_type;
hours_type h_oneday (24); // 24h
seconds_type s_oneday (60*60*24); // 86400s
milliseconds_type ms_oneday (s_oneday); // 86400000ms,强制类型转换
seconds_type s_onehour (60*60); // 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;//count为duration的成员函数,有多少微秒、秒。。
return 0;
}
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 (1000); // 1 second,1000微秒
foo*=60;
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;
//den表示分母,num表示分子,之前就说了ratio的单位是秒,
//所以milliseconds::period::num=1000,milliseconds::period::den = 1;
//同理hours::period::num = 1;hours::period::den = 3600;最好用作比例
std::cout << " seconds.\n";
return 0;
}
Time points
template <class Clock, class Duration = typename Clock::duration> class time_point;
举例:
time_point有一个函数time_from_eproch()用来获得1970年1月1日8:00:00到time_point时间经过的duration。
// time_point constructors
#include <iostream>
#include <chrono>
#include <ctime>
int main ()
{
using namespace std::chrono;
time_point <system_clock,duration<int>> tp_seconds (duration<int>(1));//系统经历了一秒的时间点,系统时间1970年开始算
system_clock::time_point tp (tp_seconds);
std::cout << "1 second since system_clock epoch = ";
std::cout << tp.time_since_epoch().count();//经历的clock
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 0;
}
举例:
经历了多少天
#include <iostream>
#include <ratio>
#include <chrono>
int main ()
{
using namespace std::chrono;
typedef duration<int,std::ratio<60*60*24>> 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 0;
}
Clocks
标签:std,定时器,chrono,理解,time,duration,include,type 来源: https://blog.csdn.net/QLeelq/article/details/111059187