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

C++獲取當(dāng)前時(shí)間戳的幾種常用方法

 更新時(shí)間:2025年08月08日 10:58:42   作者:極地星光  
在我們寫(xiě)程序時(shí),通過(guò)寫(xiě)時(shí)間戳可以方便我們進(jìn)行日志記錄、調(diào)試、計(jì)算程序運(yùn)行時(shí)間、緩存過(guò)期控制等,因此下面介紹使用C++代碼獲取時(shí)間戳的方法以及一些常見(jiàn)的應(yīng)用場(chǎng)景,需要的朋友可以參考下

一、 獲取代碼運(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)文章

  • VC中使用ADO開(kāi)發(fā)數(shù)據(jù)庫(kù)應(yīng)用程序簡(jiǎn)明教程

    VC中使用ADO開(kāi)發(fā)數(shù)據(jù)庫(kù)應(yīng)用程序簡(jiǎn)明教程

    這篇文章主要介紹了VC中使用ADO開(kāi)發(fā)數(shù)據(jù)庫(kù)應(yīng)用程序的方法,結(jié)合實(shí)例形式詳細(xì)講述了ADO的原理及VC使用ADO開(kāi)發(fā)數(shù)據(jù)庫(kù)應(yīng)用程序的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2016-06-06
  • 詳解C語(yǔ)言初階之函數(shù)

    詳解C語(yǔ)言初階之函數(shù)

    這篇文章主要介紹了C語(yǔ)言中的初階函數(shù),介紹了其相關(guān)概念,具有一定參考價(jià)值。需要的朋友可以了解下,希望能夠給你帶來(lái)幫助
    2021-11-11
  • 一文詳解QDialog中exec與open的區(qū)別

    一文詳解QDialog中exec與open的區(qū)別

    這篇文章主要為大家詳細(xì)介紹了QDialog中exec與open的區(qū)別,文中的示例代碼講解詳細(xì),對(duì)我們學(xué)習(xí)Qt有一定的幫助,需要的可以參考一下
    2023-03-03
  • C語(yǔ)言利用棧實(shí)現(xiàn)對(duì)后綴表達(dá)式的求解

    C語(yǔ)言利用棧實(shí)現(xiàn)對(duì)后綴表達(dá)式的求解

    這篇文章主要為大家詳細(xì)介紹了C語(yǔ)言利用棧實(shí)現(xiàn)對(duì)后綴表達(dá)式的求解,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-04-04
  • C++ Qt開(kāi)發(fā)之LineEdit單行輸入組件的用法詳解

    C++ Qt開(kāi)發(fā)之LineEdit單行輸入組件的用法詳解

    Qt 是一個(gè)跨平臺(tái)C++圖形界面開(kāi)發(fā)庫(kù),利用Qt可以快速開(kāi)發(fā)跨平臺(tái)窗體應(yīng)用程序,在Qt中我們可以通過(guò)拖拽的方式將不同組件放到指定的位置,實(shí)現(xiàn)圖形化開(kāi)發(fā)極大的方便了開(kāi)發(fā)效率,本章將重點(diǎn)介紹LineEdit單行輸入框組件的常用方法及靈活運(yùn)用
    2023-12-12
  • C語(yǔ)言中的文件讀寫(xiě)fseek 函數(shù)

    C語(yǔ)言中的文件讀寫(xiě)fseek 函數(shù)

    這篇文章主要介紹是我是C語(yǔ)言中的文件讀寫(xiě)fseek 函數(shù)的相關(guān)資料,fseek 函數(shù)用來(lái)移動(dòng)文件流的讀寫(xiě)位置;就好比播放器,可以直接拖拽到精彩的時(shí)間點(diǎn)一樣,下面我們就來(lái)詳細(xì)介紹該內(nèi)容吧,感興趣的小伙伴可以參考一下
    2021-10-10
  • C++讀取到回車換行符問(wèn)題處理

    C++讀取到回車換行符問(wèn)題處理

    有一個(gè)程序只需對(duì)輸入的一行字符一個(gè)個(gè)進(jìn)行獨(dú)立判斷,C的話用getchar()就好了,但是用C++的時(shí)候發(fā)現(xiàn)CIN似乎不接受回車符……搜索解決方法的時(shí)候很多人都建議將getline,然后處理數(shù)組或者定義一個(gè)流什么的,但是這樣一行可能很長(zhǎng),要占用很多空間。有沒(méi)有別的辦法?
    2015-08-08
  • error LNK2019: 無(wú)法解析的外部符號(hào) 問(wèn)題的解決辦法

    error LNK2019: 無(wú)法解析的外部符號(hào) 問(wèn)題的解決辦法

    error LNK2019: 無(wú)法解析的外部符號(hào) 問(wèn)題的解決辦法,需要的朋友可以參考一下
    2013-05-05
  • C語(yǔ)言GetStdHandle函數(shù)使用方法

    C語(yǔ)言GetStdHandle函數(shù)使用方法

    這篇文章介紹了C語(yǔ)言GetStdHandle函數(shù)的使用方法,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-12-12
  • C語(yǔ)言入門(mén)之淺談數(shù)據(jù)類型和變量常量

    C語(yǔ)言入門(mén)之淺談數(shù)據(jù)類型和變量常量

    這篇文章主要為大家介紹了C語(yǔ)言數(shù)據(jù)類型和變量常量,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2022-01-01

最新評(píng)論

札达县| 永福县| 汉川市| 皋兰县| 黄骅市| 肇东市| 抚顺县| 赤壁市| 昌江| 乐山市| 分宜县| 宜州市| 右玉县| 湛江市| 小金县| 阿拉善右旗| 鹤庆县| 理塘县| 南丰县| 丹阳市| 连云港市| 北川| 瑞昌市| 左贡县| 博爱县| 临颍县| 英吉沙县| 三亚市| 襄垣县| 贞丰县| 宁安市| 山东省| 东丽区| 孝昌县| 盐边县| 略阳县| 靖宇县| 肥西县| 临武县| 马山县| 上林县|