C++獲取當(dāng)前時(shí)間戳的幾種常用方法
一、 獲取代碼運(yùn)行時(shí)間
#include <iostream> #include <chrono> auto start_time = std::chrono::high_resolution_clock::now(); ... auto end_time = std::chrono::high_resolution_clock::now(); auto duration = std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time); std::cout <<"Running time is : " << duration.count() << "ms" << std::endl;
二、 使用函數(shù)獲取當(dāng)前時(shí)間戳
#include <ctime>
#include <string>
#include <chrono>
#include <sstream>
#include <iostream>
std::string GetCurrentTimeStamp(int time_stamp_type = 0)
{
std::chrono::system_clock::time_point now = std::chrono::system_clock::now();
std::time_t now_time_t = std::chrono::system_clock::to_time_t(now);
std::tm* now_tm = std::localtime(&now_time_t);
char buffer[128];
strftime(buffer, sizeof(buffer), "%F %T", now_tm);
std::ostringstream ss;
ss.fill('0');
std::chrono::milliseconds ms;
std::chrono::microseconds cs;
std::chrono::nanoseconds ns;
switch (time_stamp_type)
{
case 0:
ss << buffer;
break;
case 1:
ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
ss << buffer << ":" << ms.count();
break;
case 2:
ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
cs = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000;
ss << buffer << ":" << ms.count() << ":" << cs.count() % 1000;
break;
case 3:
ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch()) % 1000;
cs = std::chrono::duration_cast<std::chrono::microseconds>(now.time_since_epoch()) % 1000000;
ns = std::chrono::duration_cast<std::chrono::nanoseconds>(now.time_since_epoch()) % 1000000000;
ss << buffer << ":" << ms.count() << ":" << cs.count() % 1000 << ":" << ns.count() % 1000;
break;
default:
ss << buffer;
break;
}
return ss.str();
}
int main()
{
std::cout << GetCurrentTimeStamp(0) << std::endl;
std::cout << GetCurrentTimeStamp(1) << std::endl;
std::cout << GetCurrentTimeStamp(2) << std::endl;
std::cout << GetCurrentTimeStamp(3) << std::endl;
return 0;
}
結(jié)果輸出:
2022-05-27 14:35:58 2022-05-27 14:35:58:879 2022-05-27 14:35:58:879:200 2022-05-27 14:35:58:879:200:100
三、 獲取時(shí)間戳的總結(jié)
1. 使用gettimeofday函數(shù)獲取毫秒級(jí)時(shí)間戳
gettimeofday是Linux系統(tǒng)提供的一個(gè)函數(shù),可以獲取當(dāng)前的時(shí)間(精確到微秒)。通過(guò)這個(gè)函數(shù),我們可以輕松獲取毫秒級(jí)的時(shí)間戳,并將其格式化為人類可讀的日期時(shí)間字符串。
#include <sys/time.h>
#include <ctime>
#include <string>
#include <algorithm>
static std::string getCurrentTime() {
struct timeval tv;
gettimeofday(&tv, NULL);
static const int MAX_BUFFER_SIZE = 128;
char timestamp_str[MAX_BUFFER_SIZE];
time_t sec = static_cast<time_t>(tv.tv_sec);
int ms = static_cast<int>(tv.tv_usec) / 1000;
struct tm tm_time;
localtime_r(&sec, &tm_time);
static const char *formater = "%4d-%02d-%02d %02d:%02d:%02d.%03d";
int wsize = snprintf(timestamp_str, MAX_BUFFER_SIZE, formater,
tm_time.tm_year + 1900, tm_time.tm_mon + 1, tm_time.tm_mday,
tm_time.tm_hour, tm_time.tm_min, tm_time.tm_sec, ms);
timestamp_str[std::min(wsize, MAX_BUFFER_SIZE - 1)] = '\0';
return std::string(timestamp_str);
}
優(yōu)點(diǎn):
- 精確到毫秒。
- 簡(jiǎn)單易用,代碼直觀。
缺點(diǎn):
- 是Linux特有的函數(shù),跨平臺(tái)性較差。
- 隨著時(shí)間的發(fā)展,一些系統(tǒng)可能推薦使用更現(xiàn)代的C++標(biāo)準(zhǔn)庫(kù)函數(shù)。
2. 使用標(biāo)準(zhǔn)庫(kù)chrono獲取毫秒級(jí)時(shí)間戳
C++11引入了<chrono>庫(kù),提供了更加現(xiàn)代和跨平臺(tái)的時(shí)間處理功能。通過(guò)std::chrono,我們可以輕松獲取當(dāng)前時(shí)間,并將其轉(zhuǎn)換為毫秒級(jí)時(shí)間戳。
#include <chrono>
#include <ctime>
#include <string>
#include <iomanip>
#include <sstream>
static std::string getCurrentTime() {
auto now = std::chrono::system_clock::now();
auto now_ms = std::chrono::duration_cast<std::chrono::milliseconds>(now.time_since_epoch());
auto sectime = std::chrono::duration_cast<std::chrono::seconds>(now_ms);
int32_t milliseconds = now_ms.count() % 1000;
std::time_t timet = sectime.count();
struct tm curtime;
localtime_r(&timet, &curtime);
char buffer[64];
sprintf(buffer, "%4d-%02d-%02d %02d:%02d:%02d.%03d", curtime.tm_year + 1900, curtime.tm_mon + 1,
curtime.tm_mday, curtime.tm_hour, curtime.tm_min, curtime.tm_sec, milliseconds);
return std::string(buffer);
}
優(yōu)點(diǎn):
- 跨平臺(tái)性好,適用于所有支持C++11及以上標(biāo)準(zhǔn)的編譯器。
- 提供了豐富的時(shí)間處理功能,如時(shí)間點(diǎn)的加減、持續(xù)時(shí)間的轉(zhuǎn)換等。
缺點(diǎn):
- 相比
gettimeofday,代碼稍微復(fù)雜一些。
3. 使用std::ctime獲取秒級(jí)時(shí)間戳
std::ctime是C++標(biāo)準(zhǔn)庫(kù)中的一個(gè)函數(shù),可以將std::time_t類型的時(shí)間轉(zhuǎn)換為人類可讀的日期時(shí)間字符串。雖然它只能精確到秒,但在某些不需要毫秒級(jí)精度的場(chǎng)景中仍然非常有用。
#include <ctime>
#include <string>
static std::string getCurrentTime0() {
std::time_t result = std::time(nullptr);
std::string ret;
ret.resize(64);
int wsize = sprintf((char *)&ret[0], "%s", std::ctime(&result));
ret.resize(wsize - 1); // 去除末尾的換行符
return ret;
}
優(yōu)點(diǎn):
- 代碼簡(jiǎn)單,易于理解。
- 適用于不需要毫秒級(jí)精度的場(chǎng)景。
缺點(diǎn):
- 精度只能到秒。
- 輸出的字符串包含換行符,需要手動(dòng)去除。
到此這篇關(guān)于C++獲取當(dāng)前時(shí)間戳的幾種常用方法的文章就介紹到這了,更多相關(guān)C++獲取當(dāng)前時(shí)間戳內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C++11運(yùn)算符重載和向量類重載實(shí)例詳解(<<,>>,+,-,*等)
這篇文章主要給大家介紹了關(guān)于C++11運(yùn)算符重載和向量類重載的相關(guān)資料,主要包括<<,>>,+,-,*等,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-07-07
C語(yǔ)言中sizeof函數(shù)踩過(guò)的坑總結(jié)
sizeof是C語(yǔ)言的一種單目操作符,如C語(yǔ)言的其他操作符++、--等。它并不是函數(shù)。sizeof操作符以字節(jié)形式給出了其操作數(shù)的存儲(chǔ)大小。操作數(shù)可以是一個(gè)表達(dá)式或括在括號(hào)內(nèi)的類型名。操作數(shù)的存儲(chǔ)大小由操作數(shù)的類型決定2022-04-04
C語(yǔ)言Turbo C下實(shí)現(xiàn)俄羅斯方塊
這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言Turbo C下寫(xiě)的俄羅斯方塊,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-02-02
C++有限狀態(tài)機(jī)實(shí)現(xiàn)詳解
這篇文章主要為大家詳細(xì)介紹了C++有限狀態(tài)機(jī)的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-10-10
關(guān)于C++靜態(tài)數(shù)據(jù)成員的實(shí)現(xiàn)講解
今天小編就為大家分享一篇關(guān)于關(guān)于C++靜態(tài)數(shù)據(jù)成員的實(shí)現(xiàn)講解,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2018-12-12
深入c++中臨時(shí)對(duì)象的析構(gòu)時(shí)機(jī)的詳解
本篇文章對(duì)c++中臨時(shí)對(duì)象的析構(gòu)時(shí)機(jī)進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05
Qt中QMapIterator檢測(cè)是否為空的實(shí)現(xiàn)
本文主要介紹了Qt中QMapIterator檢測(cè)是否為空的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-10-10
C/C++實(shí)現(xiàn)日期計(jì)算器的示例代碼
本篇文章主要介紹了C/C++實(shí)現(xiàn)日期計(jì)算器的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-09-09

