C++獲取系統(tǒng)時間的三種方法
前言
在 C++ 編程中,獲取系統(tǒng)時間是一項(xiàng)常見任務(wù)。無論是記錄日志、計(jì)算程序運(yùn)行時間,還是實(shí)現(xiàn)計(jì)時功能,都需要獲取當(dāng)前的系統(tǒng)時間。本文將介紹幾種在 C++ 中獲取系統(tǒng)時間的方法,并提供相應(yīng)的代碼示例。
1. 使用 C 標(biāo)準(zhǔn)庫函數(shù)
C++ 兼容 C 標(biāo)準(zhǔn)庫,因此可以使用 C 標(biāo)準(zhǔn)庫中的 time.h 頭文件來獲取系統(tǒng)時間。
1.1 獲取當(dāng)前時間戳
#include <iostream> #include <ctime> int main() { // 獲取當(dāng)前時間戳 time_t now = time(nullptr); // 將時間戳轉(zhuǎn)換為本地時間 tm* localTime = localtime(&now); // 輸出當(dāng)前時間 std::cout << "當(dāng)前時間: " << asctime(localTime); return 0; }
代碼解釋:
- time(nullptr) 返回當(dāng)前的 Unix 時間戳,即從 1970 年 1 月 1 日 00:00:00 UTC 到現(xiàn)在的秒數(shù)。
- localtime(&now) 將時間戳轉(zhuǎn)換為本地時間結(jié)構(gòu)體 tm。
- asctime(localTime) 將 tm 結(jié)構(gòu)體轉(zhuǎn)換為可讀的字符串格式。
1.2 獲取特定時間格式
#include <iostream> #include <ctime> int main() { // 獲取當(dāng)前時間戳 time_t now = time(nullptr); // 將時間戳轉(zhuǎn)換為本地時間 tm* localTime = localtime(&now); // 格式化輸出時間 char buffer[80]; strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localTime); std::cout << "當(dāng)前時間: " << buffer << std::endl; return 0; }
代碼解釋:
strftime
函數(shù)用于將tm
結(jié)構(gòu)體格式化為指定格式的字符串。%Y-%m-%d %H:%M:%S
是格式化字符串,表示年-月-日 時:分:秒。
2. 使用 C++11 標(biāo)準(zhǔn)庫
C++11 引入了 <chrono>
庫,提供了更現(xiàn)代、更靈活的時間處理方式。
#include <iostream> #include <chrono> #include <ctime> int main() { // 獲取當(dāng)前時間點(diǎn) auto now = std::chrono::system_clock::now(); // 將時間點(diǎn)轉(zhuǎn)換為時間戳 std::time_t now_c = std::chrono::system_clock::to_time_t(now); // 將時間戳轉(zhuǎn)換為本地時間 tm* localTime = std::localtime(&now_c); // 輸出當(dāng)前時間 std::cout << "當(dāng)前時間: " << std::put_time(localTime, "%Y-%m-%d %H:%M:%S") << std::endl; return 0; }
代碼解釋:
std::chrono::system_clock::now()
獲取當(dāng)前時間點(diǎn)。std::chrono::system_clock::to_time_t(now)
將時間點(diǎn)轉(zhuǎn)換為時間戳。std::localtime(&now_c)
將時間戳轉(zhuǎn)換為本地時間結(jié)構(gòu)體tm
。std::put_time(localTime, "%Y-%m-%d %H:%M:%S")
將tm
結(jié)構(gòu)體格式化為指定格式的字符串。
2.1 計(jì)算時間差
#include <iostream> #include <chrono> int main() { // 獲取開始時間點(diǎn) auto start = std::chrono::high_resolution_clock::now(); // 模擬一些耗時操作 std::this_thread::sleep_for(std::chrono::seconds(2)); // 獲取結(jié)束時間點(diǎn) auto end = std::chrono::high_resolution_clock::now(); // 計(jì)算時間差 std::chrono::duration<double> diff = end - start; // 輸出時間差 std::cout << "耗時: " << diff.count() << " 秒" << std::endl; return 0; }
代碼解釋:
- std::chrono::high_resolution_clock::now() 獲取當(dāng)前時間點(diǎn)。
- std::this_thread::sleep_for(std::chrono::seconds(2)) 模擬 2 秒的耗時操作。
- std::chrono::duration<double> diff = end - start 計(jì)算時間差。
- diff.count() 返回時間差的秒數(shù)。
3. 使用 Boost 庫
Boost 是一個功能強(qiáng)大的 C++ 庫集合,提供了許多高級功能,包括時間處理。
#include <iostream> #include <boost/date_time/posix_time/posix_time.hpp> int main() { // 獲取當(dāng)前時間 boost::posix_time::ptime now = boost::posix_time::second_clock::local_time(); // 輸出當(dāng)前時間 std::cout << "當(dāng)前時間: " << now << std::endl; return 0; }
代碼解釋:
boost::posix_time::second_clock::local_time()
獲取當(dāng)前本地時間。now
是一個boost::posix_time::ptime
對象,表示時間點(diǎn)。
總結(jié)
本文介紹了三種在 C++ 中獲取系統(tǒng)時間的方法:
- C 標(biāo)準(zhǔn)庫函數(shù):簡單易用,但功能有限。
- C++11
<chrono>
庫:現(xiàn)代、靈活,適合復(fù)雜的時間處理需求。 - Boost 庫:功能強(qiáng)大,適合需要高級時間處理功能的場景。
到此這篇關(guān)于C++獲取系統(tǒng)時間的三種方法的文章就介紹到這了,更多相關(guān)C++獲取系統(tǒng)時間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Qt圖片繪圖類之QPixmap/QImage/QPicture詳解
這篇文章主要為大家詳細(xì)介紹了Qt圖片繪圖類中QPixmap、QImage和QPicture的使用方法,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2023-03-03C++實(shí)現(xiàn)簡單的圖書管理系統(tǒng)
本文給大家分享的是使用C++實(shí)現(xiàn)簡單的圖書管理系統(tǒng)的代碼,本系統(tǒng)采用了面向?qū)ο蟮某绦蛟O(shè)計(jì)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2015-08-08C++ 設(shè)置和獲取當(dāng)前工作路徑的實(shí)現(xiàn)代碼
這篇文章主要介紹了C++ 設(shè)置和獲取當(dāng)前工作路徑的實(shí)現(xiàn)代碼,防止DLL加載不到配置和文件,需要的朋友可以參考下2017-09-09C++?獲取當(dāng)前正在運(yùn)行函數(shù)的名稱
本文主要介紹了C++獲取當(dāng)前正在運(yùn)行函數(shù)的名稱,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-04-04C語言結(jié)構(gòu)體成員賦值的深拷貝與淺拷貝詳解
C語言中的淺拷貝是指在拷貝過程中,對于指針型成員變量只拷貝指針本身,而不拷貝指針?biāo)赶虻哪繕?biāo),它按字節(jié)復(fù)制的。深拷貝除了拷貝其成員本身的值之外,還拷貝成員指向的動態(tài)內(nèi)存區(qū)域內(nèi)容。本文將通過示例和大家詳細(xì)說說C語言的深拷貝與淺拷貝,希望對你有所幫助2022-09-09