c++實(shí)現(xiàn)獲取當(dāng)前時(shí)間(精確至秒,毫秒和微妙)
頭文件
#include <chrono>
三個(gè)概念
Duration(時(shí)間段)
概念
表示兩個(gè)時(shí)間點(diǎn)之間的時(shí)間差。
時(shí)間單位
- 小時(shí)(hours):std::chrono::hours
- 分鐘(minutes):std::chrono::minutes
- 秒(seconds):std::chrono::seconds
- 毫秒(milliseconds):std::chrono::milliseconds
- 微秒(microseconds):std::chrono::microseconds
- 納秒(nanoseconds):std::chrono::nanoseconds
時(shí)間精度
- 整數(shù)類型精度:std::chrono::duration<int, TimeUnit>
- 長整數(shù)類型精度:std::chrono::duration<long, TimeUnit>
- 浮點(diǎn)類型精度:std::chrono::duration<float, TimeUnit>
- 雙精度類型精度:std::chrono::duration<double, TimeUnit>
示例1
// 創(chuàng)建一個(gè)200毫秒的時(shí)間段 std::chrono::duration<int, std::milli> duration1(200); // 表示5秒的duration,使用長整數(shù)類型精度 std::chrono::duration<long, std::seconds> duration2(5); // 表示2.5秒的duration,使用浮點(diǎn)類型精度 duration<float, std::seconds> duration3(2.5); // 表示1分鐘的duration,使用雙精度類型精度 duration<double, std::minutes> duration4(1);
示例2
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
// 創(chuàng)建兩個(gè)時(shí)間點(diǎn)
auto start = std::chrono::steady_clock::now();
std::this_thread::sleep_for(std::chrono::seconds(5)); // 模擬5s耗時(shí)操作
auto end = std::chrono::steady_clock::now();
// 計(jì)算時(shí)間間隔
std::chrono::duration<double> duration = std::chrono::duration_cast<std::chrono::duration<double>>(end - start);
// 輸出時(shí)間間隔
std::cout << "Elapsed time: " << duration.count() << " seconds\n";
return 0;
}
執(zhí)行結(jié)果:
[root@localhost debug]# ./timeTest
Elapsed time: 5.00022 seconds
[root@localhost debug]#
Time point(時(shí)間點(diǎn))
概念
特定時(shí)鐘上的一個(gè)時(shí)間。
組成
1.時(shí)鐘(Clock),時(shí)鐘類型包括:
- steady_clock(穩(wěn)定時(shí)鐘)
- system_clock(系統(tǒng)時(shí)鐘)
- high_resolution_clock(高分辨率時(shí)鐘)
2.表示時(shí)間的持續(xù)時(shí)間(Duration)
示例
#include <iostream>
#include <chrono>
#include <thread>
int main()
{
// 使用系統(tǒng)時(shí)鐘獲取當(dāng)前時(shí)間點(diǎn)
// 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;
}
執(zhí)行結(jié)果:
[root@localhost debug]# ./timeTest
The duration is: 2.00589
The duration for 24H is: 86400
[root@localhost debug]#
Clock(時(shí)鐘)
概念
提供了基準(zhǔn)和刻度。
時(shí)鐘類型
1.system_clock
system_clock是系統(tǒng)級別的時(shí)鐘,它表示實(shí)時(shí)時(shí)鐘,也就是指示當(dāng)前時(shí)間的時(shí)鐘。它的時(shí)間點(diǎn)是與系統(tǒng)的時(shí)鐘相關(guān)聯(lián)的,可能受到時(shí)鐘調(diào)整和時(shí)區(qū)的影響;
system_clock用于獲取當(dāng)前的系統(tǒng)時(shí)間,可以用來進(jìn)行日常時(shí)間計(jì)算和顯示。它通常被用作默認(rèn)的時(shí)鐘類型;
system_clock的最小時(shí)間單位取決于系統(tǒng),可能是秒、毫秒或微秒;
2.steady_clock
steady_clock是一個(gè)單調(diào)遞增的時(shí)鐘,不受任何時(shí)鐘調(diào)整或時(shí)區(qū)的影響。它提供了一個(gè)穩(wěn)定、可靠的時(shí)間基準(zhǔn),適合用于測量時(shí)間間隔和計(jì)算算法的執(zhí)行時(shí)間;
steady_clock的最小時(shí)間單位取決于實(shí)現(xiàn),通常是納秒或微秒級別;
3.high_resolution_clock
可用于測量小時(shí)間間隔的時(shí)鐘。它通常使用最高分辨率的時(shí)鐘源來提供更高的時(shí)間精度。在大部分平臺上,high_resolution_clock是steady_clock的別名,因此也是一個(gè)單調(diào)遞增的時(shí)鐘;
最小時(shí)間單位取決于實(shí)現(xiàn),通常是納秒或微秒級別;
示例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;
}
執(zhí)行結(jié)果:
[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
// 獲取當(dāng)前時(shí)間的時(shí)間戳
#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;
}
執(zhí)行結(jié)果:
[root@localhost debug]# ./timeTest
Seconds: 1700544228
Milliseconds: 1700544228873
Microseconds: 1700544228873536
Nanoseconds: 1700544228873536309
[root@localhost debug]#
示例3
// 將當(dāng)前時(shí)間格式化為時(shí)間字符串
#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;
}
執(zhí)行結(jié)果:
[root@localhost debug]# ./timeTest
CurrentTime: 2023-11-20 14:50:35
[root@localhost debug]#
以上就是c++實(shí)現(xiàn)獲取當(dāng)前時(shí)間(精確至秒,毫秒和微妙)的詳細(xì)內(nèi)容,更多關(guān)于c++獲取時(shí)間的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
C++實(shí)現(xiàn)LeetCode(12.整數(shù)轉(zhuǎn)化成羅馬數(shù)字)
這篇文章主要介紹了C++實(shí)現(xiàn)LeetCode(12.整數(shù)轉(zhuǎn)化成羅馬數(shù)字),本篇文章通過簡要的案例,講解了該項(xiàng)技術(shù)的了解與使用,以下就是詳細(xì)內(nèi)容,需要的朋友可以參考下2021-07-07
C++的實(shí)現(xiàn)優(yōu)先隊(duì)列(Priority?Queue)的實(shí)現(xiàn)
本文主要介紹了C++的實(shí)現(xiàn)優(yōu)先隊(duì)列(Priority?Queue)的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2025-06-06
C++ 的 format 和 vformat 函數(shù)示例詳解
傳統(tǒng)C庫的printf系列函數(shù)存在安全問題,而C++推薦的基于流格式化輸入輸出雖然解決了安全性問題,但在易用性方面仍顯不足,C++11引入了新的C風(fēng)格字符串格式化函數(shù),但類型安全問題依舊存在,下面通過本文介紹C++ 的 format 和 vformat 函數(shù)示例,感興趣的朋友一起看看吧2025-02-02
C語言編程計(jì)算信噪比SNR理解學(xué)習(xí)
這篇文章主要介紹了C語言編程信噪比SNR計(jì)算的理解學(xué)習(xí),信噪比,英文名稱叫做SNR或S/N(SIGNAL-NOISE RATIO)。是指一個(gè)電子設(shè)備或者電子系統(tǒng)中信號與噪聲的比例2021-10-10
C語言單鏈隊(duì)列的表示與實(shí)現(xiàn)實(shí)例詳解
這篇文章主要介紹了C語言單鏈隊(duì)列的表示與實(shí)現(xiàn),對于研究數(shù)據(jù)結(jié)構(gòu)與算法的朋友來說很有參考借鑒價(jià)值,需要的朋友可以參考下2014-07-07
Qt設(shè)計(jì)與實(shí)現(xiàn)軟鍵盤效果
這篇文章主要為大家詳細(xì)介紹了Qt設(shè)計(jì)與實(shí)現(xiàn)軟鍵盤效果的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-11-11
vc控制臺程序關(guān)閉事件時(shí)的處理方式及注意點(diǎn)詳解
在本篇文章里小編給大家整理的是一篇關(guān)于vc控制臺程序關(guān)閉事件時(shí)的正確處理方式的相關(guān)知識點(diǎn)內(nèi)容,對此有需求的朋友們可以參閱下。2021-12-12

