C/C++實(shí)現(xiàn)獲取系統(tǒng)時(shí)間的示例代碼
概述
C 標(biāo)準(zhǔn)庫提供了 time() 函數(shù)與 localtime() 函數(shù)可以獲取到當(dāng)前系統(tǒng)的日歷時(shí)間,但 time() 函數(shù)精度只能到秒級(jí),如果需要更高精度的系統(tǒng)時(shí)間需要使用 gettimeofday() 函數(shù),精度達(dá)到微秒級(jí)。
#include <sys/time.h> int gettimeofday(struct timeval *tv, struct timezone *tz);
tv 參數(shù)是一個(gè) struct timeval 結(jié)構(gòu)體(同樣是在 <sys/time.h> 頭文件中定義):
struct timeval { time_t tv_sec; /* seconds */ suseconds_t tv_usec; /* microseconds */ };
時(shí)區(qū)結(jié)構(gòu)體 struct timezone 的使用已過時(shí),tz 參數(shù)通常應(yīng)指定為 NULL。
函數(shù) localtime() 把 timep 指向的日歷時(shí)間轉(zhuǎn)換為表示本地時(shí)間的細(xì)分時(shí)間。
#include <time.h> ???????struct tm *localtime(const time_t *timep);
localtime() 返回一個(gè)指向 struct tm 對象的指針,它保存了一個(gè)日歷時(shí)間的各組成部分,日歷時(shí)間也被稱為細(xì)分時(shí)間(Broken-down time)。該結(jié)構(gòu)體定義在 <time.h> 頭文件中:
struct tm { int tm_sec; /* Seconds (0-60) */ int tm_min; /* Minutes (0-59) */ int tm_hour; /* Hours (0-23) */ int tm_mday; /* Day of the month (1-31) */ int tm_mon; /* Month (0-11) */ int tm_year; /* Year - 1900 */ int tm_wday; /* Day of the week (0-6, Sunday = 0) */ int tm_yday; /* Day in the year (0-365, 1 Jan = 0) */ int tm_isdst; /* Daylight saving time */ };
tm 結(jié)構(gòu)體的成員:
tm_sec —— 分鐘后的秒數(shù),通常在 59 秒的范圍內(nèi),但最多可以達(dá)到 60 秒,以允許閏秒。
tm_min —— 小時(shí)后的分鐘數(shù),范圍為 0 到 59。
tm_hour —— 午夜過后的小時(shí)數(shù),范圍為 0 到 23。
tm_mday —— 一個(gè)月的某一天,范圍為 1 到 31。
tm_mon —— 自 1 月以來的月份數(shù),范圍為 0 到 11(顯示月份的時(shí)候需要加 1)
tm_year —— 自 1900 年以來的年數(shù)(顯示年份的時(shí)候需要加上 1900)
tm_wday —— 自周日(星期日)以來的天數(shù),范圍為 0 到 6。
tm_yday —— 自 1 月 1 日以來的天數(shù),范圍為 0 到 365。
tm_isdst —— 指示夏時(shí)制在所述時(shí)間是否有效的標(biāo)志。如果夏令時(shí)有效,則該值為正值;如果夏令時(shí)無效,則為零;如果信息不可用,則為負(fù)值。
示例
#include <stdio.h> // included for 'printf()' #include <sys/time.h> // included for 'gettimeofday()' #include <time.h> // included for 'localtime()' int main(int argc, char const *argv[]) { struct timeval tv; gettimeofday(&tv, NULL); time_t sec = tv.tv_sec; suseconds_t usec = tv.tv_usec; struct tm *lt = localtime(&sec); printf("%d-%02d-%02d %02d:%02d:%02d.%03d\n", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, (int)(usec / 1000)); return 0; }
使用 gettimeofday 獲取到保存在 timeval 結(jié)構(gòu)體的時(shí)間之后,通過 localtime 函數(shù)將 tv_sec 轉(zhuǎn)換成 struct tm 結(jié)構(gòu)體,在關(guān)鍵的 tm_year, tm_mon 進(jìn)行特殊處理計(jì)算出當(dāng)前到秒的日歷時(shí)間,然后通過將 tv_usec 微秒數(shù)除以 1000 得到毫秒數(shù)。
在命令行使用 gcc 編譯:
gcc -o main main.c
結(jié)果為帶毫秒數(shù)的當(dāng)前日歷時(shí)間:
$ ./main
2022-12-15 11:03:56.847
易用性封裝
如果需要直接在代碼中獲取當(dāng)前帶毫秒數(shù)的日歷時(shí)間,可以參考以下封裝接口:
使用 C++11 標(biāo)準(zhǔn)的 thread_local 創(chuàng)建一個(gè)線程安全的全局變量,然后將運(yùn)算結(jié)果存儲(chǔ)在全局變量中,最后返回對象的指針,這樣既能保證調(diào)用函數(shù)的易用性,同時(shí)也能兼顧運(yùn)算性能,這種寫法可以非常簡單地應(yīng)用到大部分應(yīng)用中:
thread_local char __timebuf[64] = {0x00}; const char *curtime() { struct timeval tv; gettimeofday(&tv, NULL); struct tm *lt = localtime(&tv.tv_sec); snprintf(__timebuf, sizeof(__timebuf) - 1, "%d-%02d-%02d %02d:%02d:%02d.%03d", lt->tm_year + 1900, lt->tm_mon + 1, lt->tm_mday, lt->tm_hour, lt->tm_min, lt->tm_sec, (int)(tv.tv_usec / 1000)); return __timebuf; }
到此這篇關(guān)于C/C++實(shí)現(xiàn)獲取系統(tǒng)時(shí)間的示例代碼的文章就介紹到這了,更多相關(guān)C++獲取系統(tǒng)時(shí)間內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
C語言詳解關(guān)鍵字sizeof與unsigned及signed的用法
這篇文章主要為大家詳細(xì)介紹了C語言關(guān)鍵字sizeof&&unsigned&&signed,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-06-06C++數(shù)據(jù)結(jié)構(gòu)模板進(jìn)階的多方面分析
今天我要給大家介紹C++中的模板更深的一些知識(shí)。有關(guān)于非類型的模板參數(shù)和模板特化的一些知識(shí),感興趣的朋友快來看看吧2022-02-02C++中strlen(),sizeof()與size()的區(qū)別
本文主要介紹了C++中strlen(),sizeof()與size()的區(qū)別,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05你知道如何自定義sort函數(shù)中的比較函數(shù)
這篇文章主要介紹了如何自定義sort函數(shù)中的比較函數(shù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12