Linux下time相关的API总结


一:相关函数原型

      #include <time.h>

      time_t time(time_t *t);

      char *asctime(const struct tm *tm);

      char *ctime(const time_t *timep);

      struct tm *gmtime(const time_t *timep);

      struct tm *localtime(const time_t *timep);

      time_t mktime(struct tm *tm);

      char *strptime(const char *s, const char *format, struct tm *tm);

      size_t strftime(char *s, size_t max, const char *format,

                                                const struct tm *tm);

          struct tm {

              int tm_sec;        /* seconds */

              int tm_min;        /* minutes */

              int tm_hour;        /* hours */

              int tm_mday;        /* day of the month */

              int tm_mon;        /* month */

              int tm_year;        /* year */

              int tm_wday;        /* day of the week */

              int tm_yday;        /* day in the year */

              int tm_isdst;      /* daylight saving time */

          };

二:API归类

time()用于当前时间戳,如果t不为空那么返回的时间戳同样也会存储在t中。
asctime()传入一个tm的时间结构体,返回这个可读性好的时间字符串
ctime()功能和asctime相同,但是ctime功能更强,可以将time_t类型的时间戳转换
成可读性好的时间字符串。
gmtime()传入一个time_t 类型的时间值,将其转换成一个时间tm结构体,但是gmtime 有一个缺点返回的时候格式是UTC时间,不是当前主机时区的时间。
localtime()和gmtime功能相同,只不过localtime返回的是当前主机时区的时间。
mktime()传入一个tm结构体将其转换成time_t类型的时间戳。
strftime()传入一个tm结构体和格式化字符串,根据格式化字符串将其格式会相对应的时间格式。
strptime()传入一个时间字符串,指定其格式,将其转换成tm的时间结构体
asctime() ctime()可分为一类,针对不同的参数使用不同的函数。
gmtime() localtime()分为一类,根据是否对时区又要求来使用。
strftime() strptime()分为一类,处理格式化时间字符串。
time() 用于获取当前时间

三:API函数相关使用


#include<stdio.h>

#include<stdlib.h>

#define _XOPEN_SOURCE #这句没有的话,编译会有警告信息,GUN库中没有strptime声明

#include<time.h>

 

time_t return_current_time();

void print_time(time_t tdata);

 

int main ( int argc, char *argv[] )

{

 

        print_time(return_current_time());

        return EXIT_SUCCESS;

}

void print_time(time_t tdata)

{

        char *wday[]={"SUn","Mon","Tue","Wed","Thu","Fri","Sat"};

        struct tm *pt;

        time_t tmp;

        char buf[256];

        //指定输出字符串的格式

        char *format = "%A %d %B, %I:%S %p";

//      pt = gmtime(&tdata);

        pt = localtime(&tdata);

        printf("%ld:convert to time:\n",tdata);

        printf("isdst:%d\n",pt->tm_isdst);

        printf("year:%d年\n",pt->tm_year+1900);

        printf("mon:%d月\n",pt->tm_mon+1);

        printf("mday:%d日\n",pt->tm_mday);

        printf("week:%s\n",wday[pt->tm_wday]);

        printf("hour:%d小时\n",pt->tm_hour);

        printf("min:%d分钟\n",pt->tm_min);

        printf("sec:%d秒\n",pt->tm_sec);

        printf("asctime的输出\n");

        printf("%s\n",asctime(pt));

        printf("Ctime 输出:\n");

        printf("%s\n",ctime(&tdata));

        printf("mktime将tm结构转换成time_t类型的值\n");

        if((tmp = mktime(pt)) == -1){

                perror("mktime error:");

                exit(1);

        }

        printf("mktime convert to time_t:%ld\n",tmp);

        printf("strftime的使用:\n");

        if(strftime(buf,256,format,pt) == -1){

                perror("strftime error:");

                exit(1);

        }

        printf("%s\n",buf);

        printf("strptime的使用:\n");

        #buf中存放的是指定format格式的时间字符串,根据其fotamt再转换成tm结构体

        strptime(buf,format,pt);

 

 

}

#获取当前的时间

time_t return_current_time ()

{

        return time((time_t *)0);

}

 

本文永久更新链接地址:

相关内容