码迷,mamicode.com
首页 > 其他好文 > 详细

时间函数

时间:2014-09-07 16:01:25      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   使用   ar   2014   div   cti   

#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进行字符串转换并显示。

时间函数

标签:style   blog   color   io   使用   ar   2014   div   cti   

原文地址:http://www.cnblogs.com/lhyz/p/3960446.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!