最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

c++實(shí)現(xiàn)獲取當(dāng)前時(shí)間(精確至秒,毫秒和微妙)

 更新時(shí)間:2023年11月21日 14:39:15   作者:卻道天涼_好個(gè)秋  
這篇文章主要為大家詳細(xì)介紹了c++實(shí)現(xiàn)獲取當(dāng)前時(shí)間(可以精確至秒,毫秒和微妙)的相關(guān)知識,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以參考一下

頭文件

#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ù)字)

    這篇文章主要介紹了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)的實(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迷途指針詳解

    C迷途指針詳解

    這篇文章主要介紹了C迷途指針,迷途指針又稱為懸空指針、野指針,其對C程序的安全性與穩(wěn)定性影響巨大,本文對其原理與檢測方法做了較為詳盡的分析,需要的朋友可以參考下
    2014-09-09
  • C++ 的 format 和 vformat 函數(shù)示例詳解

    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語言編程計(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í)例詳解

    這篇文章主要介紹了C語言單鏈隊(duì)列的表示與實(shí)現(xiàn),對于研究數(shù)據(jù)結(jié)構(gòu)與算法的朋友來說很有參考借鑒價(jià)值,需要的朋友可以參考下
    2014-07-07
  • Qt設(shè)計(jì)與實(shí)現(xiàn)軟鍵盤效果

    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)詳解

    vc控制臺程序關(guān)閉事件時(shí)的處理方式及注意點(diǎn)詳解

    在本篇文章里小編給大家整理的是一篇關(guān)于vc控制臺程序關(guān)閉事件時(shí)的正確處理方式的相關(guān)知識點(diǎn)內(nèi)容,對此有需求的朋友們可以參閱下。
    2021-12-12
  • C++之拼接長字符串問題

    C++之拼接長字符串問題

    這篇文章主要介紹了C++之拼接長字符串問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-08-08
  • 深入淺析C++ traits技術(shù)

    深入淺析C++ traits技術(shù)

    traits就是提取“被傳進(jìn)的對象”對應(yīng)的返回類型,讓同一個(gè)接口實(shí)現(xiàn)對應(yīng)的功能。因?yàn)镾TL的算法和容器是分離的,兩者通過迭代器鏈接,本文通過實(shí)例代碼給大家介紹C++ traits技術(shù),感興趣的朋友一起看看吧
    2021-05-05

最新評論

灵台县| 阳原县| 思茅市| 白银市| 乐清市| 仁化县| 贵阳市| 乌拉特前旗| 博白县| 肥城市| 华安县| 赤壁市| 汶上县| 海伦市| 精河县| 台山市| 望奎县| 永昌县| 荔波县| 昌邑市| 微山县| 台湾省| 绥宁县| 陵川县| 遂溪县| 手游| 大姚县| 涟源市| 黄梅县| 香港 | 碌曲县| 包头市| 平昌县| 赣榆县| 延边| 浦县| 岑巩县| 二连浩特市| 梧州市| 佛坪县| 中卫市|