简介
<time.h> 是C语言标准库中的一个头文件,提供了处理日期和时间的功能。这个头文件包含了多种类型、宏定义以及函数,可以用来获取当前时间、格式化时间输出等。
主要类型
time_t
time_t 类型用于表示从某个固定起点(通常是1970年1月1日午夜)到现在的秒数。这个类型通常用来存储时间戳。
struct tm
struct tm 是一个结构体,用于表示日期和时间。它包含了多个成员变量,分别表示年、月、日、小时、分钟、秒等。
// www.javascriptcn.com code example
struct tm {
int tm_sec; // 秒,范围为 0 到 61
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; // 夏令时标志
};主要宏定义
CLOCKS_PER_SEC
CLOCKS_PER_SEC 宏定义了每秒的时钟计时器滴答数。这主要用于计算程序运行时间。
主要函数
time()
time() 函数用于获取当前的时间戳。
time_t time(time_t *t);
示例:
// www.javascriptcn.com code example
#include <stdio.h>
#include <time.h>
int main() {
time_t t;
time(&t);
printf("当前时间戳:%ld\n", t);
return 0;
}localtime()
localtime() 函数将时间戳转换为本地时间,并返回一个指向 struct tm 的指针。
struct tm *localtime(const time_t *timep);
示例:
// www.javascriptcn.com code example
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm *local_time = localtime(&t);
printf("本地时间:\n");
printf("年:%d\n", local_time->tm_year + 1900);
printf("月:%d\n", local_time->tm_mon + 1);
printf("日:%d\n", local_time->tm_mday);
printf("小时:%d\n", local_time->tm_hour);
printf("分钟:%d\n", local_time->tm_min);
printf("秒:%d\n", local_time->tm_sec);
return 0;
}gmtime()
gmtime() 函数将时间戳转换为协调世界时(UTC),并返回一个指向 struct tm 的指针。
struct tm *gmtime(const time_t *timep);
示例:
// www.javascriptcn.com code example
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm *utc_time = gmtime(&t);
printf("UTC时间:\n");
printf("年:%d\n", utc_time->tm_year + 1900);
printf("月:%d\n", utc_time->tm_mon + 1);
printf("日:%d\n", utc_time->tm_mday);
printf("小时:%d\n", utc_time->tm_hour);
printf("分钟:%d\n", utc_time->tm_min);
printf("秒:%d\n", utc_time->tm_sec);
return 0;
}asctime()
asctime() 函数将 struct tm 结构体转换为一个字符串,表示日期和时间。
char *asctime(const struct tm *timeptr);
示例:
// www.javascriptcn.com code example
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm *local_time = localtime(&t);
char *time_str = asctime(local_time);
printf("当前时间:\n%s\n", time_str);
return 0;
}strftime()
strftime() 函数允许你自定义日期和时间的格式。
size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr);
示例:
// www.javascriptcn.com code example
#include <stdio.h>
#include <time.h>
int main() {
time_t t = time(NULL);
struct tm *local_time = localtime(&t);
char buffer[80];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", local_time);
printf("当前时间:\n%s\n", buffer);
return 0;
}示例代码
下面是一个综合示例,展示了如何使用 <time.h> 中的各种功能。
// www.javascriptcn.com code example
#include <stdio.h>
#include <time.h>
int main() {
// 获取当前时间戳
time_t t = time(NULL);
printf("当前时间戳:%ld\n", t);
// 将时间戳转换为本地时间
struct tm *local_time = localtime(&t);
printf("本地时间:\n");
printf("年:%d\n", local_time->tm_year + 1900);
printf("月:%d\n", local_time->tm_mon + 1);
printf("日:%d\n", local_time->tm_mday);
printf("小时:%d\n", local_time->tm_hour);
printf("分钟:%d\n", local_time->tm_min);
printf("秒:%d\n", local_time->tm_sec);
// 将时间戳转换为UTC时间
struct tm *utc_time = gmtime(&t);
printf("UTC时间:\n");
printf("年:%d\n", utc_time->tm_year + 1900);
printf("月:%d\n", utc_time->tm_mon + 1);
printf("日:%d\n", utc_time->tm_mday);
printf("小时:%d\n", utc_time->tm_hour);
printf("分钟:%d\n", utc_time->tm_min);
printf("秒:%d\n", utc_time->tm_sec);
// 将时间转换为字符串
char *time_str = asctime(local_time);
printf("当前时间:\n%s\n", time_str);
// 自定义时间格式
char buffer[80];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", local_time);
printf("自定义时间格式:\n%s\n", buffer);
return 0;
}以上代码展示了如何使用 <time.h> 中的各个功能来处理日期和时间。希望这些内容对你有所帮助!