Linux time()函数解析

来源:互联网 发布:mac照片 导入iphone 编辑:程序博客网 时间:2024/05/16 16:10

time() 函数语法如下:

所需头文件#include <time.h>函数原型time_t time(time_t * timer)参数说明timer=NULL时得到机器日历时间;
timer=时间数值时,用于设置日历时间;函数返回值机器日历时间

      功能: 获取当前的系统时间,返回的结果是一个time_t类型,其实就是一个大整数,其值表示从CUT(Coordinated Universal Time)时间1970年1月1日00:00:00(称为UNIX系统的Epoch时间)到当前时刻的秒数。然后调用localtime将time_t所表示的CUT时间转换为本地时间(我们是+8区,比CUT多8个小时)并转成struct tm类型。

     struct tm 类型的各数据成员分别表示年月日时分秒。

[cpp] view plain copy
  1. struct  tm  
  2. {  
  3.        int tm_sec;  
  4.        int tm_min;  
  5.        int tm_hour;  
  6.        int tm_mday;  
  7.        int tm_mon;  
  8.        int tm_year;  
  9.        int tm_wday;  
  10.        int tm_yday;  
  11.        int tm_isdst;  
  12. };   

补充说明:time函数的原型也可以理解为 long time(long *tloc),即返回一个long型整数。因为在time.h这个头文件中time_t 实际上就是:

[cpp] view plain copy
  1. #ifndef _TIME_T_DEFINED  
  2. #define _TIME_T_DEFINED /* avoid multiple defines of time_t */  
  3.     
  4.     typedef long time_t/* time value */  
  5.   
  6. #endif  


函数应用举例

程序例1

         time函数获得日历时间。日历时间,是用“从一个标准时间点到此时的时间经过的秒数”来表示的时间。这个标准时间点对不同的编译器来说会有所不同,但对一个编译系统来说,这个标准时间点是不变的,该编译系统中的时间对应的日历时间都通过该标准时间点来衡量,所以可以说日历时间是“相对时间”,但是无论你在哪一个时区,在同一时刻对同一个标准时间点来说,日历时间都是一样的。

[cpp] view plain copy
  1. #include <time.h>  
  2. #include <stdio.h>  
  3.   
  4. int main(void)  
  5. {  
  6.     time_t t;   
  7. <span style="white-space:pre">    </span>t = time(NULL);  
  8.   
  9.     printf("The number of seconds since January 1, 1970 is %ld",t);  
  10.   
  11.     return 0;  
  12. }  
执行结果如下:

[cpp] view plain copy
  1. fs@ubuntu:~/qiang/time$ ./time1   
  2. The number of seconds since January 1, 1970 is 1452345470  
  3. fs@ubuntu:~/qiang/time$   

程序例2:

         time函数也常用于随机数的生成,用日历时间作为种子。

[cpp] view plain copy
  1. #include <stdio.h>  
  2. #include <time.h>  
  3. #include<stdlib.h>  
  4.   
  5. int main(void)  
  6. {  
  7.     int i;  
  8.     srand((unsigned) time(NULL));  
  9.     printf("ten random numbers from 0 to 99:\n");  
  10.   
  11.     for(i = 0;i < 10;i++)  
  12.     {  
  13.         printf("%d\n",rand()%100);  
  14.     }  
  15.   
  16.     return 0;  
  17. }  

执行结果如下:

[cpp] view plain copy
  1. fs@ubuntu:~/qiang/time$ ./time2  
  2. ten random numbers from 0 to 99:  
  3. 22  
  4. 0  
  5. 58  
  6. 7  
  7. 29  
  8. 90  
  9. 74  
  10. 7  
  11. 95  
  12. 55  
  13. fs@ubuntu:~/qiang/time$   

程序例3:

        用time() 函数结合其他函数(如:localtime、gmtime、asctime、ctime)可以获得当前系统时间或是标准时间

1)localtime() 函数

        函数功能:返回一个以tm结构表达的机器时间信息;

所需头文件#include <time.h>函数原型struct tm *localtime(const time_t *timep)参数说明timerp为time(NULL)获得的日历时间;函数返回值 以tm结构表达的时间;

使用示例:

[cpp] view plain copy
  1. #include <time.h>   
  2. #include <stdio.h>   
  3.   
  4. int main()  
  5. {  
  6.     time_t timer;  
  7.     struct tm *tblock;  
  8.     timer = time(NULL);  
  9.     tblock = localtime(&timer);  
  10.   
  11.     printf("Local time is: %s",asctime(tblock));  
  12.       
  13.     return 0;   
  14. }   

执行结果如下:

[cpp] view plain copy
  1. fs@ubuntu:~/qiang/time$ ./localtime   
  2. Local time is: Sat Jan  9 21:37:13 2016  
  3. fs@ubuntu:~/qiang/time$   


2)asctime() 函数

       函数功能:asctime()将参数timeptr所指的tm结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为: "Wed Jun 30 21:49:08 1993/n" 

所需头文件#include <time.h>函数原型 char *asctime(struct tm  *ptr)
参数说明ptr 为 struct tm 类型的时间结构体函数返回值返回的时间字符串格式为:星期,月,日,小时:分:秒,年
示例如下:

[cpp] view plain copy
  1. #include <stdio.h>   
  2. #include <string.h>   
  3. #include <time.h>  
  4.   
  5. int main()   
  6. {  
  7.     struct tm t;  
  8.     char str[80];  
  9.     t.tm_sec = 1;  
  10.     t.tm_min = 3;  
  11.     t.tm_hour = 7;  
  12.     t.tm_mday = 22;  
  13.     t.tm_mon = 11;  
  14.     t.tm_year = 56;  
  15.     t.tm_wday = 4;  
  16.     t.tm_yday = 0;  
  17.     t.tm_isdst = 0;  
  18.     strcpy(str,asctime(&t));  
  19.       
  20.     printf("%s",str);  
  21.   
  22.     return 0;   
  23. }   

执行结果如下:

[cpp] view plain copy
  1. fs@ubuntu:~/qiang/time$ ./asctime   
  2. Thu Dec 22 07:03:01 1956  
  3. fs@ubuntu:~/qiang/time$   

3)ctime() 函数

函数功能:ctime () 将参数timep所指的time_t结构中的信息转换成真实世界所使用的时间日期表示方法,然后将结果以字符串形态返回。此函数已经由时区转换成当地时间,字符串格式为"Wed Jun 30 21 :49 :08 1993/n"。若再调用相关的时间日期函数,此字符串可能会被破坏。

所需头文件#include <time.h>函数原型char *ctime(const time_t *timep);
参数说明timep 是由 time(NULL) 得到的日历时间;函数返回值返回字符串格式:星期,月,日,小时:分:秒,年

示例如下:

[cpp] view plain copy
  1. #include <stdio.h>   
  2. #include <time.h>   
  3. int main()   
  4. {  
  5.     time_t t;  
  6.     time(&t);  
  7.       
  8.     printf("Today's date and time: %s",ctime(&t));  
  9.     return 0;   
  10. }   

执行结果如下:

[cpp] view plain copy
  1. fs@ubuntu:~/qiang/time$ ./ctime   
  2. Today's date and time: Sat Jan  9 21:53:51 2016  
  3. fs@ubuntu:~/qiang/time$   
0 0
原创粉丝点击