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
      %H24 小时格式的小时(00-23)23
      %I12 小时格式的小时(01-12)09
      %j一年中的第几天(001-366)366
      %m十进制数表示的月份(01-12)09
      %M分(00-59)59
      %pAM 或 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;
      }
      ```

转载于:https://www.cnblogs.com/theDesertIslandOutOfTheWorld/p/5207761.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值