加入收藏 | 设为首页 | 会员中心 | 我要投稿 开发网_开封站长网 (http://www.0378zz.com/)- 科技、AI行业应用、媒体智能、低代码、办公协同!
当前位置: 首页 > 教程 > 正文

Linux下time相关的概括

发布时间:2021-11-20 14:06:43 所属栏目:教程 来源:互联网
导读:一:相关函数原型 #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)

一:相关函数原型
 
      #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:%dn",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:%sn",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("%sn",asctime(pt));
 
        printf("Ctime 输出:n");
 
        printf("%sn",ctime(&tdata));
 
        printf("mktime将tm结构转换成time_t类型的值n");
 
        if((tmp = mktime(pt)) == -1){
 
                perror("mktime error:");
 
                exit(1);
 
        }
 
        printf("mktime convert to time_t:%ldn",tmp);
 
        printf("strftime的使用:n");
 
        if(strftime(buf,256,format,pt) == -1){
 
                perror("strftime error:");
 
                exit(1);
 
        }
 
        printf("%sn",buf);
 
        printf("strptime的使用:n");
 
        #buf中存放的是指定format格式的时间字符串,根据其fotamt再转换成tm结构体
 
        strptime(buf,format,pt);
 
 
 
 
 
}
 
#获取当前的时间
 
time_t return_current_time ()
 
{
 
        return time((time_t *)0);
 
}
 
 

(编辑:开发网_开封站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读