zoukankan      html  css  js  c++  java
  • 【UNIX环境高级编程】 时间函数

    几个时间概念

    1:Coordinated Universal Time(UTC):

    协调世界时,又称世界标准时间,也即格林威治标准时间(Greenwich Mean Time,GMT),中国内地的时间与UTC得时差为+8,也即UTC+8,美国为UTC-5。

    2:Calendar Time:

    日历时间,是用"从一个标准时间点到此时的时间经过的秒数"来表示的时间。标准时间点对不同编译器可能会不同,但对一个编译系统来说,标准时间是不变的。一般是表示距离UTC时间 1970-01-01 00:00:00的秒数。

    3:epoch:

    时间点。在标准c/c++中是一个整数,用此时的时间和标准时间点相差的秒数(即日历时间)来表示。

    4:clock tick:

    时钟计时单元(而不叫做时钟滴答次数),一个时钟计时单元的时间长短是由cpu控制的,一个clock tick不是cpu的一个时钟周期,而是c/c++的一个基本计时单位。

    time.h 的定义

    time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数。

    4个变量

    size_t是无符号整数类型,它是 sizeof 关键字的结果。
    clock_t 这是一个适合存储处理器时间的类型,类型为unsigned long
    time_t 这是一个适合存储日历时间类型。
    struct tm 这是一个用来保存时间和日期的结构。

     tm 结构的定义如下:

    struct tm
    {
      int tm_sec;          /* 秒,范围从 0 到 59      */
      int tm_min;          /* 分,范围从 0 到 59      */
      int tm_hour;         /* 小时,范围从 0 到 23     */
      int tm_mday;         /* 一月中的第几天,范围从 1 到 31    */
      int tm_mon;          /* 月,范围从 0 到 11(注意)  */
      int tm_year;         /* 自 1900 年起的年数      */
      int tm_wday;         /* 一周中的第几天,范围从 0 到 6 */
      int tm_yday;         /* 一年中的第几天,范围从 0 到 365   */
      int tm_isdst;        /* 夏令时               */
      long int tm_gmtoff;  /*指定了日期变更线东面时区中UTC东部时区正秒数或UTC西部时区的负秒数*/
      const char *tm_zone; /*当前时区的名字(与环境变量TZ有关)*/
    };

    一个宏:

    NULL这个宏是一个空指针常量的值。
    CLOCKS_PER_SEC 这个宏表示每秒的处理器时钟个数。用于将clock()函数的结果转化为以秒为单位的量,这个量的具体值是与操作系统相关的,通常为1000。

    库函数

    1:clock函数

    函数原型: clock_t clock(void)

    函数返回:返回clock函数执行起(一般为程序的开头),处理器时钟所使用的时间。

    函数功能:用来计算程序或程序的某一段的执行时间。

     1 #include <stdio.h>
     2 #include <time.h>
     3 
     4 int main() {
     5     long long sum = 0;
     6     clock_t start_t, finish_t;
     7     double total_t = 0;
     8     int i = 0;
     9     start_t = clock();
    10     for (; i <= 100000; ++i) {
    11         sum += i;
    12     }
    13     finish_t = clock();
    14     total_t = (double)(finish_t - start_t) / CLOCKS_PER_SEC;//将时间转换为秒
    15 
    16     printf("CPU 占用的总时间:%f
    ", total_t);
    17     return 0;
    18 }

    输出:

    CPU 占用的总时间:0.000337

    time函数

    函数原型:

    time_t time(time_t *timer)

    参数说明: timer=NULL时得到当前日历时间(从1970-01-01 00:00:00到现在的秒数),time_t是一个unsigned long类型。如果 timer不为空,则返回值也存储在变量 timer中。

    函数功能: 得到当前日历时间或者设置日历时间

    函数返回: 当前日历时间

     1 #include <stdio.h>
     2 #include <time.h>
     3 
     4 int main()
     5 {
     6   time_t seconds;
     7 
     8   seconds = time(NULL);
     9   printf("自 1970-01-01 起的小时数 = %ld
    ", seconds / 3600);
    10 
    11   return 0;
    12 }

    gtime函数

    函数原型:

    struct tm *gmtime(const time_t *timer)

    函数功能:C 库函数 struct tm *gmtime(const time_t *timer) 使用 timer 的值来填充 tm 结构,并用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。

    日历时间转换成协调统一时间的年、月、日、时、分、秒等分解结构。

    返回值:该函数返回指向 tm 结构的指针,该结构带有被填充的时间信息。

    1. 示例

     1 #include <stdio.h>
     2 #include <time.h>
     3 
     4 #define BST (+1)
     5 #define CCT (+8)
     6 
     7 int main()
     8 {
     9 
    10   time_t rawtime;
    11   struct tm *info;
    12 
    13   time(&rawtime);
    14   /* 获取 GMT 时间 */
    15   info = gmtime(&rawtime);
    16 
    17   printf("当前的世界时钟:
    ");
    18   printf("伦敦:%2d:%02d
    ", (info->tm_hour + BST) % 24, info->tm_min);
    19   printf("中国:%2d:%02d
    ", (info->tm_hour + CCT) % 24, info->tm_min);
    20 
    21   return 0;
    22 }

    输出:

    当前的世界时钟:
    伦敦:14:28
    中国:21:28

    asctime函数

    函数原型:

    char* asctime(struct tm * ptr)

    函数功能:将结构struct tm * ptr所表示的时间以字符串表示

    函数返回: 返回的时间字符串格式为:星期,月,日,小时:分:秒,年。

    1.示例

     1 #include <stdio.h>
     2 #include <time.h>
     3 
     4 int main()
     5 {
     6   struct tm t; //更多情况下是通过localtime函数及gmtime函数获得tm结构
     7 
     8   t.tm_sec = 10;
     9   t.tm_min = 10;
    10   t.tm_hour = 6;
    11   t.tm_mday = 25;
    12   t.tm_mon = 2;
    13   t.tm_year = 89;
    14   t.tm_wday = 6;
    15 
    16   printf("%s
    ", asctime(&t));
    17 
    18   return (0);
    19 }

    输出结果:

    Sat Mar 25 06:10:10 1989

    localtime函数

    函数原型:

    struct tm *localtime(const time_t *timer)

    函数功能: localtime将日历时间转换成本地时间(考虑到本地时区和夏令时标志)。

    函数返回: 以tm结构表达的时间。

    1. 示例

     1 #include <stdio.h>
     2 #include <time.h>
     3 
     4 int main()
     5 {
     6   time_t timer;
     7   struct tm *now;
     8 
     9   time(&timer);
    10   now = localtime(&timer);
    11   printf("当前的本地时间和日期:%s", asctime(now));
    12   return 0;
    13 }

    输出:

    当前的本地时间和日期:Tue Oct 19 21:31:53 2021

    mktime函数

    函数原型:

    time_t mktime(struct tm *timeptr) 

    函数功能:mktime()用来将本地时间参数timeptr 所指的tm 结构数据转换成从公元1970 年1 月1 日0 时0 分0 秒算起至今的UTC 时间所经过的秒数。

    返回值:返回经过的秒数。

    这是指向表示日历时间的 time_t 值的指针,该日历时间被分解为以下各部分。下面是 timeptr 结构的细节:

     1. 示例

     1 #include <time.h>
     2 #include <stdio.h>
     3 
     4 int main()
     5 {
     6   time_t timep;
     7   struct tm *p;
     8   time(&timep);
     9   printf("time() : %d
    ", timep);
    10   p = localtime(&timep);
    11   timep = mktime(p);
    12   printf("time()->localtime()->mktime():%d
    ", timep);
    13 }

    输出:

    time() : 1634649761 
    time()->localtime()->mktime():1634649761

    2. 示例

     1 #include <stdio.h>
     2 #include <time.h>
     3 #include <iostream>
     4 using namespace std;
     5 
     6 void dump(std::string msg, struct tm* p)
     7 {
     8   std::cout << msg << std::endl;
     9   std::cout << "tm_sec: " << p->tm_sec << std::endl;
    10   std::cout << "tm_min: " << p->tm_min << std::endl;
    11   std::cout << "tm_hour: " << p->tm_hour << std::endl;
    12   std::cout << "tm_mday: " << p->tm_mday << std::endl;
    13   std::cout << "tm_mon: " << p->tm_mon << std::endl;
    14   std::cout << "tm_year: " << p->tm_yday << std::endl;
    15   std::cout << "tm_wday: " << p->tm_wday << std::endl;
    16   std::cout << "tm_yday: " << p->tm_yday << std::endl;
    17   std::cout << "tm_isdst: " << p->tm_isdst << std::endl;
    18   std::cout << "tm_zone: " << p->tm_zone << std::endl;
    19 }
    20 
    21 int main()
    22 {
    23   time_t timep;
    24   time(&timep);
    25 
    26   dump("gmtime:", gmtime(&timep));
    27   dump("localtime:", localtime(&timep));
    28 
    29   return 0;
    30 }

    输出:

    gmtime:
    tm_sec: 22
    tm_min: 38
    tm_hour: 14
    tm_mday: 19
    tm_mon: 9
    tm_year: 291
    tm_wday: 2
    tm_yday: 291
    tm_isdst: 0
    tm_zone: GMT
    localtime:
    tm_sec: 22
    tm_min: 38
    tm_hour: 22
    tm_mday: 19
    tm_mon: 9
    tm_year: 291
    tm_wday: 2
    tm_yday: 291
    tm_isdst: 0
    tm_zone: CST
     

     strftime函数与strftime_l函数

    说明符替换为实例
    %a 缩写的星期几名称 Sun
    %A 完整的星期几名称 Sunday
    %b 缩写的月份名称 Mar
    %B 完整的月份名称 March
    %c 日期和时间表示法 Sun Aug 19 02:56:02 2012
    %d 一月中的第几天(01-31) 19
    %H 24 小时格式的小时(00-23) 14
    %I 12 小时格式的小时(01-12) 05
    %j 一年中的第几天(001-366) 231
    %m 十进制数表示的月份(01-12) 08
    %M 分(00-59) 55
    %p AM 或 PM 名称 PM
    %S 秒(00-61) 02
    %U 一年中的第几周,以第一个星期日作为第一周的第一天(00-53) 33
    %w 十进制数表示的星期几,星期日表示为 0(0-6) 4
    %W 一年中的第几周,以第一个星期一作为第一周的第一天(00-53) 34
    %x 日期表示法 08/19/12
    %X 时间表示法 02:50:06
    %y 年份,最后两个数字(00-99) 01
    %Y 年份 2012
    %Z 时区的名称或缩写 CDT
    %% 一个 % 符号 %

    1. 示例

     1 #include <stdio.h>
     2 #include <time.h>
     3 
     4 int main()
     5 {
     6   time_t rawtime;
     7   struct tm *info;
     8   char buffer[80];
     9 
    10   time(&rawtime);
    11 
    12   info = localtime(&rawtime);
    13 
    14   strftime(buffer, 80, "%Y-%m-%d %H:%M:%S", info);
    15   printf("格式化的日期 & 时间 : |%s|
    ", buffer); //本地时间
    16 
    17   return 0;
    18 }

    输出:

    格式化的日期 & 时间 : |2021-10-19 21:37:12|

    strptime函数

    函数原型:

     char *strptime(const char *s, const char  *format, struct tm  *tm);

    函数功能:trptime() 函数解析由 strftime() 生成的时间/日期。

    参数:s:字符串指针,用来接收tm结构体中的时间;format:格式化字符串;tm:时间结构体

    函数返回值:指向上次解析的字符的下一个字符的指针;否者,返回NULL

    1. 示例

     1 #include <stdio.h>
     2 #include <time.h>
     3 
     4 int main()
     5 {
     6   struct tm tm;
     7   char buf[255];
     8 
     9   strptime("2001-11-12 18:31:01", "%Y-%m-%d %H:%M:%S", &tm);
    10   strftime(buf, sizeof(buf), "%d %b %Y %H:%M", &tm);
    11 
    12   puts(buf);
    13   return 0;
    14 }

    输出:

    12 Nov 2001 18:31

     

    【注意】localtime、mktime、和strftime函数受环境变量TZ的影响,如果定义了TZ, 则这些函数使用其值代替系统默认的时间,如果TZ定义为空串(即TZ=), 则使用世界协调统一时间UTC。

    参考资料

    1. C 语言中的 time 函数总结

  • 相关阅读:
    mysql 数据迁移方案
    tomcat:run 指定端口号
    idea open gradle project
    tomcat:run命令执行端口号
    域名代理
    stackoverflow慢问题
    chrome 添加 vue tools
    深度学习——结构化机器学习项目(学习策略2)[9]
    深度学习——超参数调试[7]
    深度学习——优化算法[6]
  • 原文地址:https://www.cnblogs.com/sunbines/p/15395633.html
Copyright © 2011-2022 走看看