time_t mktime(struct tm *tmp)
将struct tm
表示的时间转换为time_t
表示的时间。
struct tm
的定义:
struct tm {
int tm_sec; /* seconds [0,60] */
int tm_min; /* minutes [0,59] */
int tm_hour; /* hours [0,23]*/
int tm_mday; /* day of the month [1,31] */
int tm_mon; /* month [0,11] */
int tm_year; /* year since 1900 */
int tm_wday; /* day of the week [0,6] (Sunday =0) */
int tm_yday; /* day in the year [0,365] */
int tm_isdst; /* daylight saving time */
};
time_t
的定义:
typedef long int __time_t;
typedef __time_t time_t;
time_t
表示从1970-01-01 00:00:00
,到某个时间点,所经过的秒
数, 它是相对时间。
time_t
是一个有符号数
,也就是它也可以是负数
,表示1970-01-01 00:00:00
之前的时间。
#include <stdio.h>
#include <time.h>
int main() {
struct tm tms = {.tm_year = 2019 - 1900, .tm_mon = 3 - 1, .tm_mday = 18};
time_t tt = mktime(&tms);
printf("tt = %ld\n", tt2);
return 0;
}
使用cc命令编译 ⤵︎
cc -o test_time test.c
运行结果如下 ⤵︎
tt = 1552838400