C语言-12-日期和时间处理标准库详细解析及示例

概述

  • 标准库 <time.h> 提供了用于日期和时间处理的结构和函数
  • 是C++语言日期和时间处理的基础

与时间相关的类型

  • clock_t,本质是:unsigned long

    typedef unsigned long __darwin_clock_t;
    typedef __darwin_clock_t clock_t;
    
  • time_t,本质是:long

    typedef long __darwin_time_t;
    typedef __darwin_time_t	 time_t;
    
  • size_t,本质是:unsigned long,其中包含条件编译指令

    // size_t
    typedef __darwin_size_t        size_t;
    // _darwin_size_t
    #if defined(__SIZE_TYPE__)
    typedef __SIZE_TYPE__		__darwin_size_t;
    #else
    typedef unsigned long		__darwin_size_t;
    #endif
    
  • tm 结构

    struct tm {
    	int	tm_sec;		/* seconds after the minute [0-60] */
    	int	tm_min;		/* minutes after the hour [0-59] */
    	int	tm_hour;	/* hours since midnight [0-23] */
    	int	tm_mday;	/* day of the month [1-31] */
    	int	tm_mon;		/* months since January [0-11] */
    	int	tm_year;	/* years since 1900 */
    	int	tm_wday;	/* days since Sunday [0-6] */
    	int	tm_yday;	/* days since January 1 [0-365] */
    	int	tm_isdst;	/* Daylight Savings Time flag */
    	long	tm_gmtoff;	/* offset from CUT in seconds */
    	char	*tm_zone;	/* timezone abbreviation */
    };
    

标准库函数

  • time_t time(time_t *time)

    • 返回系统当前日历时间,自1970年1月1日以来经过的秒数;如果系统没有时间,则返回.1,如:1456131213
    time_t seconds;
    seconds = time(0);
    
  • char *ctime(const time_t *time)

    • 返回一个表示当地时间的字符串指针,格式为:Wed Feb 17 15:23:46 2016(与系统有关)
    char *date = ctime(&seconds);
    
  • struct tm *localtime(const time_t *time)

    • 返回指向表示本地时间的tm结构的指针
    tm *localTime = localtime(&seconds);
    
  • clock_t clock(void)

    • 该函数返回程序执行起(一般为程序的开头),处理器时钟所使用的时间,如:1456102413
    clock_t clockTime = clock();
    
  • char *asctime(const struct tm *time)

    • 该函数返回一个指向字符串的指针,字符串包含了 localTime 所指向结构中存储的信息,格式为:Wed Feb 17 15:48:45 2016
    date = asctime(localTime);
    
  • struct tm *gmtime(const time_t *time)

    • 该函数返回一个指向 localTime 的指针,localTime 为 tm 结构,用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示
    localTime = gmtime(&seconds);
    date = asctime(localTime);
    
  • time_t mktime(struct tm *time)

    • 该函数返回日历时间,相当于 time 所指向结构中存储的时间
    seconds = mktime(localTime);
    
  • double difftime(time_t time2, time_t time1)

    • 该函数返回 time1 和 time2 之间相差的秒数
    double difference = difftime(seconds, seconds1);
    
  • size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)
    `

    • 可用于格式化日期和时间为指定的格式

    • 格式说明符

      格式说明符 含义 示例
      %a 缩写的星期几名称 Sun
      %A 完整的星期几名称 Sunday
      %b 缩写的月份名称 march
      %B 日期和时间表示法 Wed Feb 17 08:07:12 2016
      %d 一月中的第几天(01-31) 28
      %H 24 小时格式的小时(00-23) 23
      %I 12 小时格式的小时(01-12) 09
      %j 一年中的第几天(001-366) 366
      %m 十进制数表示的月份(01-12) 09
      %M 分(00-59) 59
      %p AM 或 PM 名称 PM
      %S 秒(00-59) 09
      %U 一年中的第几周,以第一个星期日作为第一周的第一天(00-53) 53
      %w 十进制数表示的星期几,星期日表示为 0(0-6) 6
      %W 一年中的第几周,以第一个星期一作为第一周的第一天(00-53) 1
      %x 日期表示法 02/17/16
      %X 时间表示法 08:14:44
      % 年份,最后两个数字(00-99) 99
      %Y 年份 2016
      %Z 时区的名称或缩写 CST
      %% 一个 % 符号 %
    • 示例

      strftime(date, 99, "%Y年%m月%d日 %A %p %I:%M:%S", localTime);
      // 打印结果:2016年02月22日 Monday AM 08:53:33
      

示例

  • 完整代码

      ```
      #include <stdio.h>
      #include <time.h>
      int main(int argc, const char * argv[]) {
          //
          time_t seconds;
          seconds = time(0);
          printf("%ld\n", seconds);
          //
          char *date = ctime(&seconds);
          printf("%s\n", date);
          //
          struct tm *localTime = localtime(&seconds);
          printf("%d\n", localTime->tm_sec);
          //
          clock_t clockTime = clock();
          printf("%lu\n", clockTime);
          //
          date = asctime(localTime);
          printf("%s\n", date);
          //
          localTime = gmtime(&seconds);
          date = asctime(localTime);
          printf("%s\n", date);
          //
          time_t seconds1 = mktime(localTime);
          printf("%ld\n", seconds1);
          //
          double difference = difftime(seconds, seconds1);
          printf("%f\n", difference);
          //
          strftime(date, 99, "%Y年%m月%d日 %A %p %I:%M:%S", localTime);
          printf("%s\n", date);
          return 0;
      }
      ```
    
posted @ 2016-02-22 17:37  世俗孤岛  阅读(1109)  评论(0编辑  收藏  举报