c语言 strptime函数

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

读取将格式化时间

原型

#include <time.h>

char *strptime(const char *s, const char *format, struct tm *tm);

参数

相关结构体

#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;
}
words: 199 tags: c c库函数