当前位置:网站首页>Chrono usage notes
Chrono usage notes
2022-06-24 07:48:00 【Fog dispels eyesight】
chrono Use notes
chrono Is a namespace , For time-dependent libraries .
Include
Durations: Duration class
Durations The implementation classes of are as follows :
hours when
minutes branch
seconds second
milliseconds millisecond
microseconds Microsecond
nanoseconds nanosecond
Time points Point in time , Record a moment
Clocks
A framework that associates a point in time with actual physical time .
The library provides at least three clocks , They provide a way to express the current time as a point in time :system_clock( The system clock )、steady_clock( Stabilize the clock ) and high_resolution_clock( High resolution clock ).
Durations Example :
#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;
return 0;
}
// duration::zero
#include <iostream>
#include <chrono>
int main ()
{
using std::chrono::steady_clock;
steady_clock::time_point t1 = steady_clock::now();
std::cout << "Printing out something...\n";
steady_clock::time_point t2 = steady_clock::now();
steady_clock::duration d = t2 - t1;
if ( d == steady_clock::duration::zero() )
std::cout << "The internal clock did not tick.\n";
else
std::cout << "The internal clock advanced " << d.count() << " periods.\n";
return 0;
}
// duration::min/max
#include <iostream>
#include <chrono>
int main ()
{
std::cout << "system_clock durations can represent:\n";
std::cout << "min: " << std::chrono::system_clock::duration::min().count() << "\n";
std::cout << "max: " << std::chrono::system_clock::duration::max().count() << "\n";
return 0;
}
Clocks Example :
// system_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
// Get the current time converted to a string
int main ()
{
using std::chrono::system_clock;
std::chrono::duration<int,std::ratio<60*60*24> > one_day (1);
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 0;
}
// Calculate the time interval
// system_clock::from_time_t
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
int main ()
{
using namespace std::chrono;
// create tm with 1/1/2000:
std::tm timeinfo = std::tm();
timeinfo.tm_year = 100; // year: 2000
timeinfo.tm_mon = 0; // month: january
timeinfo.tm_mday = 1; // day: 1st
std::time_t tt = std::mktime (&timeinfo);
system_clock::time_point tp = system_clock::from_time_t (tt);
system_clock::duration d = system_clock::now() - tp;
// convert to number of days:
typedef duration<int,std::ratio<60*60*24>> days_type;
days_type ndays = duration_cast<days_type> (d);
// display result:
std::cout << ndays.count() << " days have passed since 1/1/2000";
std::cout << std::endl;
return 0;
}
…
duration_cast Time conversion
More reference chrono
边栏推荐
- How to realize multi protocol video capture and output in video surveillance system?
- What should I pay attention to after the live broadcast system source code is set up?
- Climbing 10000 NASA pictures about Mars exploration, I found a secret
- exness:鲍威尔坚持抗通胀承诺,指出衰退是可能的
- 向量操作与坐标转换相关方法
- 行内元素、块元素、行内块元素
- Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its
- Actual target shooting - skillfully use SMB to take down the off-line host
- Thread support
- 自动化测试的生命周期是什么?
猜你喜欢

语料库数据处理个案实例(读取多个文本文件、读取一个文件夹下面指定的多个文件、解码错误、读取多个子文件夹文本、多个文件批量改名)

ImportError: cannot import name ‘process_pdf‘ from ‘pdfminer.pdfinterp‘错误完全解决

Error:Kotlin: Module was compiled with an incompatible version of Kotlin. The binary version of its

exness:鲍威尔坚持抗通胀承诺,指出衰退是可能的

2022年PMP项目管理考试敏捷知识点(1)

GPU is not used when the code is running

Oracle-高级SQL限定查询

光照使用的简单总结

火线,零线,地线,你知道这三根线的作用是什么吗?

【008】表格数据逐行筛选,跳出for循环及跳过本次循环思路_#VBA
随机推荐
『C语言』系统日期&时间
How to realize high stability and high concurrency of live video streaming transmission and viewing?
uniapp uni-app H5 端如何取消 返回按钮的显示 autoBackButton不生效
Global and Chinese market of inline drip irrigation 2022-2028: Research Report on technology, participants, trends, market size and share
光照使用的简单总结
Pyhton crawls to Adu (Li Yifeng) Weibo comments
.jar中没有主清单属性
线程的阻塞问题
Take my brother to make a real-time Leaderboard
Experience of Shenzhou computer
The seminar on "global IPv6 development and outlook 2020-2021" was held in Beijing
Session & cookie details
随机数备注
pair类备注
Maxcompute remote connection, uploading and downloading data files
. No main manifest attribute in jar
What kind of experience is it when the Institute earns 20000 yuan a month!
Event related | reveal how Ti-One's support ability for large-scale events is developed
Wechat cloud hosting hot issues Q & A
【Django中运行scrapy框架,并将数据存入数据库】