自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

詳細(xì)介紹下C/C++時(shí)間相關(guān)的那些函數(shù)

開發(fā) 后端
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)一番。

[[344240]]

本文轉(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í)的示例代碼:

  1. void func() {  // 計(jì)時(shí) 
  2.    std::chrono::time_point<std::chrono::high_resolution_clock> begin = high_resolution_clock::now(); 
  3.    std::this_thread::sleep_for(std::chrono::milliseconds(20)); 
  4.    auto end = high_resolution_clock::now(); 
  5.    cout << "time " << duration_cast<milliseconds>(end - begin).count() << endl; 

chrono中有三個(gè)概念duration、time_point、clock

duration:表示一段時(shí)間,三分鐘、三秒等,它的定義如下:

  1. template <class _Rep, class _Period = ratio<1>> class duration; 

ratio的定義如下:

  1. template <intmax_t N, intmax_t D = 1> class ratio; 

Rep表示數(shù)據(jù)類型,int,long等,Period表示時(shí)間單位,N是分子,D是分母,直接看例子吧:

  1. using atto  = ratio<1, 1000000000000000000LL>; 
  2. using femto = ratio<1, 1000000000000000LL>; 
  3. using pico  = ratio<1, 1000000000000LL>; 
  4. using nano  = ratio<1, 1000000000>; 
  5. using micro = ratio<1, 1000000>; 
  6. using milli = ratio<1, 1000>; 
  7. using centi = ratio<1, 100>; 
  8. using deci  = ratio<1, 10>; 
  9. using deca  = ratio<10, 1>; 
  10. using hecto = ratio<100, 1>; 
  11. using kilo  = ratio<1000, 1>; 
  12. using mega  = ratio<1000000, 1>; 
  13. using giga  = ratio<1000000000, 1>; 
  14. using tera  = ratio<1000000000000LL, 1>; 
  15. using peta  = ratio<1000000000000000LL, 1>; 
  16. using exa   = ratio<1000000000000000000LL, 1>; 
  17.  
  18. using nanoseconds  = duration<long long, nano>; 
  19. using microseconds = duration<long long, micro>; 
  20. using milliseconds = duration<long long, milli>; 
  21. using seconds      = duration<long long>; 
  22. using minutes      = duration<int, ratio<60>>; 
  23. using hours        = duration<int, ratio<3600>>; 
  24.  
  25. using hours2       = duration<int, ratio<3600, 1>>; 
  26. 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。

  1. template <class _To, class _Rep, class _Period, enable_if_t<_Is_duration_v<_To>, int> = 0> 
  2.    constexpr _To duration_cast(const duration<_Rep, _Period>&) noexcept( 
  3.    is_arithmetic_v<_Rep>&& is_arithmetic_v<typename _To::rep>); 
  4.  
  5. template <class _Ty> 
  6.    _INLINE_VAR constexpr bool _Is_duration_v = _Is_specialization_v<_Ty, duration>; 
  7.  
  8. template <class _Ty> 
  9. _INLINE_VAR constexpr bool is_arithmetic_v = // determine whether _Ty is an arithmetic type 
  10.    is_integral_v<_Ty> || is_floating_point_v<_Ty>; 

函數(shù)看著很繁瑣,直接看看示例代碼吧:

  1. void func() { 
  2.    auto sec = std::chrono::seconds(10); 
  3.    auto mill = std::chrono::duration_cast<std::chrono::milliseconds>(sec); 
  4.    cout << sec.count() << endl; // 返回多少s 
  5.    cout << mill.count() << endl; // 返回多少ms 
  6. 輸出: 
  7. 10 
  8. 10000 

time_point:用來表示某個(gè)具體時(shí)間點(diǎn)。

定義如下:

  1. template <class _Clock, class _Duration = typename _Clock::duration> 
  2.    class time_point; 

使用方式如下:

  1. void func() { 
  2.    std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp(std::chrono::seconds(12)); 
  3.    cout << tp.time_since_epoch().count() << endl; 
  4.    std::time_t t = system_clock::to_time_t(tp); 
  5.    cout << "time " << ctime(&t) << endl; 
  6. 輸出: 
  7. 12000 
  8. 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()。

  1. void func() { 
  2.     time_point<system_clock, milliseconds> tp(seconds(12)); 
  3.     cout << tp.time_since_epoch().count() << endl; 
  4.     time_point<system_clock, seconds> tp2 = time_point_cast<seconds>(tp); 
  5.     cout << tp2.time_since_epoch().count() << endl; 
  6. 輸出: 
  7. 12000 
  8. 12 

Clocks:

這里的時(shí)鐘大體有三種:

  • system_clock
  • steady_clock
  • high_resolution_clock

system_clock表示當(dāng)前的系統(tǒng)時(shí)鐘,有三個(gè)函數(shù):

  1. now():表示當(dāng)前時(shí)間的time_point 
  2. to_time_t():將time_point轉(zhuǎn)換成time_t秒 
  3. from_time_t():將time_t轉(zhuǎn)換成time_point 

源碼如下:

  1. struct system_clock { // wraps GetSystemTimePreciseAsFileTime/GetSystemTimeAsFileTime 
  2.     using rep = long long; 
  3.  
  4.     using period = ratio_multiply<ratio<_XTIME_NSECS_PER_TICK, 1>, nano>; 
  5.  
  6.     using duration                  = chrono::duration<rep, period>; 
  7.     using time_point                = chrono::time_point<system_clock>; 
  8.     static constexpr bool is_steady = false
  9.  
  10.     _NODISCARD static time_point now() noexcept { // get current time 
  11.         return time_point(duration(_Xtime_get_ticks())); 
  12.     } 
  13.  
  14.     _NODISCARD static __time64_t to_time_t(const time_point& _Time) noexcept { // convert to __time64_t 
  15.         return static_cast<__time64_t>(_Time.time_since_epoch().count() / _XTIME_TICKS_PER_TIME_T); 
  16.     } 
  17.  
  18.     _NODISCARD static time_point from_time_t(__time64_t _Tm) noexcept { // convert from __time64_t 
  19.         return time_point(duration(_Tm * _XTIME_TICKS_PER_TIME_T)); 
  20.     } 
  21. }; 

steady_clock表示穩(wěn)定的時(shí)鐘,它只有一個(gè)函數(shù),就是now(),后一次調(diào)用now()肯定比上一次調(diào)用now()的返回值大,不受系統(tǒng)時(shí)間修改的影響。

源碼如下:

  1. struct steady_clock { // wraps QueryPerformanceCounter 
  2.     using rep                       = long long; 
  3.     using period                    = nano; 
  4.     using duration                  = nanoseconds; 
  5.     using time_point                = chrono::time_point<steady_clock>; 
  6.     static constexpr bool is_steady = true
  7.  
  8.     _NODISCARD static time_point now() noexcept { // get current time 
  9.         const long long _Freq = _Query_perf_frequency(); // doesn't change after system boot 
  10.         const long long _Ctr  = _Query_perf_counter(); 
  11.         static_assert(period::num == 1, "This assumes period::num == 1."); 
  12.         const long long _Whole = (_Ctr / _Freq) * period::den; 
  13.         const long long _Part  = (_Ctr % _Freq) * period::den / _Freq; 
  14.         return time_point(duration(_Whole + _Part)); 
  15.     } 
  16. }; 

使用方式和之前的都相同:

  1. void func() {  // 計(jì)時(shí) 
  2.     std::chrono::time_point<std::chrono::steady_clock> begin = steady_clock::now(); 
  3.     std::this_thread::sleep_for(std::chrono::milliseconds(20)); 
  4.     auto end = steady_clock::now(); 
  5.     cout << "time " << duration_cast<milliseconds>(end - begin).count() << endl; 

high_resolution_clock表示高精度時(shí)鐘,是系統(tǒng)可用的最高精度的時(shí)鐘,它其實(shí)就是system_clock或者steady_clock的別名:

  1. using high_resolution_clock = steady_clock; 

介紹完了C++的chrono那下面再看下C語言的各種時(shí)間相關(guān)的API吧:

首先可以通過C語言的clock拿到程序執(zhí)行時(shí)處理器所使用的時(shí)鐘數(shù)來計(jì)時(shí):

  1. clock_t clock(void); 

該函數(shù)返回程序執(zhí)行起(一般為程序的開頭),處理器時(shí)鐘所使用的時(shí)間。也獲取 CPU 所使用的秒數(shù),除以 CLOCKS_PER_SEC即可,返回的clock_t其實(shí)就是long類型的重命名。

使用方式如下:

  1. void func() { 
  2.     clock_t start_t = clock(); 
  3.     cout << start_t << " 個(gè)時(shí)鐘 \n"
  4.     for (int i = 0; i < 100000000; i++) { 
  5.     } 
  6.     clock_t end_t = clock(); 
  7.     cout << end_t << " 個(gè)時(shí)鐘 \n"
  8.     cout << "循環(huán)的秒數(shù):" << (double)(end_t - start_t) / CLOCKS_PER_SEC << endl; 

如何獲取當(dāng)前時(shí)間戳,單位為秒

  1. void func() {  // 獲取當(dāng)前時(shí)間戳,單位為秒 
  2.     struct timeval time
  3.     gettimeofday(&timeNULL); 
  4.     cout << time.tv_sec << " s \n"

也可以使用time函數(shù):

  1. 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í)間。

  1. void func() {  // 獲取當(dāng)前時(shí)間戳,單位為秒 
  2.     time_t now = time(NULL); 
  3.     cout << static_cast<int64_t>(now) << " s \n"

如何獲取當(dāng)前時(shí)間戳?單位為毫秒

  1. void func() {  // 獲取當(dāng)前時(shí)間戳,單位為毫秒 
  2.     struct timeval time
  3.     gettimeofday(&timeNULL); 
  4.     cout << time.tv_sec * 1000 + time.tv_usec / 1000 << " ms \n"

如何顯示當(dāng)前的系統(tǒng)時(shí)間呢?可以使用ctime顯示當(dāng)前時(shí)間:

  1. char* ctime(const time_t* time); 

該函數(shù)返回一個(gè)表示當(dāng)?shù)貢r(shí)間的字符串指針,輸出內(nèi)容格式如下:

  1. day month year hours:minutes:seconds year\n\0。 

示例代碼如下:

  1. void func() { 
  2.     time_t now = time(NULL); 
  3.     char* dt = ctime(&now); 
  4.     cout << "cur time is: " << dt; 
  5. 輸出: 
  6. Tue Sep 22 22:01:40 2020 

可以使用tm結(jié)構(gòu)自定義顯示當(dāng)前時(shí)間的格式:

  1. 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ù):

  1. 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)如下:

  1. struct tm { 
  2.   int tm_sec;   // 秒,正常范圍從 0 到 59,但允許至 61 
  3.   int tm_min;   // 分,范圍從 0 到 59 
  4.   int tm_hour;  // 小時(shí),范圍從 0 到 23 
  5.   int tm_mday;  // 一月中的第幾天,范圍從 1 到 31 
  6.   int tm_mon;   // 月,范圍從 0 到 11 
  7.   int tm_year;  // 自 1900 年起的年數(shù) 
  8.   int tm_wday;  // 一周中的第幾天,范圍從 0 到 6,從星期日算起 
  9.   int tm_yday;  // 一年中的第幾天,范圍從 0 到 365,從 1 月 1 日算起 
  10.   int tm_isdst; // 夏令時(shí) 
  11. }; 

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

  1. void func() { 
  2.     time_t rawtime = time(NULL); 
  3.     struct tm* ptminfo = localtime(&rawtime); 
  4.     printf("cur time is: %02d-%02d-%02d %02d:%02d:%02d\n", ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, 
  5.            ptminfo->tm_mday, ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec); 
  6.     ptminfo = gmtime(&rawtime); 
  7.     printf("cur time is: %02d-%02d-%02d %02d:%02d:%02d\n", ptminfo->tm_year + 1900, ptminfo->tm_mon + 1, 
  8.            ptminfo->tm_mday, ptminfo->tm_hour, ptminfo->tm_min, ptminfo->tm_sec); 
  9. 輸出: 
  10. cur time is: 2020-09-23 21:27:37 
  11. cur time is: 2020-09-23 13:27:37 

可以通過asctime顯示tm結(jié)構(gòu)的時(shí)間:

  1. char * asctime ( const struct tm * time ); 

和ctime類似,返回的都是一個(gè)固定時(shí)間格式的字符串,只是傳入的參數(shù)不同。

  1. void func() { 
  2.     time_t rawtime = time(NULL); 
  3.     struct tm* info1 = localtime(&rawtime); 
  4.     cout << "正常 日期和時(shí)間:" << asctime(info1) << endl; 
  5.     info1 = gmtime(&rawtime); 
  6.     cout << "UTC 日期和時(shí)間:" << asctime(info1) << endl; 
  7. 輸出: 
  8. 正常 日期和時(shí)間:Wed Sep 23 21:47:44 2020 
  9. UTC 日期和時(shí)間:Wed Sep 23 13:47:44 2020 

也可以使用strftime()函數(shù),該函數(shù)可用于格式化日期和時(shí)間為指定的格式,如果產(chǎn)生的 C 字符串小于 size 個(gè)字符(包括空結(jié)束字符),則會(huì)返回復(fù)制到 str 中的字符總數(shù)(不包括空結(jié)束字符),否則返回零。

  1. size_t strftime( 
  2.     char *str, // 指向目標(biāo)數(shù)組的指針,用來復(fù)制產(chǎn)生的C字符串 
  3.     size_t maxsize, // 最多傳出字符數(shù)量 
  4.     const char *format, // 格式化方式 
  5.     const struct tm *timeptr // tm指針 
  6. ); 

format格式如下:

  1. %a 星期幾的縮寫 
  2. %A 星期幾的全稱 
  3. %b 月份的縮寫 
  4. %B 月份的全稱 
  5. %c 標(biāo)準(zhǔn)的日期的時(shí)間串 
  6. %C 年份的前兩位數(shù)字 
  7. %d 十進(jìn)制表示的每月的第幾天(值從1到31) 
  8. %D 月/天/年 
  9. %e 在兩字符域中,十進(jìn)制表示的每月的第幾天 
  10. %F 年-月-日 
  11. %g 年份的后兩位數(shù)字,使用基于周的年 
  12. %G 年份,使用基于周的年 
  13. %h 簡寫的月份名 
  14. %H 24小時(shí)制的小時(shí)(值從0到23) 
  15. %I 12小時(shí)制的小時(shí)(值從1到12) 
  16. %j 十進(jìn)制表示的每年的第幾天(值從1到366) 
  17. %m 十進(jìn)制表示的月份(值從1到12) 
  18. %M 十時(shí)制表示的分鐘數(shù)(值從0到59) 
  19. %n 換行符 
  20. %p 本地的AM或PM的等價(jià)顯示 
  21. %r 12小時(shí)的時(shí)間 
  22. %R 顯示小時(shí)和分鐘:hh:mm 
  23. %S 十進(jìn)制的秒數(shù)(值從0到61) 
  24. %t 水平制表符 
  25. %T 顯示時(shí)分秒:hh:mm:ss 
  26. %u 每周的第幾天,星期一為第一天 (值從1到7,星期一為1) 
  27. %U 第年的第幾周,把星期日作為第一天(值從0到53) 
  28. %V 每年的第幾周,使用基于周的年 
  29. %w 十進(jìn)制表示的星期幾(值從0到6,星期天為0) 
  30. %W 每年的第幾周,把星期一做為第一天(值從0到53) 
  31. %x 標(biāo)準(zhǔn)的日期串 
  32. %X 標(biāo)準(zhǔn)的時(shí)間串 
  33. %y 不帶世紀(jì)的十進(jìn)制年份(值從0到99) 
  34. %Y 帶世紀(jì)部分的十制年份 
  35. %Z 時(shí)區(qū)名稱,如果不能得到時(shí)區(qū)名稱則返回空字符。 
  36. %% 一個(gè)%符號 

使用代碼如下:

  1. void func() { 
  2.     time_t rawtime = time(NULL); 
  3.     char buf[256]; 
  4.     strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", localtime(&rawtime)); 
  5.     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

 

責(zé)任編輯:武曉燕 來源: 程序喵大人
相關(guān)推薦

2011-07-13 11:34:58

CC++時(shí)間函數(shù)

2011-07-20 17:16:50

C++重載函數(shù)

2024-04-01 09:13:20

C++函數(shù)遞增

2011-07-20 13:40:09

拷貝構(gòu)造函數(shù)

2010-01-19 13:43:59

C++函數(shù)

2011-06-17 16:09:04

freadfwrite

2010-02-01 09:18:49

C++函數(shù)指針

2011-07-20 15:58:53

C++引用

2011-07-13 16:49:59

C++

2011-07-20 13:57:06

C++STL

2010-01-18 10:16:58

學(xué)習(xí)C++

2011-07-14 16:56:21

2011-07-14 23:27:05

C++引用

2011-07-14 16:26:01

2011-07-20 16:43:34

C++

2011-06-21 10:37:56

const

2011-07-13 11:12:43

C++MFC

2010-01-11 09:56:07

C++編程實(shí)例

2011-07-20 14:12:48

2010-01-19 13:17:05

C++數(shù)據(jù)類型
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號