1. 在这个头文件中共有四种类型,clock_t, size_t, time_t, struct tm
(1)clock_t比较系统格式化,它是用来返回函数clock衡量的一段时间。
一般是从一个文件开始到一个文件执行结束的时间。而这段时间衡量的单位
就是clock ticks, 在系统用CLOCKS_PER_SEC来表示1毫秒。
clock_t clock(void);
我们下面来举一个例子,如果要计算我们打开计算机的时间
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
clock_t start;
clock_t end;
start = clock();
system("start calc");
end = clock();
printf("the duration of opening calc is %f", (double)(end-start)/CLOCKS_PER_SEC);
system("pause");
}
(2)time_t表示日历时间,是用秒数来表示的时间的数据,它同时还是一个长整型数据。
对应的函数是time,用这个函数来获得日历时间,返回值为time_t。
time_t time(time_t *timer)
我们来举一个例子,如何计算现在的日历时间:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
time_t it;
it=time(NULL); //NULL 表示现在的时间
printf("the calender time now is %f", it);
system("pause");
}
(3)struct tm 用来表示另一种tm的时间形式,这被成为分解时间。
#ifndef _TM_DEFINED 
struct tm { 
int tm_sec; /* 秒 – 取值区间为[0,59] */ 
int tm_min; /* 分 - 取值区间为[0,59] */ 
int tm_hour; /* 时 - 取值区间为[0,23] */ 
int tm_mday; /* 一个月中的日期 - 取值区间为[1,31] */ 
int tm_mon; /* 月份(从一月开始,0代表一月) - 取值区间为[0,11] */ 
int tm_year; /* 年份,其值等于实际年份减去1900 */ 
int tm_wday; /* 星期 – 取值区间为[0,6],其中0代表星期天,1代表星期一,以此类推 */ 
int tm_yday; /* 从每年的1月1日开始的天数 – 取值区间为[0,365],其中0代表1月1日,1代表1月2日,以此类推 */ 
int tm_isdst; /* 夏令时标识符,实行夏令时的时候,tm_isdst为正。不实行夏令时的进候,tm_isdst为0;不了解情况时,tm_isdst()为负。*/ 
}; 
#define _TM_DEFINED 
#endif
2.函数形式
(1)gmtime(),localtime()可以将日历时间保存为一个tm对象
struct tm *gmtime(const time_t *timer);
struct tm *localtime(const time_t *timer);
其中gmtime()将日历时间转化为世界标准时间(格林尼治时间),
localtime()将日历时间转化为本地时间
我们可以将现在本地时间转化为分解时间
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
struct tm *local;
time_t it = time(NULL);
local = localtime(&it);
printf(asctime(
system("pause");
}
(2)mktime()可以将分解时间转化为日历时间
time_t mktime(struct tm *timeptr)
我们来一个例子,将今天的分解日期转变为日历时间
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main
()
{
struct tm *ti;
system("pause");
}
(3)asctime(),ctime()将时间用固定的格式显示出来,两者的返回值都是char*型的字符串。
利用printf函数输出日历返回值就是:星期几 月份 日期 时:分:秒 念/n/0 (/n为换行符,/0为空字符)
char *astime(const struct tm *timeptr);
char *ctime(const struct tm *timeptr);
其中asctime()函数直接通过tm结构来生成具有固定格式的保存时间的字符串,而ctime()函数
是将日历时间转化为本地时间,再生成字符串。
(4)difftime()函数用来计算持续时间的长度。clock()函数可以精确到毫秒,但是这个函数智能精确到
秒级。
double difftime(time_t time1, time_t time2);