1、time 函数
头文件
#include <time.h>
函数定义
time_t time(time_t *tloc);
参数说明
tloc:一个指向 time_t 类型变量的指针。如果 tloc 不为 NULL,函数会将当前时间存储在 tloc 指向的变量中。
返回值
- 返回当前时间的秒数(time_t 类型)。
- 如果 tloc 为 NULL,则直接返回当前时间。
- 如果函数执行失败,返回 (time_t)-1。
示例
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main()
{
//time()
time_t tm;
time_t sec = time(&tm);
printf("tm = %ld sec = %ld\n",tm,sec);
}
2、gmtime 函数
头文件
#include <time.h>
函数定义
struct tm *gmtime(const time_t *timep);
struct tm *gmtime_r(const time_t *timep, struct tm *result);
参数说明
- timep:一个指向 time_t 类型变量的指针,表示从 Unix 纪元(1970年1月1日 00:00:00 UTC)到某个时间点的秒数。
- result:一个指向 struct tm 结构体的指针,用于存储转换后的 UTC 时间。
返回值
gmtime
返回一个指向 struct tm 结构体的指针,该结构体包含了分解后的 UTC 时间。
gmtime_r
如果成功,返回指向 result 的指针。
如果函数执行失败,返回 NULL。
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](0 表示 1 月)
int tm_year; // 年份(从 1900 年开始)
int tm_wday; // 星期 [0, 6](0 表示星期日)
int tm_yday; // 一年中的第几天 [0, 365]
int tm_isdst; // 夏令时标志(正数表示启用,0 表示未启用,负数表示未知)
};
gmtime
示例
#include <stdio.h>
#include <time.h>
int main() {
time_t current_time;
struct tm *utc_time;
// 获取当前时间戳
current_time = time(NULL);
// 将时间戳转换为 UTC 时间
utc_time = gmtime(¤t_time);
if (utc_time == NULL) {
perror("gmtime");
return 1;
}
// 打印 UTC 时间
printf("UTC time: %04d-%02d-%02d %02d:%02d:%02d\n",
utc_time->tm_year + 1900, // 年份需要加上 1900
utc_time->tm_mon + 1, // 月份需要加上 1
utc_time->tm_mday,
utc_time->tm_hour,
utc_time->tm_min,
utc_time->tm_sec);
return 0;
}
gmtime_r
示例
#include <stdio.h>
#include <time.h>
int main() {
time_t current_time;
struct tm utc_time;
// 获取当前时间戳
current_time = time(NULL);
// 将时间戳转换为 UTC 时间(线程安全)
if (gmtime_r(¤t_time, &utc_time) == NULL) {
perror("gmtime_r");
return 1;
}
// 打印 UTC 时间
printf("UTC time: %04d-%02d-%02d %02d:%02d:%02d\n",
utc_time.tm_year + 1900,
utc_time.tm_mon + 1,
utc_time.tm_mday,
utc_time.tm_hour,
utc_time.tm_min,
utc_time.tm_sec);
return 0;
}
区别说明
- 线程安全:
- gmtime_r 是线程安全的,因为它不依赖于静态内存,而是使用用户提供的 struct tm 结构体来存储结果。
- 返回值:
- gmtime_r 返回的是指向 result 的指针,如果函数执行成功,返回值与 result 相同。 如果函数执行失败,返回 NULL。
- 两者区别:
- gmtime 返回的是静态分配的 struct tm 结构体指针,因此不是线程安全的。
- gmtime_r 需要用户提供一个 struct tm 结构体来存储结果,因此是线程安全的。
3、asctime 函数
头文件
#include <time.h>
函数定义
char *asctime(const struct tm *tm);
char *asctime_r(const struct tm *tm, char *buf);
功能
asctime 函数将 struct tm 结构体表示的时间转换为一个固定格式的字符串,格式如下:
Www Mmm dd hh:mm:ss yyyy\n
- Www:星期几的缩写(例如 Mon 表示星期一)。
- Mmm:月份的缩写(例如 Jan 表示一月)。
- dd:日期(例如 01 表示 1 号)。
- hh:mm:ss:时间(小时、分钟、秒)。
- yyyy:年份。
- \n:换行符。
示例
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main()
{
//time()
time_t tm;
time_t current_time;
time_t sec = time(&tm);
printf("tm = %ld sec = %ld\n",tm,sec);
//gmtime()
struct tm *today = gmtime(&tm);
//asctime()
char *str = asctime(today);
printf("str = %s\n",str);
}
4、localtime 函数
头文件
#include <time.h>
函数定义
struct tm *localtime(const time_t *timep);
struct tm *localtime_r(const time_t *timep, struct tm *result);
功能
localtime
函数将一个 time_t
类型的时间戳转换为本地时间,并将其存储在 struct tm 结构体中。struct tm 结构体包含以下字段:
示例
#include <stdio.h>
#include <time.h>
#include <unistd.h>
int main()
{
//time()
time_t tm;
time_t current_time;
time_t sec = time(&tm);
printf("tm = %ld sec = %ld\n",tm,sec);
//gmtime()
struct tm *today = gmtime(&tm);
//localtime()
struct tm *today2 = localtime(&tm);
char *str2 = asctime(today2);
printf("%s\n",str2);
}
5、mktime 函数
头文件
#include <time.h>
函数定义
time_t mktime(struct tm *tm);
函数功能
mktime
函数将一个 struct tm 结构体表示的本地时间转换为 time_t 类型的时间戳。这个时间戳表示从 1970 年 1 月 1 日 00:00:00 UTC(Unix 纪元)开始经过的秒数。
示例
#include<stdio.h>
#include <time.h>
int main()
{
struct tm tim = {0};
tim.tm_year = 2025 - 1900;
tim.tm_mon = 3 - 1;
tim.tm_mday = 4;
tim.tm_hour = 11;
tim.tm_min = 13;
tim.tm_sec = 0;
time_t sec = mktime(&tim);
printf("sec = %ld\n",sec);
time_t sec2 = time(NULL);
printf("sec2 = %ld\n",sec2);
}
6、ctime 函数
头文件
#include <time.h>
函数定义
char *ctime(const time_t *timep);
参数说明
- timer 是一个指向 time_t 类型的时间值的指针
返回值
- 返回一个指向表示本地时间的字符串的指针。字符串的格式为 “
Day Mon dd hh:mm:ss yyyy\n
”,例如 “Wed Jun 30 21:49:08 1993\n
”。
示例
#include <stdio.h>
#include <time.h>
int main() {
time_t current_time;
time(¤t_time);
//time_t current_time = time(NULL);
printf("Current time: %s", ctime(¤t_time));
return 0;
}
与asctime函数的区别
- ctime 直接接受一个 time_t 类型的时间值,并将其转换为字符串。
- asctime 接受一个 tm 结构体,并将其转换为字符串。