首页 > 代码库 > 时间函数

时间函数

#include<time.h>//系统当前日历的时间time_t time(time_t *time);//返回以tm结构存储的一个时间struct tm *localtime(const time_t *time);//返回字符串形式的时间表示char *asctime(const struct tm *ptr);//返回以tm结构存储的一个UTC时间,就是格林尼治时间struct tm *gmtime(const time_t *time);

以上是时间函数,具体使用如下:

 1 #include<stdio.h> 2 #include<time.h> 3  4 int main(void) 5 { 6     struct tm *local; 7     time_t tm; 8  9     tm=time(NULL);10 11     local=localtime(&tm);12 13     printf("Local time and date is: %s\n",asctime(local));14 15     local=gmtime(&tm);16     printf("TUC time an date id : %s\n",asctime(local));17 18     return 0;19 }

 

结果是

 

Local time and date is: Sun Sep  7 15:01:54 2014

TUC time an date id : Sun Sep  7 07:01:54 2014

可以看出二者形式类似,通过asc转换出来的字符串形式。

gm和local都是接受time返回的time,将其转换为标准时间存储到结构tm中,并通过asc进行字符串转换并显示。

时间函数