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指向的单元内
    
    
  • 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 */
    };
    
  • 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);
    
  • 将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);
    
  • strftime函数根据 format 中定义的格式化规则,格式化结构 tm 表示的时间,并把它存储在s中。如果产生的字符串小于 size 个字符(包括空结束字符),则会返回复制到 str 中的字符总数(不包括空结束字符),否则返回零。

    #include <time.h>
    
    size_t strftime(char *s, size_t max, const char *format,
                    const struct tm *tm);
    
    • 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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

money的大雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值