Linux的时间函数

2023年7月19日,周三下午

我今天基于GitHub搭建了自己的博客网站,欢迎大家来我的个人博客网站阅读我的博客

巨龙之路的GitHub个人博客 (julongzhilu.github.io)


目录


time

函数原型

获取自 1970 年 1月 1 日早晨零点到现在的秒数,也叫做

#include<time.h>
time_t time(time_t *timep);
使用方法

如果time函数的参数timep为NULL,那么time函数会返回自 1970 年 1月 1 日早晨零点到现在的秒数;

#include<time.h>
#include<stdio.h>
int main(){
    time_t tm;
    tm=time(NULL);
    printf("%d",tm);
}

如果time函数的参数不为NULL,那么time函数会把自 1970 年 1月 1 日早晨零点到现在的秒数赋值给timep指向的time_t类型变量

#include<time.h>
#include<stdio.h>
int main(){
    time_t tm;
    time(&tm);
    printf("%d",tm);
}

建议使用time(NULL),因为这样可以减少出错的可能性,并且使用起来更简单


ctime

函数原型

将time_t转换成可打印格式,所谓可打印格式类似于“Wed Jul 19 01:59:16 2023”

#include<time.h>
char *ctime(const time_t *timep);
使用方法

把一个指向 time_t 的指针作为 timep 参数传入函数 ctime(),将返回一个长达 26 字节的字符串,内含标准格式的日期和时间

#include <stdio.h>
#include <time.h>
​
int main() {
    time_t current_time;
    time(&current_time);
    
    char* time_str = ctime(&current_time);
    printf("Current time: %s", time_str);
    
    return 0;
}
疑惑

为什么ctime函数要用一个指针作为参数,这样不是很麻烦吗?

这是因为ctime函数需要修改一个静态缓冲区中的内容,并返回指向该缓冲区的指针。这个缓冲区包含了一个字符串表示的时间。通过将时间的指针传递给ctime函数,函数可以直接在缓冲区中进行修改,然后返回指向缓冲区的指针,以便用户可以使用这个字符串。

也就是说,通过用指针作为参数,ctime函数就不需要拷贝一份time_t,再根据这副本来修改静态缓冲区的内容,而是可以直接通过指针找到time_t来修改静态缓冲区的内容。这样可以节省内存空间和提高运行效率。

需要注意的是,ctime函数在每次调用时都会使用同一个静态缓冲区,因此如果多次调用ctime函数,后续的调用会覆盖前面调用的结果。如果需要保存多个时间字符串,可以使用localtimegmtime函数进行转换,并使用不同的缓冲区来存储结果。


gmtime、 localtime

函数原型
函数 gmtime() localtime() 可将一 time_t 值转换为一个所谓的分解时间( broken-down
time)。分解时间被置于一个经由静态分配的结构中,其地址则作为函数结果返回。
#include<time.h>

struct tm *gmtime(const time_t *timep);

struct tm *localtime(const time_t *timep);
什么是分解时间

所谓分解时间,其实就是把time_t分解成年、月、日、时、分,秒等

 使用方法
#include<time.h>

int main(){
    time_t ti;
    struct tm *gm_tm;
    struct tm *local_tm;

    //把从1970年1月1日00:00到现在的秒数赋值给ti
    ti=time(NULL);

    //用gmtime函数把ti分解成年、月、日、时、分、秒等,并把分解的结果
    //存放在一个静态分配的结构tm中,返回tm的地址给gm_tm
    gm_tm=gmtime(&ti);

    //用localtime函数把ti分解成年、月、日、时、分、秒等并把分解的结果
    //存放在一个静态分配的结构tm中,返回tm的地址给local_tm
    local_tm=localtime(&ti);

    printf("gm_tm:%d-%d-%d %d:%d:%d\n",gm_tm->tm_year+1900,gm_tm->tm_mon+1,gm_tm->tm_mday,gm_tm->tm_hour,gm_tm->tm_min,gm_tm->tm_sec);
    printf("local_tm:%d-%d-%d %d:%d:%d\n",local_tm->tm_year+1900,local_tm->tm_mon+1,local_tm->tm_mday,local_tm->tm_hour,local_tm->tm_min,local_tm->tm_sec);
}


mktime

函数原型

mktime函数将一个分解时间转换成time_t值

#include<time.h>

time_t mktime(struct tm *timeptr);
使用方法
#include<time.h>
#include<stdio.h>

int main(){

    //获取当前时间的time_t值
    time_t current_time;
    current_time=time(NULL);
    printf("1:current_time:%d\n",current_time);

    //把当前时间的time_t值转换成分解时间
    struct tm *current_tm;
    current_tm=gmtime(&current_time);

    //把分解时间再转换成time_t
    current_time=mktime(current_tm);
    printf("2:current_time:%d\n",current_time);
}


asctime

函数原型

asctime函数把分解时间转换成可打印格式,所谓可打印格式类似于“Wed Jul 19 01:59:16 2023”

#include<time.h>

char *asctime(const struct *timeptr);
使用方法
#include<time.h>
#include<stdio.h>

int main(){
    time_t current_time;
    current_time=time(NULL);

    //获取分解时间
    struct tm *current_tm;
    current_tm=gmtime(&current_time);

    //把分解时间转换成可打印格式
    char* printFormat;
    printFormat=asctime(current_tm);
    printf("%s",printFormat);
}


strftime

函数原型

strftime函数把分解时间转换成指定格式的字符串

#include<time.h>

size_t strftime(char *outstr,size_t maxsize,const char *format,const struct tm *timeptr);
strftime()的转换说明符集
  • %a:缩写的星期几名称(Sun、Mon、Tue等)
  • %A:完整的星期几名称(Sunday、Monday、Tuesday等)
  • %b:缩写的月份名称(Jan、Feb、Mar等)
  • %B:完整的月份名称(January、February、March等)
  • %c:默认的日期和时间表示(例如:Thu Aug 23 14:55:02 2001)
  • %C:世纪数(取值为年份的前两位数字)
  • %d:月份中的日期(01-31)
  • %D:日期(mm/dd/yy)
  • %e:月份中的日期,带有前导空格(1-31)
  • %F:日期(yyyy-mm-dd)
  • %H:小时(00-23)
  • %I:小时(01-12)
  • %j:一年中的天数(001-366)
  • %m:月份(01-12)
  • %M:分钟(00-59)
  • %n:换行符
  • %p:AM或PM
  • %r:12小时制的时间(hh:mm:ss AM/PM)
  • %R:24小时制的时间(hh:mm)
  • %S:秒(00-61)
  • %t:制表符
  • %T:24小时制的时间(hh:mm:ss)
  • %u:星期几(1-7,其中1表示星期一)
  • %U:一年中的周数(00-53,星期天作为每周的第一天)
  • %V:一年中的周数(01-53,ISO 8601标准,星期一作为每周的第一天)
  • %w:星期几(0-6,其中0表示星期天)
  • %W:一年中的周数(00-53,星期一作为每周的第一天)
  • %x:默认的日期表示(例如:08/23/01)
  • %X:默认的时间表示(例如:14:55:02)
  • %y:年份的最后两位数字(00-99)
  • %Y:年份的完整表示(例如:2001)
  • %z:时区偏移(+hhmm或-hhmm)
  • %Z:时区名称
使用方法
#include <stdio.h>
#include <time.h>

int main() {
    time_t now;
    time(&now);
    struct tm* timeinfo = localtime(&now);

    char buffer[80];
    strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", timeinfo);
    printf("Formatted date and time: %s\n", buffer);

    return 0;
}


strptime

函数原型

strptime函数按照指定格式把一个时间字符串转换成分解时间

#include<time.h>

char *strptime(const char *str,const char *format,struct tm *timeptr);
使用方法
#include <stdio.h>
#include <time.h>

int main() {
    const char* str = "2022-03-24 10:30:00";
    const char* format = "%Y-%m-%d %H:%M:%S";

    struct tm timeinfo;
    if (strptime(str, format, &timeinfo) != NULL) {
        printf("Year: %d\n", timeinfo.tm_year + 1900);
        printf("Month: %d\n", timeinfo.tm_mon + 1);
        printf("Day: %d\n", timeinfo.tm_mday);
        printf("Hour: %d\n", timeinfo.tm_hour);
        printf("Minute: %d\n", timeinfo.tm_min);
        printf("Second: %d\n", timeinfo.tm_sec);
    } else {
        printf("Failed to parse the string.\n");
    }

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

巨龙之路

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

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

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

打赏作者

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

抵扣说明:

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

余额充值