欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

頭文件

#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

時間精度

  • 整數(shù)類型精度:std::chrono::duration<int, TimeUnit>
  • 長整數(shù)類型精度:std::chrono::duration<long, TimeUnit>
  • 浮點類型精度:std::chrono::duration<float, TimeUnit>
  • 雙精度類型精度:std::chrono::duration<double, TimeUnit>

示例1

// 創(chuàng)建一個200毫秒的時間段
std::chrono::duration<int, std::milli> duration1(200); 

// 表示5秒的duration,使用長整數(shù)類型精度
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()
{
	    // 創(chuàng)建兩個時間點
    	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;
}

執(zhí)行結果:

[root@localhost debug]# ./timeTest
Elapsed time: 5.00022 seconds
[root@localhost debug]#

Time point(時間點)

概念

特定時鐘上的一個時間。

組成

1.時鐘(Clock),時鐘類型包括:

  • steady_clock(穩(wěn)定時鐘)
  • system_clock(系統(tǒng)時鐘)
  • high_resolution_clock(高分辨率時鐘)

2.表示時間的持續(xù)時間(Duration)

示例

#include <iostream>
#include <chrono>
#include <thread>

int main()
{
	// 使用系統(tǒng)時鐘獲取當前時間點
    // 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í)行結果:

[root@localhost debug]# ./timeTest
The duration is: 2.00589
The duration for 24H is: 86400
[root@localhost debug]#

Clock(時鐘)

概念

提供了基準和刻度。

時鐘類型

1.system_clock

system_clock是系統(tǒng)級別的時鐘,它表示實時時鐘,也就是指示當前時間的時鐘。它的時間點是與系統(tǒng)的時鐘相關聯(lián)的,可能受到時鐘調(diào)整和時區(qū)的影響;

system_clock用于獲取當前的系統(tǒng)時間,可以用來進行日常時間計算和顯示。它通常被用作默認的時鐘類型;

system_clock的最小時間單位取決于系統(tǒng),可能是秒、毫秒或微秒;

2.steady_clock

steady_clock是一個單調(diào)遞增的時鐘,不受任何時鐘調(diào)整或時區(qū)的影響。它提供了一個穩(wěn)定、可靠的時間基準,適合用于測量時間間隔和計算算法的執(zhí)行時間;

steady_clock的最小時間單位取決于實現(xiàn),通常是納秒或微秒級別;

3.high_resolution_clock

可用于測量小時間間隔的時鐘。它通常使用最高分辨率的時鐘源來提供更高的時間精度。在大部分平臺上,high_resolution_clock是steady_clock的別名,因此也是一個單調(diào)遞增的時鐘;

最小時間單位取決于實現(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í)行結果:

[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;
}

執(zhí)行結果:

[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;
}

執(zhí)行結果:

[root@localhost debug]# ./timeTest
CurrentTime: 2023-11-20 14:50:35
[root@localhost debug]#

以上就是c++實現(xiàn)獲取當前時間(精確至秒,毫秒和微妙)的詳細內(nèi)容,更多關于c++獲取時間的資料請關注腳本之家其它相關文章!

相關文章

  • C語言 二級指針詳解及示例代碼

    C語言 二級指針詳解及示例代碼

    本文主要介紹C語言 二級指針,這里整理了C語言中二級指針的基礎資料并附有示例代碼和實現(xiàn)結果,幫助大家學習理解相關知識,有學習的朋友可以參考下
    2016-08-08
  • Opengl?ES之FBO幀緩沖對象使用詳解

    Opengl?ES之FBO幀緩沖對象使用詳解

    這篇文章主要為大家介紹了Opengl?ES之FBO幀緩沖對象使用詳解,<BR>有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2022-09-09
  • C語言中格式化輸出符號%d、%c、%p、%x等詳解

    C語言中格式化輸出符號%d、%c、%p、%x等詳解

    格式化輸出在C語言中非常常用,提供了多種用法來控制輸出的格式,下面這篇文章主要給大家介紹了關于C語言中格式化輸出符號%d、%c、%p、%x等的相關資料,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-06-06
  • C語言實現(xiàn)學生信息管理系統(tǒng)(鏈表)

    C語言實現(xiàn)學生信息管理系統(tǒng)(鏈表)

    這篇文章主要為大家詳細介紹了C語言實現(xiàn)學生信息管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C++實現(xiàn)十進制數(shù)轉(zhuǎn)為其它進制數(shù)

    C++實現(xiàn)十進制數(shù)轉(zhuǎn)為其它進制數(shù)

    這篇文章主要為大家詳細介紹了C++實現(xiàn)十進制數(shù)轉(zhuǎn)為其它進制數(shù),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • 詳解C++之函數(shù)重載

    詳解C++之函數(shù)重載

    這篇文章主要介紹了c++函數(shù)重載的相關知識,文章講解的非常細致,代碼幫助大家更好的理解和學習,感興趣的朋友可以了解下
    2020-06-06
  • Qt編寫自定義控件實現(xiàn)抽獎轉(zhuǎn)盤

    Qt編寫自定義控件實現(xiàn)抽獎轉(zhuǎn)盤

    這篇文章主要為大家詳細介紹了Qt編寫自定義控件實現(xiàn)抽獎轉(zhuǎn)盤,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-06-06
  • C++繼承介紹

    C++繼承介紹

    C++繼承可以是單一繼承或多重繼承,每一個繼承連接可以是public,protected,private也可以是virtual或non-virtual
    2013-01-01
  • 詳解C語言中fseek函數(shù)和ftell函數(shù)的使用方法

    詳解C語言中fseek函數(shù)和ftell函數(shù)的使用方法

    這篇文章主要介紹了C語言中fseek函數(shù)和ftell函數(shù)的使用方法,兩個函數(shù)分別用于設置和返回文件指針stream的位置,需要的朋友可以參考下
    2016-03-03
  • c++中的繼承關系

    c++中的繼承關系

    繼承呈現(xiàn)了面向?qū)ο蟪绦蛟O計的層次結構,體現(xiàn)了由簡單到復雜的認知過程,本文給大家介紹c++中的繼承關系,感興趣的朋友跟隨小編一起看看吧
    2021-07-07

最新評論