当前位置:网站首页>chrono 使用备注
chrono 使用备注
2022-06-24 06:44:00 【雾散睛明】
chrono 使用备注
chrono 是一个命名空间,用于时间相关的库。
包括
Durations:时长类
Durations 的实现类有如下:
hours 时
minutes 分
seconds 秒
milliseconds 毫秒
microseconds 微秒
nanoseconds 纳秒
Time points 时间点,记录某个时刻
Clocks
将时间点与实际物理时间相关联的框架。
该库至少提供三个时钟,它们提供了将当前时间表示为时间点的方法:system_clock(系统时钟)、steady_clock(稳定时钟)和high_resolution_clock(高分辨率时钟)。
Durations 示例:
#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 示例:
// system_clock example
#include <iostream>
#include <ctime>
#include <ratio>
#include <chrono>
//获取当前时间转换成字符串
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;
}
//计算时间间隔
// 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 进行时长转换
更多参考chrono
边栏推荐
- Unity Culling 相关技术
- [GUET-CTF2019]zips
- 图形技术之管线概念
- Commandes de console communes UE
- Reconfiguration of nebula integration testing framework based on BDD theory (Part 2)
- 【MySQL 使用秘籍】克隆数据表、保存查询数据至数据表以及创建临时表
- [WUSTCTF2020]alison_ likes_ jojo
- C code writing specification
- 2.1.1 QML grammar foundation I
- How to delete / select an input method on your computer
猜你喜欢

Face pincher: a hot meta universe stylist

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

相机标定(标定目的、原理)

Only two lines are displayed, and the excess part is displayed with Ellipsis

Shader 常用函数

【008】表格数据逐行筛选,跳出for循环及跳过本次循环思路_#VBA
![[signal recognition] signal modulation classification based on deep learning CNN with matlab code](/img/0b/7475cd4cf2ddce912816ab3c29b914.png)
[signal recognition] signal modulation classification based on deep learning CNN with matlab code

Dichotomous special training

When MFC uses the console, the project path cannot have spaces or Chinese, otherwise an error will be reported. Lnk1342 fails to save the backup copy of the binary file to be edited, etc
![[OGeek2019]babyrop](/img/74/5f93dcee9ea5a562a7fba5c17aab76.png)
[OGeek2019]babyrop
随机推荐
[wustctf2020] climb
[cnpm] tutorial
Commandes de console communes UE
[tips] use the deep learning toolbox of MATLAB deepnetworkdesigner to quickly design
6000多万铲屎官,捧得出一个国产主粮的春天吗?
Étalonnage de la caméra (objectif et principe d'étalonnage)
MySQL case: analysis of full-text indexing
[image fusion] image fusion based on NSST and PCNN with matlab code
atguigu----16-自定义指令
jarvisoj_level2
[OGeek2019]babyrop
What are the dazzling skills of spot gold?
[image segmentation] retinal vessel segmentation based on morphology with matlab code
[mrctf2020] thousand layer routine
[WordPress website] 5 Set code highlight
[WordPress website] 6 Article content copy prevention
相機標定(標定目的、原理)
Global and Chinese market of digital fryer 2022-2028: Research Report on technology, participants, trends, market size and share
Knowledge points of 2022 system integration project management engineer examination: ITSS information technology service
Face pincher: a hot meta universe stylist