time.h 头文件定义了四个变量类型、两个宏和各种操作日期和时间的函数。
库变量
size_t 是无符号整数类型,它是sizeof关键字的结果。
clock_t 是一个适合存储处理器时间的类型
time_t is 是一个适合存储日历时间类型
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; / 夏令时 */
};
库宏
NULL 一个空指针常量的值
CLOCK_PER_SEC 表示每秒处理器时钟个数
库函数
char *asctime(const struct tm *timeptr)
返回一个指向字符串的指针,它代表了结构timeptr的日期和时间
返回值: 返回一个C字符串,包含了可读格式的日期和时间信息 Www Mmm dd hh:mm:ss yyyy
void main()
{
struct tm time;
time.tm_sec = 10;
time.tm_min = 45;
time.tm_hour = 22;
time.tm_mday = 8;
time.tm_mon = 10;
time.tm_year = 118;
time.tm_wday = 1;
puts(asctime(&time));
getchar();
}
C库函数clock_t clock(void) 返回程序执行起处理器时钟所用的时间。为了获取CPU所使用的秒数,你需要除以 CLOCKS_PER_SEC.
在32位系统中,CLOCKS_PER_SEC等于 1000 000,该函数大约每72分钟会返回相同的值。
返回值: 该函数返回自程序启动起,处理器时钟所使用的时间。如果失败,则返回 -1 值。
void main()
{
clock_t start_t, end_t;
double total_t;
int i;
start_t = clock();
printf("程序启动,start_t = %ld\n", start_t);
for(i = 0; i < 10000000; i++)
{
5+3;
}
end_t = clock();
printf("循环结束, end_t = %ld\n", end_t);
total_t = (double)(end_t - start_t)/CLOCKS_PER_SEC;
printf("CPU占用的时间为:total_t = %f\n", total_t);
getchar();
}
C库函数 char *ctime(const time_t *timer)返回一个表示当地时间的字符串,当当地时间是基于参数 timer.
void main()
{
time_t currentTime;
printf("当前时间: %s\n", ctime(¤tTime));
getchar();
}
![](https://img.haomeiwen.com/i13613752/259a0772af2f2902.png)
C库函数double difftime(time_t time1, time_t time2) 返回time 1 和 time 2之间相差的秒数(time1 - time2).
#include <stdio.h>
#include <time.h>
#include <unistd.h>
void main()
{
time_t start_t, end_t;
double diff_t;
printf("启动程序。。。\n");
time(&start_t);
sleep(6);
time(&end_t);
diff_t = difftime(end_t, start_t);
printf("执行时间: %f\n", diff_t);
getchar();
}
C库函数 struct tm *gmtime(const time_t *timer)使用timer 的值来填充tm结构,并用协调世界时间(UTC)也被称为格林尼治标准时间(GMT)表示。
#define CCT 8
void main()
{
time_t current_t;
struct tm *pstCurrentTime;
time(¤t_t);
pstCurrentTime = gmtime(¤t_t);
printf("当前的世界时钟为: \n");
printf("中国:%.2d: %02d \n", (pstCurrentTime->tm_hour+CCT)%24, pstCurrentTime- >tm_min);
getchar();
}
C库函数struct *localtime(const time_t *timer)使用timer的值来填充tm结构。timer的值被分解为tm结构,并用本地时区表示。
C库函数 time_t mktime(struct tm *timerptr) 把timeptr所指向的结构转换为一个依据本地时区的time_t的值。
void main()
{
int year, month, day;
time_t rawTime;
struct tm * pstTime;
const char * weekday[] = {"周日", "周一",
"周二", "周三", "周四", "周五", "周六"};
printf("年:");;
scanf("%d", &year);
printf("月:");
scanf("%d", &month);
printf("日:");
scanf("%d", &day);
time(&rawTime);
pstTime = localtime(&rawTime);
pstTime->tm_year = year - 1900;
pstTime->tm_mon = month - 1;
pstTime->tm_mday = day;
mktime(pstTime);
printf("这一天是:%s\n", weekday[pstTime->tm_wday]);
getchar();
}
![](https://img.haomeiwen.com/i13613752/2d3f22b525ebd916.png)
C库函数 size_t strftime(char *str, size_t maxsize, const char *format, const struct tm *timeptr)根据format中定义的格式化规则,格式化结构timeptr表示的时间,并把它存储在str中。
void main()
{
time_t currentTime;
struct tm *pstTime;
char cOut[52];
time(¤tTime);
pstTime = localtime(¤tTime);
strftime(cOut, 52, "%Y-%m-%d %H:%M:%S", pstTime);
printf("格式化的日期时间为:%s\n", cOut);
getchar();
}
C库函数 time_t time(time_t *seconds)返回自Epoch起经过的时间,以秒为单位。
如果seconds不为空,则返回值也存储在seconds中。
void main()
{
time_t seconds;
seconds = time(NULL);
printf("自1970年起的小时数为:%ld\n", seconds/3600);
printf("自1970年起的天数为:%ld\n", seconds/3600/24);
getchar();
}
网友评论