詳細(xì)介紹下C/C++時(shí)間相關(guān)的那些函數(shù)
本文轉(zhuǎn)載自微信公眾號「程序喵大人」,作者程序喵大人。轉(zhuǎn)載本文請聯(lián)系程序喵大人公眾號。
大家平時(shí)工作,如果有計(jì)算函數(shù)耗時(shí)或者打印當(dāng)前時(shí)間的需求,一定要來看看這篇文章!
首先介紹下C++標(biāo)準(zhǔn)中的chrono庫
chrono是一個(gè)關(guān)于時(shí)間的庫,起源于boost,現(xiàn)在是C++的標(biāo)準(zhǔn),話說現(xiàn)在的C++標(biāo)準(zhǔn)好多都是源于boost,要進(jìn)標(biāo)準(zhǔn)的特性似乎都會(huì)先在boost試驗(yàn)一番。
首先看一下使用chrono簡單計(jì)時(shí)的示例代碼:
- void func() { // 計(jì)時(shí)
- std::chrono::time_point<std::chrono::high_resolution_clock> begin = high_resolution_clock::now();
- std::this_thread::sleep_for(std::chrono::milliseconds(20));
- auto end = high_resolution_clock::now();
- cout << "time " << duration_cast<milliseconds>(end - begin).count() << endl;
- }
chrono中有三個(gè)概念duration、time_point、clock
duration:表示一段時(shí)間,三分鐘、三秒等,它的定義如下:
- template <class _Rep, class _Period = ratio<1>> class duration;
ratio的定義如下:
- template <intmax_t N, intmax_t D = 1> class ratio;
Rep表示數(shù)據(jù)類型,int,long等,Period表示時(shí)間單位,N是分子,D是分母,直接看例子吧:
- using atto = ratio<1, 1000000000000000000LL>;
- using femto = ratio<1, 1000000000000000LL>;
- using pico = ratio<1, 1000000000000LL>;
- using nano = ratio<1, 1000000000>;
- using micro = ratio<1, 1000000>;
- using milli = ratio<1, 1000>;
- using centi = ratio<1, 100>;
- using deci = ratio<1, 10>;
- using deca = ratio<10, 1>;
- using hecto = ratio<100, 1>;
- using kilo = ratio<1000, 1>;
- using mega = ratio<1000000, 1>;
- using giga = ratio<1000000000, 1>;
- using tera = ratio<1000000000000LL, 1>;
- using peta = ratio<1000000000000000LL, 1>;
- using exa = ratio<1000000000000000000LL, 1>;
- using nanoseconds = duration<long long, nano>;
- using microseconds = duration<long long, micro>;
- using milliseconds = duration<long long, milli>;
- using seconds = duration<long long>;
- using minutes = duration<int, ratio<60>>;
- using hours = duration<int, ratio<3600>>;
- using hours2 = duration<int, ratio<3600, 1>>;
- using hours2 = duration<int, ratio<7200, 2>>;
詳細(xì)看完上述例子您也明白了,ratio的默認(rèn)的時(shí)間單位是1秒,以小時(shí)為例,一小時(shí)等于3600秒,3600 / 1 == 7200 / 2 == 3600,所以hours == hours2 == hours3。
標(biāo)準(zhǔn)庫還提供了duration_cast用于轉(zhuǎn)換各種duration。
- template <class _To, class _Rep, class _Period, enable_if_t<_Is_duration_v<_To>, int> = 0>
- constexpr _To duration_cast(const duration<_Rep, _Period>&) noexcept(
- is_arithmetic_v<_Rep>&& is_arithmetic_v<typename _To::rep>);
- template <class _Ty>
- _INLINE_VAR constexpr bool _Is_duration_v = _Is_specialization_v<_Ty, duration>;
- template <class _Ty>
- _INLINE_VAR constexpr bool is_arithmetic_v = // determine whether _Ty is an arithmetic type
- is_integral_v<_Ty> || is_floating_point_v<_Ty>;
函數(shù)看著很繁瑣,直接看看示例代碼吧:
- void func() {
- auto sec = std::chrono::seconds(10);
- auto mill = std::chrono::duration_cast<std::chrono::milliseconds>(sec);
- cout << sec.count() << endl; // 返回多少s
- cout << mill.count() << endl; // 返回多少ms
- }
- 輸出:
- 10
- 10000
time_point:用來表示某個(gè)具體時(shí)間點(diǎn)。
定義如下:
- template <class _Clock, class _Duration = typename _Clock::duration>
- class time_point;
使用方式如下:
- void func() {
- std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp(std::chrono::seconds(12));
- cout << tp.time_since_epoch().count() << endl;
- std::time_t t = system_clock::to_time_t(tp);
- cout << "time " << ctime(&t) << endl;
- }
- 輸出:
- 12000
- time Thu Jan 1 08:00:12 1970
這里有個(gè)函數(shù)time_since_epoch(),表示這個(gè)time_point距離元年也就是1970年1月1日所經(jīng)過的duration。
time_point也有各種表示方式,類似于duration,也提供了轉(zhuǎn)換函數(shù)time_point_cast()。
- void func() {
- time_point<system_clock, milliseconds> tp(seconds(12));
- cout << tp.time_since_epoch().count() << endl;
- time_point<system_clock, seconds> tp2 = time_point_cast<seconds>(tp);
- cout << tp2.time_since_epoch().count() << endl;
- }
- 輸出:
- 12000
- 12
Clocks:
這里的時(shí)鐘大體有三種:
- system_clock
- steady_clock
- high_resolution_clock
system_clock表示當(dāng)前的系統(tǒng)時(shí)鐘,有三個(gè)函數(shù):
- now():表示當(dāng)前時(shí)間的time_point
- to_time_t():將time_point轉(zhuǎn)換成time_t秒
- from_time_t():將time_t轉(zhuǎn)換成time_point
源碼如下:
- struct system_clock { // wraps GetSystemTimePreciseAsFileTime/GetSystemTimeAsFileTime
- using rep = long long;
- using period = ratio_multiply<ratio<_XTIME_NSECS_PER_TICK, 1>, nano>;
- using duration = chrono::duration<rep, period>;
- using time_point = chrono::time_point<system_clock>;
- static constexpr bool is_steady = false;
- _NODISCARD static time_point now() noexcept { // get current time
- return time_point(duration(_Xtime_get_ticks()));
- }
- _NODISCARD static __time64_t to_time_t(const time_point& _Time) noexcept { // convert to __time64_t
- return static_cast<__time64_t>(_Time.time_since_epoch().count() / _XTIME_TICKS_PER_TIME_T);
- }
- _NODISCARD static time_point from_time_t(__time64_t _Tm) noexcept { // convert from __time64_t
- return time_point(duration(_Tm * _XTIME_TICKS_PER_TIME_T));
- }
- };
steady_clock表示穩(wěn)定的時(shí)鐘,它只有一個(gè)函數(shù),就是now(),后一次調(diào)用now()肯定比上一次調(diào)用now()的返回值大,不受系統(tǒng)時(shí)間修改的影響。
源碼如下:
- struct steady_clock { // wraps QueryPerformanceCounter
- using rep = long long;
- using period = nano;
- using duration = nanoseconds;
- using time_point = chrono::time_point<steady_clock>;
- static constexpr bool is_steady = true;
- _NODISCARD static time_point now() noexcept { // get current time
- const long long _Freq = _Query_perf_frequency(); // doesn't change after system boot
- const long long _Ctr = _Query_perf_counter();
- static_assert(period::num == 1, "This assumes period::num == 1.");
- const long long _Whole = (_Ctr / _Freq) * period::den;
- const long long _Part = (_Ctr % _Freq) * period::den / _Freq;
- return time_point(duration(_Whole + _Part));
- }
- };
使用方式和之前的都相同:
- void func() { // 計(jì)時(shí)
- std::chrono::time_point<std::chrono::steady_clock> begin = steady_clock::now();
- std::this_thread::sleep_for(std::chrono::milliseconds(20));
- auto end = steady_clock::now();
- cout << "time " << duration_cast<milliseconds>(end - begin).count() << endl;
- }
high_resolution_clock表示高精度時(shí)鐘,是系統(tǒng)可用的最高精度的時(shí)鐘,它其實(shí)就是system_clock或者steady_clock的別名:
- using high_resolution_clock = steady_clock;
介紹完了C++的chrono那下面再看下C語言的各種時(shí)間相關(guān)的API吧:
首先可以通過C語言的clock拿到程序執(zhí)行時(shí)處理器所使用的時(shí)鐘數(shù)來計(jì)時(shí):
- clock_t clock(void);
該函數(shù)返回程序執(zhí)行起(一般為程序的開頭),處理器時(shí)鐘所使用的時(shí)間。也獲取 CPU 所使用的秒數(shù),除以 CLOCKS_PER_SEC即可,返回的clock_t其實(shí)就是long類型的重命名。
使用方式如下:
- void func() {
- clock_t start_t = clock();
- cout << start_t << " 個(gè)時(shí)鐘 \n";
- for (int i = 0; i < 100000000; i++) {
- }
- clock_t end_t = clock();
- cout << end_t << " 個(gè)時(shí)鐘 \n";
- cout << "循環(huán)的秒數(shù):" << (double)(end_t - start_t) / CLOCKS_PER_SEC << endl;
- }
如何獲取當(dāng)前時(shí)間戳,單位為秒
- void func() { // 獲取當(dāng)前時(shí)間戳,單位為秒
- struct timeval time;
- gettimeofday(&time, NULL);
- cout << time.tv_sec << " s \n";
- }
也可以使用time函數(shù):
- time_t time(time_t *time);
該函數(shù)返回系統(tǒng)的當(dāng)前日歷時(shí)間,返回的是自1970年1月1日以來所經(jīng)過的秒數(shù)。
time_t其實(shí)就是一個(gè)整數(shù)類型,是int64_t的重命名,該函數(shù)直接使用返回值就好,參數(shù)一般傳空即可。
timer 存取結(jié)果的時(shí)間指針變量,類型為time_t,指針變量可以為null。
如果timer指針非null,則time()函數(shù)返回值變量與timer指針一樣,都指向同一個(gè)內(nèi)存地址;
否則如果timer指針為null,則time()函數(shù)返回一個(gè)time_t變量時(shí)間。
- void func() { // 獲取當(dāng)前時(shí)間戳,單位為秒
- time_t now = time(NULL);
- cout << static_cast<int64_t>(now) << " s \n";
- }
如何獲取當(dāng)前時(shí)間戳?單位為毫秒
- void func() { // 獲取當(dāng)前時(shí)間戳,單位為毫秒
- struct timeval time;
- gettimeofday(&time, NULL);
- cout << time.tv_sec * 1000 + time.tv_usec / 1000 << " ms \n";
- }
如何顯示當(dāng)前的系統(tǒng)時(shí)間呢?可以使用ctime顯示當(dāng)前時(shí)間:
- char* ctime(const time_t* time);
該函數(shù)返回一個(gè)表示當(dāng)?shù)貢r(shí)間的字符串指針,輸出內(nèi)容格式如下:
- day month year hours:minutes:seconds year\n\0。
示例代碼如下:
- void func() {
- time_t now = time(NULL);
- char* dt = ctime(&now);
- cout << "cur time is: " << dt;
- }
- 輸出:
- Tue Sep 22 22:01:40 2020
可以使用tm結(jié)構(gòu)自定義顯示當(dāng)前時(shí)間的格式:
- struct tm * localtime(const time_t * timer);
將日歷時(shí)間轉(zhuǎn)換為本地時(shí)間,從1970年起始的時(shí)間戳轉(zhuǎn)換為1900年起始的時(shí)間數(shù)據(jù)結(jié)構(gòu)
另一個(gè)類似的函數(shù)是gmtime函數(shù):
- struct tm *gmtime(const time_t *time);
只是該函數(shù)返回的是UTC時(shí)間,協(xié)調(diào)世界時(shí)(UTC)也被稱為格林尼治標(biāo)準(zhǔn)時(shí)間(GMT)。
tm結(jié)構(gòu)如下:
- struct tm {
- int tm_sec; // 秒,正常范圍從 0 到 59,但允許至 61
- int tm_min; // 分,范圍從 0 到 59
- int tm_hour; // 小時(shí),范圍從 0 到 23
- int tm_mday; // 一月中的第幾天,范圍從 1 到 31
- int tm_mon; // 月,范圍從 0 到 11
- int tm_year; // 自 1900 年起的年數(shù)
- int tm_wday; // 一周中的第幾天,范圍從 0 到 6,從星期日算起
- int tm_yday; // 一年中的第幾天,范圍從 0 到 365,從 1 月 1 日算起
- int tm_isdst; // 夏令時(shí)
- };
tm_sec 在C89的范圍是[0-61],在C99更正為[0-60]。通常范圍是[0-59],貌似有些系統(tǒng)會(huì)出現(xiàn)60秒的跳躍。
tm_mon 是從零開始的,所以一月份為0,十二月份為11。
tm_year是從1900年開始計(jì)算,所以顯示年份的時(shí)候需要加上1900
- void func() {
- time_t rawtime = time(NULL);
- struct tm* ptminfo = localtime(&rawtime);
- printf("cur time is: %02d-%02d-%02d %02d:%02d:%02d\n", ptminfo->tm_year + 1900, ptminfo->tm_mon + 1,
- ptminfo->tm_mday, ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
- ptminfo = gmtime(&rawtime);
- printf("cur time is: %02d-%02d-%02d %02d:%02d:%02d\n", ptminfo->tm_year + 1900, ptminfo->tm_mon + 1,
- ptminfo->tm_mday, ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec);
- }
- 輸出:
- cur time is: 2020-09-23 21:27:37
- cur time is: 2020-09-23 13:27:37
可以通過asctime顯示tm結(jié)構(gòu)的時(shí)間:
- char * asctime ( const struct tm * time );
和ctime類似,返回的都是一個(gè)固定時(shí)間格式的字符串,只是傳入的參數(shù)不同。
- void func() {
- time_t rawtime = time(NULL);
- struct tm* info1 = localtime(&rawtime);
- cout << "正常 日期和時(shí)間:" << asctime(info1) << endl;
- info1 = gmtime(&rawtime);
- cout << "UTC 日期和時(shí)間:" << asctime(info1) << endl;
- }
- 輸出:
- 正常 日期和時(shí)間:Wed Sep 23 21:47:44 2020
- UTC 日期和時(shí)間:Wed Sep 23 13:47:44 2020
也可以使用strftime()函數(shù),該函數(shù)可用于格式化日期和時(shí)間為指定的格式,如果產(chǎn)生的 C 字符串小于 size 個(gè)字符(包括空結(jié)束字符),則會(huì)返回復(fù)制到 str 中的字符總數(shù)(不包括空結(jié)束字符),否則返回零。
- size_t strftime(
- char *str, // 指向目標(biāo)數(shù)組的指針,用來復(fù)制產(chǎn)生的C字符串
- size_t maxsize, // 最多傳出字符數(shù)量
- const char *format, // 格式化方式
- const struct tm *timeptr // tm指針
- );
format格式如下:
- %a 星期幾的縮寫
- %A 星期幾的全稱
- %b 月份的縮寫
- %B 月份的全稱
- %c 標(biāo)準(zhǔn)的日期的時(shí)間串
- %C 年份的前兩位數(shù)字
- %d 十進(jìn)制表示的每月的第幾天(值從1到31)
- %D 月/天/年
- %e 在兩字符域中,十進(jìn)制表示的每月的第幾天
- %F 年-月-日
- %g 年份的后兩位數(shù)字,使用基于周的年
- %G 年份,使用基于周的年
- %h 簡寫的月份名
- %H 24小時(shí)制的小時(shí)(值從0到23)
- %I 12小時(shí)制的小時(shí)(值從1到12)
- %j 十進(jìn)制表示的每年的第幾天(值從1到366)
- %m 十進(jìn)制表示的月份(值從1到12)
- %M 十時(shí)制表示的分鐘數(shù)(值從0到59)
- %n 換行符
- %p 本地的AM或PM的等價(jià)顯示
- %r 12小時(shí)的時(shí)間
- %R 顯示小時(shí)和分鐘:hh:mm
- %S 十進(jìn)制的秒數(shù)(值從0到61)
- %t 水平制表符
- %T 顯示時(shí)分秒:hh:mm:ss
- %u 每周的第幾天,星期一為第一天 (值從1到7,星期一為1)
- %U 第年的第幾周,把星期日作為第一天(值從0到53)
- %V 每年的第幾周,使用基于周的年
- %w 十進(jìn)制表示的星期幾(值從0到6,星期天為0)
- %W 每年的第幾周,把星期一做為第一天(值從0到53)
- %x 標(biāo)準(zhǔn)的日期串
- %X 標(biāo)準(zhǔn)的時(shí)間串
- %y 不帶世紀(jì)的十進(jìn)制年份(值從0到99)
- %Y 帶世紀(jì)部分的十制年份
- %Z 時(shí)區(qū)名稱,如果不能得到時(shí)區(qū)名稱則返回空字符。
- %% 一個(gè)%符號
使用代碼如下:
- void func() {
- time_t rawtime = time(NULL);
- char buf[256];
- strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&rawtime));
- cout << buf << endl;
- }
參考資料:
https://www.runoob.com/cprogramming/c-function-strftime.html
https://www.runoob.com/cprogramming/c-function-clock.html
https://www.runoob.com/cplusplus/cpp-date-time.html
https://www.cnblogs.com/jwk000/p/3560086.html