c语言 strptime函数
2021-02-26, updated 2021-09-12
读取将格式化时间
原型
#include <time.h>
char *strptime(const char *s, const char *format, struct tm *tm);
参数
- s: 需要获取的时间字符串
- tm: 存放时间的变量
- format: 描述文件格式
- %d: The day of month (1-31).
- %H: The hour (0-23).
- %m: The month number (1-12).
- %M: The minute (0-59).
- %S: The second (0-60; 60 may occur for leap seconds; earlier also 61 was allowed).
- %Y: The year, including century (for example, 1991).
相关结构体
#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;
}