头文件
#include <chrono>
三个概念
Duration(时间段)
概念
表示两个时间点之间的时间差。
时间单位
-
小时(hours):
std::chrono::hours
-
分钟(minutes):
std::chrono::minutes
-
秒(seconds):
std::chrono::seconds
-
毫秒(milliseconds):
std::chrono::milliseconds
-
微秒(microseconds):
std::chrono::microseconds
-
纳秒(nanoseconds):
std::chrono::nanoseconds
时间精度
- 整数类型精度:
std::chrono::duration<int, TimeUnit>
- 长整数类型精度:
std::chrono::duration<long, TimeUnit>
- 浮点类型精度:
std::chrono::duration<float, TimeUnit>
- 双精度类型精度:
std::chrono::duration<double, TimeUnit>
示例1
// 创建一个200毫秒的时间段
std::chrono::duration<int, std::milli> duration1(200);
// 表示5秒的duration,使用长整数类型精度
std::chrono::duration<long, std::seconds> duration2(5);
// 表示2.5秒的duration,使用浮点类型精度
duration<float, std::seconds> duration3(2.5);
// 表示1分钟的duration,使用双精度类型精度
duration<double, std::minutes> duration4(1);
示例2
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
// 创建两个时间点
auto start = std::chrono::steady_clock::now();
std::this_thread::sleep_for(std::chrono::seconds(5)); // 模拟5s耗时操作
auto end = std::chrono::steady_clock::now();
// 计算时间间隔
std::chrono::duration<double> duration = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
// 输出时间间隔
std::cout << "Elapsed time: " << duration.count() << " seconds\n";
return 0;
}
执行结果:
[root@localhost debug]# ./timeTest
Elapsed time: 5.00022 seconds
[root@localhost debug]#
Time point(时间点)
概念
特定时钟上的一个时间。
组成
-
时钟(Clock),时钟类型包括:
- steady_clock(稳定时钟)
- system_clock(系统时钟)
- high_resolution_clock(高分辨率时钟)
-
表示时间的持续时间(Duration)
示例
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
// 使用系统时钟获取当前时间点
// std::chrono::system_clock::time_point currentTime = std::chrono::system_clock::now();
auto currentTime = std::chrono::system_clock::now();
std::this_thread::sleep_for(std::chrono::seconds(2));
auto laterTime = std::chrono::system_clock::now();
// std::chrono::duration<double> duration = std::chrono::duration_cast<std::chrono::duration<double>>(laterTime - currentTime);
auto duration = std::chrono::duration_cast<std::chrono::duration<double>>(laterTime - currentTime);
std::cout << "The duration is: " << duration.count() << std::endl;
auto AfterTime = laterTime + std::chrono::hours(24);
duration = std::chrono::duration_cast<std::chrono::duration<double>>(AfterTime - laterTime);
std::cout << "The duration for 24H is: " << duration.count() << std::endl;
return 0;
}
执行结果:
[root@localhost debug]# ./timeTest
The duration is: 2.00589
The duration for 24H is: 86400
[root@localhost debug]#
Clock(时钟)
概念
提供了基准和刻度。
时钟类型
- system_clock
- system_clock是系统级别的时钟,它表示实时时钟,也就是指示当前时间的时钟。它的时间点是与系统的时钟相关联的,可能受到时钟调整和时区的影响;
- system_clock用于获取当前的系统时间,可以用来进行日常时间计算和显示。它通常被用作默认的时钟类型;
- system_clock的最小时间单位取决于系统,可能是秒、毫秒或微秒;
- steady_clock
- steady_clock是一个单调递增的时钟,不受任何时钟调整或时区的影响。它提供了一个稳定、可靠的时间基准,适合用于测量时间间隔和计算算法的执行时间;
- steady_clock的最小时间单位取决于实现,通常是纳秒或微秒级别;
- high_resolution_clock
- 可用于测量小时间间隔的时钟。它通常使用最高分辨率的时钟源来提供更高的时间精度。在大部分平台上,high_resolution_clock是steady_clock的别名,因此也是一个单调递增的时钟;
- 最小时间单位取决于实现,通常是纳秒或微秒级别;
示例1
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
// std::chrono::steady_clock::time_point steady_start = std::chrono::steady_clock::now();
auto steady_start = std::chrono::steady_clock::now();
std::this_thread::sleep_for(std::chrono::seconds(1));
auto steady_end = std::chrono::steady_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::duration<double>>(steady_end - steady_start);
std::cout << "The steady_clock duration is: " << duration.count() << std::endl;
// std::chrono::high_resolution_clock::time_point high_resolution_start = std::chrono::high_resolution_clock::now();
auto high_resolution_start = std::chrono::high_resolution_clock::now();
std::this_thread::sleep_for(std::chrono::seconds(1));
auto high_resolution_end = std::chrono::high_resolution_clock::now();
duration = std::chrono::duration_cast<std::chrono::duration<double>>(high_resolution_end - high_resolution_start);
std::cout << "The high_resolution_clock duration is: " << duration.count() << std::endl;
return 0;
}
执行结果:
[root@localhost debug.x64-linux-g8]# ./timeTest
The steady_clock duration is: 1.00066
The high_resolution_clock duration is: 1.00085
[root@localhost debug.x64-linux-g8]#
示例2
// 获取当前时间的时间戳
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
auto currentTime = std::chrono::system_clock::now();
auto currentTime_s = std::chrono::time_point_cast<std::chrono::seconds>(currentTime);
auto currentTime_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(currentTime);
auto currentTime_micro = std::chrono::time_point_cast<std::chrono::microseconds>(currentTime);
auto currentTime_ns = std::chrono::time_point_cast<std::chrono::nanoseconds>(currentTime);
auto valueS = currentTime_s.time_since_epoch().count();
auto valueMS = currentTime_ms.time_since_epoch().count();
auto valueMicroS = currentTime_micro.time_since_epoch().count();
auto valueNS = currentTime_ns.time_since_epoch().count();
std::cout << "Seconds: " << valueS << std::endl;
std::cout << "Milliseconds: " << valueMS << std::endl;
std::cout << "Microseconds: " << valueMicroS << std::endl;
std::cout << "Nanoseconds: " << valueNS << std::endl;
return 0;
}
执行结果:文章来源:https://www.toymoban.com/news/detail-763904.html
[root@localhost debug]# ./timeTest
Seconds: 1700544228
Milliseconds: 1700544228873
Microseconds: 1700544228873536
Nanoseconds: 1700544228873536309
[root@localhost debug]#
示例3
// 将当前时间格式化为时间字符串
#include <iostream>
#include <chrono>
#include <iomanip>
int main()
{
auto currentTime = std::chrono::system_clock::now();
std::time_t t = std::chrono::system_clock::to_time_t(currentTime);
std::cout << "CurrentTime: " << std::put_time(std::localtime(&t), "%F %T") << std::endl;
return 0;
}
执行结果:文章来源地址https://www.toymoban.com/news/detail-763904.html
[root@localhost debug]# ./timeTest
CurrentTime: 2023-11-20 14:50:35
[root@localhost debug]#
到了这里,关于c++ 获取当前时间(精确至秒、毫秒和微妙)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!