详细介绍下C/C++时间相关的那些函数

开发 后端
chrono是一个关于时间的库,起源于boost,现在是C++的标准,话说现在的C++标准好多都是源于boost,要进标准的特性似乎都会先在boost试验一番。

[[344240]]

本文转载自微信公众号「程序喵大人」,作者程序喵大人。转载本文请联系程序喵大人公众号。  

大家平时工作,如果有计算函数耗时或者打印当前时间的需求,一定要来看看这篇文章!

首先介绍下C++标准中的chrono库

chrono是一个关于时间的库,起源于boost,现在是C++的标准,话说现在的C++标准好多都是源于boost,要进标准的特性似乎都会先在boost试验一番。

首先看一下使用chrono简单计时的示例代码:

  1. void func() {  // 计时 
  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中有三个概念duration、time_point、clock

duration:表示一段时间,三分钟、三秒等,它的定义如下:

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

ratio的定义如下:

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

Rep表示数据类型,int,long等,Period表示时间单位,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>>; 

详细看完上述例子您也明白了,ratio的默认的时间单位是1秒,以小时为例,一小时等于3600秒,3600 / 1 == 7200 / 2 == 3600,所以hours == hours2 == hours3。

标准库还提供了duration_cast用于转换各种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>; 

函数看着很繁琐,直接看看示例代码吧:

  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:用来表示某个具体时间点。

定义如下:

  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 

这里有个函数time_since_epoch(),表示这个time_point距离元年也就是1970年1月1日所经过的duration。

time_point也有各种表示方式,类似于duration,也提供了转换函数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:

这里的时钟大体有三种:

  • system_clock
  • steady_clock
  • high_resolution_clock

system_clock表示当前的系统时钟,有三个函数:

  1. now():表示当前时间的time_point 
  2. to_time_t():将time_point转换成time_t秒 
  3. from_time_t():将time_t转换成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表示稳定的时钟,它只有一个函数,就是now(),后一次调用now()肯定比上一次调用now()的返回值大,不受系统时间修改的影响。

源码如下:

  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() {  // 计时 
  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表示高精度时钟,是系统可用的最高精度的时钟,它其实就是system_clock或者steady_clock的别名:

  1. using high_resolution_clock = steady_clock; 

介绍完了C++的chrono那下面再看下C语言的各种时间相关的API吧:

首先可以通过C语言的clock拿到程序执行时处理器所使用的时钟数来计时:

  1. clock_t clock(void); 

该函数返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。也获取 CPU 所使用的秒数,除以 CLOCKS_PER_SEC即可,返回的clock_t其实就是long类型的重命名。

使用方式如下:

  1. void func() { 
  2.     clock_t start_t = clock(); 
  3.     cout << start_t << " 个时钟 \n"
  4.     for (int i = 0; i < 100000000; i++) { 
  5.     } 
  6.     clock_t end_t = clock(); 
  7.     cout << end_t << " 个时钟 \n"
  8.     cout << "循环的秒数:" << (double)(end_t - start_t) / CLOCKS_PER_SEC << endl; 

如何获取当前时间戳,单位为秒

  1. void func() {  // 获取当前时间戳,单位为秒 
  2.     struct timeval time
  3.     gettimeofday(&timeNULL); 
  4.     cout << time.tv_sec << " s \n"

也可以使用time函数:

  1. time_t time(time_t *time); 

该函数返回系统的当前日历时间,返回的是自1970年1月1日以来所经过的秒数。

time_t其实就是一个整数类型,是int64_t的重命名,该函数直接使用返回值就好,参数一般传空即可。

timer 存取结果的时间指针变量,类型为time_t,指针变量可以为null。

如果timer指针非null,则time()函数返回值变量与timer指针一样,都指向同一个内存地址;

否则如果timer指针为null,则time()函数返回一个time_t变量时间。

  1. void func() {  // 获取当前时间戳,单位为秒 
  2.     time_t now = time(NULL); 
  3.     cout << static_cast<int64_t>(now) << " s \n"

如何获取当前时间戳?单位为毫秒

  1. void func() {  // 获取当前时间戳,单位为毫秒 
  2.     struct timeval time
  3.     gettimeofday(&timeNULL); 
  4.     cout << time.tv_sec * 1000 + time.tv_usec / 1000 << " ms \n"

如何显示当前的系统时间呢?可以使用ctime显示当前时间:

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

该函数返回一个表示当地时间的字符串指针,输出内容格式如下:

  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结构自定义显示当前时间的格式:

  1. struct tm * localtime(const time_t * timer); 

将日历时间转换为本地时间,从1970年起始的时间戳转换为1900年起始的时间数据结构

另一个类似的函数是gmtime函数:

  1. struct tm *gmtime(const time_t *time); 

只是该函数返回的是UTC时间,协调世界时(UTC)也被称为格林尼治标准时间(GMT)。

tm结构如下:

  1. struct tm { 
  2.   int tm_sec;   // 秒,正常范围从 0 到 59,但允许至 61 
  3.   int tm_min;   // 分,范围从 0 到 59 
  4.   int tm_hour;  // 小时,范围从 0 到 23 
  5.   int tm_mday;  // 一月中的第几天,范围从 1 到 31 
  6.   int tm_mon;   // 月,范围从 0 到 11 
  7.   int tm_year;  // 自 1900 年起的年数 
  8.   int tm_wday;  // 一周中的第几天,范围从 0 到 6,从星期日算起 
  9.   int tm_yday;  // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起 
  10.   int tm_isdst; // 夏令时 
  11. }; 

tm_sec 在C89的范围是[0-61],在C99更正为[0-60]。通常范围是[0-59],貌似有些系统会出现60秒的跳跃。

tm_mon 是从零开始的,所以一月份为0,十二月份为11。

tm_year是从1900年开始计算,所以显示年份的时候需要加上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结构的时间:

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

和ctime类似,返回的都是一个固定时间格式的字符串,只是传入的参数不同。

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

也可以使用strftime()函数,该函数可用于格式化日期和时间为指定的格式,如果产生的 C 字符串小于 size 个字符(包括空结束字符),则会返回复制到 str 中的字符总数(不包括空结束字符),否则返回零。

  1. size_t strftime( 
  2.     char *str, // 指向目标数组的指针,用来复制产生的C字符串 
  3.     size_t maxsize, // 最多传出字符数量 
  4.     const char *format, // 格式化方式 
  5.     const struct tm *timeptr // tm指针 
  6. ); 

format格式如下:

  1. %a 星期几的缩写 
  2. %A 星期几的全称 
  3. %b 月份的缩写 
  4. %B 月份的全称 
  5. %c 标准的日期的时间串 
  6. %C 年份的前两位数字 
  7. %d 十进制表示的每月的第几天(值从1到31) 
  8. %D 月/天/年 
  9. %e 在两字符域中,十进制表示的每月的第几天 
  10. %F 年-月-日 
  11. %g 年份的后两位数字,使用基于周的年 
  12. %G 年份,使用基于周的年 
  13. %h 简写的月份名 
  14. %H 24小时制的小时(值从0到23) 
  15. %I 12小时制的小时(值从1到12) 
  16. %j 十进制表示的每年的第几天(值从1到366) 
  17. %m 十进制表示的月份(值从1到12) 
  18. %M 十时制表示的分钟数(值从0到59) 
  19. %n 换行符 
  20. %p 本地的AM或PM的等价显示 
  21. %r 12小时的时间 
  22. %R 显示小时和分钟:hh:mm 
  23. %S 十进制的秒数(值从0到61) 
  24. %t 水平制表符 
  25. %T 显示时分秒:hh:mm:ss 
  26. %u 每周的第几天,星期一为第一天 (值从1到7,星期一为1) 
  27. %U 第年的第几周,把星期日作为第一天(值从0到53) 
  28. %V 每年的第几周,使用基于周的年 
  29. %w 十进制表示的星期几(值从0到6,星期天为0) 
  30. %W 每年的第几周,把星期一做为第一天(值从0到53) 
  31. %x 标准的日期串 
  32. %X 标准的时间串 
  33. %y 不带世纪的十进制年份(值从0到99) 
  34. %Y 带世纪部分的十制年份 
  35. %Z 时区名称,如果不能得到时区名称则返回空字符。 
  36. %% 一个%符号 

使用代码如下:

  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

 

责任编辑:武晓燕 来源: 程序喵大人
相关推荐

2011-07-13 11:34:58

CC++时间函数

2011-07-20 17:16:50

C++重载函数

2011-07-20 13:40:09

拷贝构造函数

2010-01-19 13:43:59

C++函数

2010-02-01 09:18:49

C++函数指针

2011-06-17 16:09:04

freadfwrite

2011-07-13 16:49:59

C++

2011-07-20 15:58:53

C++引用

2010-01-18 10:16:58

学习C++

2011-07-20 13:57:06

C++STL

2011-07-14 16:56:21

2011-07-14 23:27:05

C++引用

2011-07-14 16:26:01

2011-06-21 10:37:56

const

2011-07-20 16:43:34

C++

2010-03-26 16:17:24

Python嵌入

2011-07-20 17:04:55

C++虚函数动态联编

2011-06-21 15:00:07

JAVAC++

2011-07-20 13:57:06

C++STL

2011-07-20 14:12:48

点赞
收藏

51CTO技术栈公众号