c语言 mktime函数

2021-02-26, updated 2021-09-12

将struct tm转为时间戳

1
2
3
#include <time.h>

time_t mktime(struct tm *tm);

参数

1
2
3
#include <time.h>

typedef unsigned long time_t;    
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
#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 */
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#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;
}
words: 177 tags: c c库函数