std::chrono是C++11引入的标准库,用于时间的计算和处理。它按照ISO8601标准定义了多个时间类,例如:duration(持续时间)、time_point(时间点)和clock(时钟)。以下是一些常见的用法:
1. 计算程序运行时间
#include <iostream>
#include <string>
#include <chrono>
#include <unistd.h>
#include <sstream>
#include <iomanip>
#include <thread>
int main(int argc, char *argv[])
{
//1. 计算耗时
auto start = std::chrono::system_clock::now();
//std::chrono::time_point start = std::chrono::system_clock::now();
int32_t j = 0;
for(int i=0;i<10000;i++)
{
j++;
}
//sleep(1);
std::this_thread::sleep_for(std::chrono::seconds(1));
auto end = std::chrono::system_clock::now();
auto diff1 = std::chrono::duration_cast<std::chrono::seconds>(end - start).count(); //秒
auto diff2 = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count(); //毫秒
auto diff3 = std::chrono::duration_cast<std::chrono::microseconds> (end - start).count(); //微妙
auto diff4 = std::chrono::duration_cast<std::chrono::nanoseconds>(end - start).count(); //纳秒
//std::chrono::minutes
//std::chrono::hours
std::cout<<"diff1 is: "<<diff1<<std::endl;
std::cout<<"diff2 is: "<<diff2<<std::endl;
std::cout<<"diff3 is: "<<diff3<<std::endl;
std::cout<<"diff4 is: "<<diff4<<std::endl;
return 0;
}
输出
diff1 is: 1
diff2 is: 1001
diff3 is: 1001608
diff4 is: 1001608504
2. 等待时间
线程的等待时间
std::this_thread::sleep_for(std::chrono::seconds(1));
3. 获取当前时间以及时间转换
获取当前时间
auto nowTime = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(nowTime);
std::cout << "nowTime is: " << std::ctime(&t) << std::endl;
输出
nowTime is: Fri Jun 9 08:01:18 2023
时间转换
在工作中上述的时间不是我们需要的,通常用到的时间为string和int类型:
类型 | 说明 |
---|---|
2023-06-09 08:01:18 | string类型 |
1686297678 | int64类型(秒级,10位) |
1686297678549 | int64类型(毫秒级,13位) |
1686297678549829 | int64类型(微妙级,16位) |
1686297678549829794 | int64类型(纳秒级,19位) |
时间类型的转换
转换类型 | 转换类型 |
---|---|
time_point -> string | time_point -> int64 |
string -> time_point | string -> int64 |
int64 -> string | int64 -> time_point |
测试demo文章来源:https://www.toymoban.com/news/detail-477870.html
time_conversion.hpp文件的下载地址文章来源地址https://www.toymoban.com/news/detail-477870.html
/*
* @brief: example about time format conversion by chrono
* @data: 2023/06/09
* @complie: g++ -g main.cc time_conversion.hpp -o d -std=c++11
* @author: guokerenjian
* @lastEditDate:
*/
#include <iostream>
#include "time_conversion.hpp"
using namespace t_convert;
int main()
{
TimeConvert tc;
auto nowTime = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(nowTime);
std::cout << "nowTime is: " << std::ctime(&t) << std::endl;
//1.time_point -> string
std::cout<<"time_point -> string"<<std::endl;
std::string strTime;
if(tc.timePointToString(nowTime, strTime))
{
std::cout<<"strTime is: "<<strTime<<"\n\n";
}
else
{
std::cout<<"failure"<<std::endl;
}
//2.time_point -> int64
std::cout<<"time_point -> int64"<<std::endl;
int64_t itime{0};
if(tc.timePointToInt64(nowTime, itime, TIME_TYPE::SECOND_TIME)) //秒
{
std::cout<<"itime is: "<<itime<<"\n";
}
if(tc.timePointToInt64(nowTime, itime, TIME_TYPE::MILLISSECOND_TIME)) //毫秒
{
std::cout<<"itime is: "<<itime<<"\n";
}
if(tc.timePointToInt64(nowTime, itime, TIME_TYPE::MICROSECOND_TIME)) //微妙
{
std::cout<<"itime is: "<<itime<<"\n";
}
if(tc.timePointToInt64(nowTime, itime, TIME_TYPE::NANOSECOND_TIME)) //纳秒
{
std::cout<<"itime is: "<<itime<<"\n\n";
}
//3.string -> time_point
std::cout<<"string -> time_point"<<std::endl;
decltype(nowTime) resultTime;
if(tc.stringToTimePoint(strTime, resultTime))
{
std::time_t t_result = std::chrono::system_clock::to_time_t(resultTime);
std::cout << "resultTime is: " << std::ctime(&t_result) <<"\n\n";
}
//4.string -> int64
std::cout<<"string -> int64"<<std::endl;
int64_t time_int{0};
if(tc.stringToInt64(strTime, time_int))
{
std::cout<<"time_int is: "<<time_int<<"\n\n";
}
//5.int64 -> time_point
std::cout<<"int64 -> time_point"<<std::endl;
decltype(nowTime) resultTimeFromeInt;
int64_t time_test{0};
if(tc.timePointToInt64(nowTime, time_test, TIME_TYPE::UNKOWN) && tc.Int64ToTimePoint(time_test,resultTimeFromeInt))
{
std::time_t t_result_int = std::chrono::system_clock::to_time_t(resultTimeFromeInt);
std::cout << "t_result_int is: " << std::ctime(&t_result_int) <<"\n";
}
if(tc.timePointToInt64(nowTime, time_test, TIME_TYPE::MILLISSECOND_TIME) && tc.Int64ToTimePoint(time_test,resultTimeFromeInt))
{
std::time_t t_result_int = std::chrono::system_clock::to_time_t(resultTimeFromeInt);
std::cout << "t_result_int is: " << std::ctime(&t_result_int) <<"\n";
}
if(tc.timePointToInt64(nowTime, time_test, TIME_TYPE::MICROSECOND_TIME) && tc.Int64ToTimePoint(time_test,resultTimeFromeInt))
{
std::time_t t_result_int = std::chrono::system_clock::to_time_t(resultTimeFromeInt);
std::cout << "t_result_int is: " << std::ctime(&t_result_int) <<"\n";
}
if(tc.timePointToInt64(nowTime, time_test, TIME_TYPE::NANOSECOND_TIME) && tc.Int64ToTimePoint(time_test,resultTimeFromeInt))
{
std::time_t t_result_int = std::chrono::system_clock::to_time_t(resultTimeFromeInt);
std::cout << "t_result_int is: " << std::ctime(&t_result_int) <<"\n\n";
}
//6.time_point -> string
std::cout<<"time_point -> string"<<std::endl;
std::string str_time_result;
if(tc.Int64ToString(time_test, str_time_result))
{
std::cout<<"str_time_result is: "<<str_time_result<<std::endl;
}
return 0;
}
到了这里,关于std::chrono时间处理的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!