c语言 mktime函数
2021-02-26, updated 2021-09-12
将struct tm转为时间戳
#include <time.h>
time_t mktime(struct tm *tm);
参数
- tm: 需要转换的时间
#include <time.h>
typedef unsigned long time_t;
#include <time.h>
struct tm {
int tm_sec; /* seconds */
int tm_min; /* minutes */
int tm_hour; /* hours */
int tm_mday; /* day of the month */
int tm_mon; /* month */
int tm_year; /* year */
int tm_wday; /* day of the week */
int tm_yday; /* day in the year */
int tm_isdst; /* daylight saving time */
};
#include <stdio.h>
#include <time.h>
// #include <sys/time.h>
int main()
{
// char *str = "Fri, 26 Feb 2021 02:22:08 GMT";
char *str = "2021-2-26 02:22:08";
struct tm time;
time_t tt;
strptime(str, "%Y-%m-%d %H:%M:%S", &time);
tt = mktime(&time);
printf("%lu\n", tt);
return 0;
}