• Linux时间相关C库函数


    Linux时间相关C函数


    在这里插入图片描述

    • 由UNIX内核提供的基本时间服务是国际标准时间公元1970年1月1日00:00:00以来经过的秒数。这种秒数是以数据类型 time_t表示的。我们称它们为日历时间,由函数time()内的系统调用获取。

      #include <time.h>
      
      time_t time(time_t *tloc);//时间值作为函数值返回。如果参数非 n u l l,则时间值也存放在由tloc指向的单元内
      
      
      • 1
      • 2
      • 3
      • 4
    • tm结构

      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 */
      };
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
    • time_t和tm结构可通过下面的函数转换

      #include <time.h>
      
      //使用 timep 的值来填充 tm 结构,并用格林尼治标准时间(GMT)表示。
      struct tm *gmtime(const time_t *timep);
      struct tm *gmtime_r(const time_t *timep, struct tm *result);
      
      //使用 timep 的值来填充 tm 结构,并用本地时区表示。
      struct tm *localtime(const time_t *timep);
      struct tm *localtime_r(const time_t *timep, struct tm *result);
      
      //以本地时间的tm结构作为参数,将其变换成 time _ t值
      time_t mktime(struct tm *tm);
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
    • 将time_t或tm表示成字符串,形如 “Wed Jun 30 21:49:08 1993\n

      #include <time.h>
      
      char *asctime(const struct tm *tm);
      char *asctime_r(const struct tm *tm, char *buf);
      
      char *ctime(const time_t *timep);
      char *ctime_r(const time_t *timep, char *buf);
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
    • strftime函数根据 format 中定义的格式化规则,格式化结构 tm 表示的时间,并把它存储在s中。如果产生的字符串小于 size 个字符(包括空结束字符),则会返回复制到 str 中的字符总数(不包括空结束字符),否则返回零。

      #include <time.h>
      
      size_t strftime(char *s, size_t max, const char *format,
                      const struct tm *tm);
      
      • 1
      • 2
      • 3
      • 4
      • format参数控制时间值的格式。如同 printf函数一样,变换说明的形式是百分号之后跟一个特定字符。 format中的其他字符则按原样输出。两个连续的百分号在输出中产生一个百分号。
      格 式说 明例 子
      % a缩写的周日名T u e
      % A全周日名T u e s d a y
      % b缩写的月名J a n
      % B月全名J a n u a r y
      % c日期和时间 Tue Jan 14 19:40:301992
      % d月日: [01, 31]1 4
      % H小时(每天2 4小时): [00, 23]1 9
      % I小时(上、下午各 1 2小时): [01, 12]0 7
      % j年日: [001, 366]0 1 4
      % m月: [01, 12]0 1
      % M分: [00, 59]4 0
      % pA M / P MP M
      % S秒:[00, 61]3 0
      % U星期日周数: [00, 53]0 2
      % w周日: [ 0 =星期日, 6 ]2
      % W星期一周数: [00, 53]0 2
      % x日期0 1 / 1 4 / 9 2
      % X时间1 9 : 4 0 : 3 0
      % y不带公元的年: [00, 991]9 2
      % Y带公元的年1 9 9 2
      % Z时区名M S T
  • 相关阅读:
    Nginx学习笔记01
    Redis安装-Docker
    【uniapp】HBuilderx中uniapp项目运行到微信小程序报错Error: Fail to open IDE
    mysql的常见函数
    万数藏:国内首家影视IP艺术数藏平台今日上线
    阿里巴巴国际站开店,佛山本地品牌如何征服海外高端市场
    【图像增强】基于萤火虫算法实现图像对比度增强附matlab代码
    shell脚本的多线程介绍
    代码随想录day52|子序列系列|300.最长递增子序列|674. 最长连续递增序列|718. 最长重复子数组|Golang
    C语言经典面试题目(十四)
  • 原文地址:https://blog.csdn.net/weixin_49638344/article/details/125474439