C 语言 详解:时间戳、struct tm 与格式化输出怎么用
时间:2026-08-22 | 作者:白桃企划师 | 阅读:0简介
这个头文件,可是C语言标准库中的一员大将,它专门负责处理日期和时间相关的功能。这里面有各种各样的类型、宏定义以及函数,通过它们,咱们就能轻松获取当前时间,还能对时间输出进行格式化处理呢。
主要类型
time_t
time_t类型用于表示时间,它的值是从1970年1月1日午夜(通常被视为固定起点)到当前时刻所经过的秒数。这种类型一般被用于存储时间戳。
struct tm
struct tm 是一个结构体,用于表示日期和时间。它包含了多个成员变量,分别表示年、月、日、小时、分钟、秒等。
// www.ja vascriptcn.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.ja vascriptcn.com code example #include#include int main() { time_t t; time(&t); printf("当前时间戳:%ldn", t); return 0; }
localtime()
localtime() 函数将时间戳转换为本地时间,并返回一个指向 struct tm 的指针。
struct tm *localtime(const time_t *timep);
示例:
// www.ja vascriptcn.com code example #include#include int main() { time_t t = time(NULL); struct tm *local_time = localtime(&t); printf("本地时间:n"); printf("年:%dn", local_time->tm_year + 1900); printf("月:%dn", local_time->tm_mon + 1); printf("日:%dn", local_time->tm_mday); printf("小时:%dn", local_time->tm_hour); printf("分钟:%dn", local_time->tm_min); printf("秒:%dn", local_time->tm_sec); return 0; }
gmtime()
gmtime() 函数将时间戳转换为协调世界时(UTC),并返回一个指向 struct tm 的指针。
struct tm *gmtime(const time_t *timep);
示例:
// www.ja vascriptcn.com code example #include#include int main() { time_t t = time(NULL); struct tm *utc_time = gmtime(&t); printf("UTC时间:n"); printf("年:%dn", utc_time->tm_year + 1900); printf("月:%dn", utc_time->tm_mon + 1); printf("日:%dn", utc_time->tm_mday); printf("小时:%dn", utc_time->tm_hour); printf("分钟:%dn", utc_time->tm_min); printf("秒:%dn", utc_time->tm_sec); return 0; }
asctime()
asctime() 函数将 struct tm 结构体转换为一个字符串,表示日期和时间。
char *asctime(const struct tm *timeptr);
示例:
// www.ja vascriptcn.com code example #include#include int main() { time_t t = time(NULL); struct tm *local_time = localtime(&t); char *time_str = asctime(local_time); printf("当前时间:n%sn", time_str); return 0; }
strftime()
strftime() 函数允许你自定义日期和时间的格式。
size_t strftime(char *s, size_t maxsize, const char *format, const struct tm *timeptr);
示例:
// www.ja vascriptcn.com code example #include#include 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%sn", buffer); return 0; }
示例代码
下面是一个综合示例,展示了如何使用 中的各种功能。
// www.ja vascriptcn.com code example #include#include int main() { // 获取当前时间戳 time_t t = time(NULL); printf("当前时间戳:%ldn", t); // 将时间戳转换为本地时间 struct tm *local_time = localtime(&t); printf("本地时间:n"); printf("年:%dn", local_time->tm_year + 1900); printf("月:%dn", local_time->tm_mon + 1); printf("日:%dn", local_time->tm_mday); printf("小时:%dn", local_time->tm_hour); printf("分钟:%dn", local_time->tm_min); printf("秒:%dn", local_time->tm_sec); // 将时间戳转换为UTC时间 struct tm *utc_time = gmtime(&t); printf("UTC时间:n"); printf("年:%dn", utc_time->tm_year + 1900); printf("月:%dn", utc_time->tm_mon + 1); printf("日:%dn", utc_time->tm_mday); printf("小时:%dn", utc_time->tm_hour); printf("分钟:%dn", utc_time->tm_min); printf("秒:%dn", utc_time->tm_sec); // 将时间转换为字符串 char *time_str = asctime(local_time); printf("当前时间:n%sn", time_str); // 自定义时间格式 char buffer[80]; strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", local_time); printf("自定义时间格式:n%sn", buffer); return 0; }
以上代码展示了如何使用 中的各个功能来处理日期和时间。希望这些内容对你有所帮助!
免责声明:文中图文均来自网络,如有侵权请联系删除,心愿游戏发布此文仅为传递信息,不代表心愿游戏认同其观点或证实其描述。
相关文章
更多-
- 【C语言基础】:深入理解指针(三)
- 时间:2026-08-27
-
- C语言基础:字符与字符串函数详解及模拟实现
- 时间:2026-08-27
-
- C语言进阶:深入解析字符串、结构体与typedef用法
- 时间:2026-08-27
-
- LibCoroutine:细粒度C语言协程库开发解析
- 时间:2026-08-27
-
- C语言分支与循环入门:if、switch 和三种循环该怎么用
- 时间:2026-08-25
-
- C 语言字符指针入门:搞清地址、字符串常量与数组的区别
- 时间:2026-08-24
-
- C语言指针入门:从地址、数组到函数调用一次讲清
- 时间:2026-08-24
-
- C 语言自定义数据类型梳理:枚举、结构体、共用体与 typedef
- 时间:2026-08-24
精选合集
更多大家都在玩
大家都在看
更多-
- 2026年9月17日小鸡庄园答案
- 时间:2026-09-16
-
- 蚂蚁庄园今日答案2026年9月17日
- 时间:2026-09-16
-
- 蚂蚁庄园小课堂今日最新答案2026年9月17日
- 时间:2026-09-16
-
- 蚂蚁庄园小鸡答题今日答案2026年9月17日
- 时间:2026-09-16
-
- 褪黑素主要由人体哪个器官分泌 蚂蚁庄园今日答案9.17
- 时间:2026-09-16
-
- 蚂蚁庄园今天答题答案2026年9月17日
- 时间:2026-09-16
-
- 蚂蚁庄园答题今日答案2026年9月17日
- 时间:2026-09-16
-
- 研学旅游指导师的核心服务对象是 蚂蚁新村今日答案2026.9.16
- 时间:2026-09-16
