linux时间函数大集合

#include <time.h>

time_t time(time_t *t);

 

返回值:当前时间到197011号 000秒 的秒数

time_t 的实质是long 型,time_t *t 参数是用来传入参数获取时间的

例如:

#include <time.h>

#include <stdio.h>

int main()

{

time_t now;

time_t time(&now);

printf(“%ld\n”,now);

}

当然time_t *t参数也可以为NULL

例如:

#include <time.h>

#include <stdio.h>

int main()

{

printf(“%ld\n”,time_t time(NULL))

}

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

#include <time.h>

struct tm *localtime(const time_t *clock); 

功能:把从1970-1-1 000秒到现在的秒数转换成struct tm结构体中相应的变量

参数:const time *clock就是1970-1-1 000秒到现在的秒数,所以可以直接用上述time()函数得到的值传过来

返回值:是一个struct tm的结构体指针,指针的内容如下:

/usr/include/time.h可以查看到

struct tm

{

  int tm_sec;                   /* Seconds.     [0-60] (1 leap second) */

  int tm_min;                   /* Minutes.     [0-59] */

  int tm_hour;                  /* Hours.       [0-23] */

  int tm_mday;                  /* Day.         [1-31] */

  int tm_mon;                   /* Month.       [0-11] */

  int tm_year;                  /* Year - 1900.  */

  int tm_wday;                  /* Day of week. [0-6] */

  int tm_yday;                  /* Days in year.[0-365] */

  int tm_isdst;                 /* DST.         [-1/0/1]*/

 

};

当使用localtime函数的时候,传入的秒数就会相应的转换成结构体中的 秒,分,时,天,月等变量,例如:笔者现在的时间是2014-7-29 83320秒  那么tm结构体中的变量就相应的为20Second33Minutes8hours29days7month(2014-1900) years 因为参数中标明了需要减去1900

应当注意的是虽然秒数是以0时区(格林尼治时间)为基准获得的,但是localtime会自动的进行转换,所以结构体中的变量获得的值就是当地的时间。

 

例程:


#include "time.h"

#include "stdio.h"

int main()

{

        struct tm *now_time;

        time_t seconds_time;

        time(&seconds_time);

        now_time = localtime(&seconds_time);

        printf("%04dyear  %02dmon  %02dday  %02d:  %02d:  %02d\n",now_time->tm_year+1900,now_time->tm_mon+1,now_time->tm_mday,now_time->tm_hour,now_time->tm_min,now_time->tm_sec);

        return 0;

}

 

运行结果:2014year  07mon  28day  18 : 26 : 53  

 

 

需要注意的是因为参数中年份减去了1900 所以这里要加上1900 因为月份没有0,所以要在月份上加1  这样打印出来的结果即是你电脑上的时间

 ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- 

 

使用localtime获得当地的时间是很方便的一件事,但是!localtime却有一个致命的缺点,看程序如下:

#include "time.h"

#include "stdio.h"

int main()

{

        struct tm *now_time_1;

        struct tm *now_time_2;

 

        time_t seconds_time_1 = time(NULL);

    time_t seconds_time_2 =seconds_time_1+1800;   

        now_time_1 = localtime(&seconds_time_1);

now_time_2 = localtime(&seconds_time_2);

        printf("%04dyear  %02dmon  %02dday  %02d:  %02d:  %02d\n",now_time_1->tm_year+1900,now_time_1->tm_mon+1,now_time_1->tm_mday,now_time_1->tm_hour,now_time_1->tm_min,now_time_1->tm_sec);

     printf("%04dyear  %02dmon  %02dday  %02d:  %02d:  %02d\n",now_time_2->tm_year+1900,now_time_2->tm_mon+1,now_time_2->tm_mday,now_time_2->tm_hour,now_time_2->tm_min,now_time_2->tm_sec);

        return 0;

}

运行的结果如下:

2014year  07mon  29day  10:  55:  11

2014year  07mon  29day  10:  55:  11

明明seconds_time_2seconds_time_1多了1800秒 为什么输出结果却是一样的呢?

查看localtime文档,可以发现下边这句话

This structure is statically allocated and shared by the functions gmtime and localtime. Each time either one of these functions is called the content of this structure is overwritten.

就是说localtime只能调用这一次如果在调用就会被重写,被重写什么意思?就是说你本来上边这个程序第一次调用localtime获得的now_time_1结构体里面是一个当时的时间值,结果你又调用了一次localtime,而这次的localtime里面的参数是多了1800秒的,那么得到的now_time_2结构体里面的变量肯定跟now_time_1里面的变量不同,这时候now_time_2结构体就把1给覆盖掉了(因为两个localtime是指向同一个内存区域的)!所以最终输出的结果是一样的!

 

那么linux就提供了一种更为安全的函数:localtime_r()

 

#include <time.h>

struct tm *localtime_r(const time_t *timep, struct tm *result);

 

参数1是传入的秒数,参数2就是我们想要存的时间变量的结构体了,而不用关心函数的返回值,当然使用返回值也是一样的,因为返回值结构体跟传入的结构体得到的结果是一致的,在使用的时候我们用 sturct tm *result 这个结构体就OK了。

 

例程:

#include "time.h"

#include "stdio.h"

int main()

{

        struct tm now_time_1 ;

        struct tm now_time_2 ;

 

        time_t seconds_time_1 = time(NULL);

        time_t seconds_time_2 =seconds_time_1+1800;

 

        localtime_r(&seconds_time_1,&now_time_1);

        localtime_r(&seconds_time_2,&now_time_2);

 

        printf("%04dyear  %02dmon  %02dday  %02d:  %02d:  %02d\n",now_time_1.tm_year+1900,now_time_1.tm_mon+1,now_time_1.tm_mday,now_time_1.tm_hour,now_time_1.tm_min,now_time_1.tm_sec);

     printf("%04dyear  %02dmon  %02dday  %02d:  %02d:  %02d\n",now_time_2.tm_year+1900,now_time_2.tm_mon+1,now_time_2.tm_mday,now_time_2.tm_hour,now_time_2.tm_min,now_time_2.tm_sec);

 

 

      return 0;

}

运行结果:

2014year  07mon  29day  11:  03:  29

2014year  07mon  29day  11:  33:  29

 

-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------  

 

上例中调用localtime之后需要自己取struct tm 结构体中的变量进行输出,这样还是相当的麻烦的,麻烦从来不是编程应该有的,所以linux提供了更为简单的方法,即:asctime()

 

#include <time.h>

char *asctime(const struct *tm)

功能:将struct tm结构体中的时间信息转换成整理好的时间字符串输出

#include "time.h"

#include "stdio.h"

int main()

{

        struct tm *now_time;

        time_t seconds_time;

        time(&seconds_time);

        now_time = localtime(&seconds_time);

    //   printf("%04dyear  %02dmon  %02dday  %02d:  %02d:  %02d\n",now_time->tm_year+1900,now_time->tm_mon+1,now_time->tm_mday,now_time->tm_hour,now_time->tm_min,now_time->tm_sec);

        printf(%s\n,asctime(now_time));

return 0;

}

运行结果:  Mon Jul 28 18:30:04 2014

 

 

#include<sys/time.h>

int gettimeofday(struct  timeval*tv,struct  timezone *tz )

首先了解下参数中的两个结构体:

struct  timeval

{

  long  tv_sec;/*秒*/

       long  tv_usec;/*微秒*/

};

 

struct  timezone

{

  int tz_minuteswest;/*和greenwich 时间差了多少分钟*/

      int tz_dsttime;/*type of DST correction*/

}

tv 结构体存放的是格林尼治时间时间到现在的秒数和微妙数,这里需要注意的是微妙数是在秒数基础上剩下的那些微秒,例如格林尼治到现在的秒数是140000000秒,另外多了500微秒,那么tv_sec就是140000000秒  tv_usec就是500微秒 当微秒超过1000的时候,那么tv_sec就是140000001秒,tv_usec就是0微秒了

 

当地时区的信息则放到tz所指的结构中,在用的时候我们通常不用这个结构体,即:第二个参数用NULL代替

功能:将从1970-1-1 0:0:0到现在的秒数和微秒数存到timeval结构体中

返回值:函数执行成功后返回0,失败后返回-1,错误代码存于errno中。

所以上述的例子也可以这样实现:

#include "time.h"

#include "stdio.h"

#include "sys/time.h"

int main()

{

        struct tm  *now_time;

        struct timeval t;

        gettimeofday(&t,NULL);

        printf("sec:%ld usec:%ld\n",t.tv_sec,t.tv_usec);

        now_time = localtime(&t.tv_sec);

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

 

        return 0;

}

运行结果:

sec:1406599298 usec:23178

Tue Jul 29 10:01:38 2014

 

 

 

 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值