time.h 数据类型
time_t (unsigned int)
clock_t (这是一个适合存储处理器时间的类型)
size_t (是无符号整数类型,它是 sizeof 关键字的结果)
struct tm (这是一个用来保存时间和日期的结构。)
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 到 11 */
int tm_year; /* 自 1900 起的年数 */
int tm_wday; /* 一周中的第几天,范围从 0 到 6 */
int tm_yday; /* 一年中的第几天,范围从 0 到 365 */
int tm_isdst; /* 夏令时 */
};
time.h 宏
- NULL (这个宏是一个空指针常量的值)
- CLOCKS_PER_SEC (这个宏表示每秒的处理器时钟个数)
time.h 库函数
1. time_t time(time_t *seconds)
这个函数的功能是返回(1970-01-01 00:00:00 UTC)起经过的时间,以秒为单位。如果 seconds 不为空,则返回值也存储在变量 seconds 中。也就说此函数有两种用法:
- 程序示例:
#include <stdio.h>
#include <time.h>
int main(void){
time_t seconds;
time(&seconds); //seconds不为空
printf("seconds1 is : %d\n", (int)seconds);
seconds = time(NULL); //seconds为空
printf("seconds2 is : %d\n", (int)seconds);
return 0;
}
//运行结果
seconds1 is : 1423533473
seconds2 is : 1423533473
程序可以运行,但是在变量还是指针之间的切换不免令人厌烦,所以建议在选择程序用法的时候就确定变量的声明方式,比如选择seconds非空方式则将seconds定义成指向time_t类型的指针,若选择seconds为空的方式,即NULL(空指针)作为参数的方式,则将seconds声明成一个time_t类型的变量。
2. struct tm *localtime(const time_t *timer)
这个函数的功能是使用 timer 的值来填充 tm 结构。timer 的值被分解为 tm 结构,并用本地时区表示, 并返回该tm结构。
#include<stdio.h>
#include<time.h>
int main(void){
struct tm* t;
time_t timer;
time(&timer);
t = localtime(&timer); //tm 类型的timer
//可以访问其成员tm_year,1900年至今的年数
printf("%d\n", t ->tm_year);
}
//运行结果
115
3. size_t strftime(char* str, size_t maxsize, const char* format, const struct tm* timeptr)
这个函数的功能是将tm结构体按照format格式,以字符串的形式写到长度为maxsize的字符数组中。
char* str //普通的字符指针,表示字符串
size_t maxsize //sizeof结果类型的整数
const char* format //字符串
const struct tm* timeptr //指向tm结构体的指针
#include<stdio.h>
#include<time.h>
#define LEN 80 //宏,LEN=80
int main(void){
char* str[LEN]
const char* format[] = "%X";
size_t maxsize = LEN;
time_t t;
time(&t);
struct tm* timeptr;
timeptr = localtime(&t);
strftime(str, maxsize, format, timeptr);
printf("%s\n", str);
return 0;
}
//运行结果:
10:46:04
4. size_t mktime(const struct tm* timeptr)
这个函数的功能是把 timeptr 所指向的结构转换为一个依据本地时区的 time_t 值。
#include<stdio.h>
#include<time.h>
int main(void){
int ret;
time_t seconds;
char buffer[80];
struct tm timeptr;
timeptr.tm_sec = 20; //0-59
timeptr.tm_min = 30; //0-59
timeptr.tm_hour = 16; //0-23
timeptr.tm_year = 2015-1900; //自1900年的年数
timeptr.tm_mon = 8; //0-11
//timeptr.tm_wday = 5; //0-6
timeptr.tm_mday = 20; //1-31
//timeptr.tm_yday = 125; //0-365
timeptr.tm_isdst = -1;
seconds = mktime(&timeptr);
printf("seconds is :%d\n", (int)seconds);
ret = mktime(&timeptr);
if( ret == -1 ){
printf("错误:不能使用 mktime 转换时间。\n");
}else{
strftime(buffer, sizeof(buffer), "%c", &timeptr);
printf("%s\n",buffer);
}
return 0;
}
//运行结果:
1442737820
Sun Sep 20 16:30:20 2015
5. struct tm *gmtime(const time_t *timer)
这个函数的功能是使用 timer 的值来填充 tm 结构,并用协调世界时(UTC)也被称为格林尼治标准时间(GMT)表示。
#include<stdio.h>
#include<time.h>
#define BST (+1)
#define CCT (+8)
int main(void){
struct tm* gm;
time_t seconds;
time(&seconds);
gm = gmtime(&seconds);
printf("当前的世界时钟:\n");
printf("伦敦:%2d:%02d\n", (gm->tm_hour+BST)%24, gm->tm_min);
printf("北京:%2d:%02d\n", (gm->tm_hour+CCT)%24, gm->tm_min);
return 0;
}
//运行结果:
当前的世界时钟:
伦敦: 4:16
北京:11:16
6. char *asctime(const struct tm *timeptr)
这个函数的功能是返回一个指向字符串的指针,它代表了结构 struct timeptr 的日期和时间。
#include <stdio.h>
#include <string.h>
#include <time.h>
int main()
{
struct tm t;
t.tm_sec = 10;
t.tm_min = 10;
t.tm_hour = 6;
t.tm_mday = 25;
t.tm_mon = 2;
t.tm_year = 89;
t.tm_wday = 6;
puts(asctime(&t));
return 0;
}
//运行结果:
Sat Mar 25 06:10:10 1989
7. char *ctime(const time_t *timeptr)
这个函数的功能是返回一个表示当地时间的字符串,当地时间是基于参数 timer。
返回的字符串格式如下: Www Mmm dd hh:mm:ss yyyy 其中,Www 表示星期几,Mmm 是以字母表示的月份,dd 表示一月中的第几天,hh:mm:ss 表示时间,yyyy 表示年份
#include <stdio.h>
#include <time.h>
int main()
{
time_t curtime;
time(&curtime);
puts("s%\n", ctime(&curtime));
return 0;
}
//运行结果:
Tue Feb 10 11:48:46 2015
8. double difftime(time_t time1, time_t time2)
返回 time1 和 time2 之间相差的秒数 (time1 - time2)。这两个时间是在日历时间中指定的,表示了自纪元 Epoch(协调世界时 UTC:1970-01-01 00:00:00)起经过的时间。
#include<stdio.h>
#include<time.h>
int main(void){
clock_t start_t, end_t, total_t;
int i;
start_t = clock();
printf("程序启动,start_t = %ld\n", start_t);
printf("开始一个大循环,start_t = %ld\n", start_t);
for(i=0; i< 10000000; i++){
}
end_t = clock();
printf("大循环结束,end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("CPU 占用的总时间:%f8\n", total_t);
printf("程序退出...\n");
return 0;
}
运行结果:
程序启动...
休眠 5 秒...
执行时间 = 5.000000
程序退出...
9. double clock(void)
返回程序执行起(一般为程序的开头),处理器时钟所使用的时间。为了获取 CPU 所使用的秒数,您需要除以 CLOCKS_PER_SEC。在 32 位系统中,CLOCKS_PER_SEC 等于 1000000,该函数大约每 72 分钟会返回相同的值。
#include <time.h>
#include <stdio.h>
int main()
{
clock_t start_t, end_t, total_t;
int i;
start_t = clock();
printf("程序启动,start_t = %ld\n", start_t);
printf("开始一个大循环,start_t = %ld\n", start_t);
for(i=0; i< 10000000; i++)
{
}
end_t = clock();
printf("大循环结束,end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t) / CLOCKS_PER_SEC;
printf("CPU 占用的总时间:%f\n", total_t );
printf("程序退出...\n");
return(0);
}
运行结果:
程序启动,start_t = 0
开始一个大循环,start_t = 0
大循环结束,end_t = 20000
CPU 占用的总时间:0.000000
程序退出...