能够将这类时间格式逐步分隔最后分解为,年,月,日,时,分,秒,可以直接调用mktime转化为时间戳。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main( ){
time_t time;
struct tm stm;
char str[] = "'2014-08-11 10:11:22'";
char *ptr = NULL;
char *str_data = NULL;
char *str_time = NULL;
char *str_year = NULL;
char *str_mon = NULL;
char *str_day = NULL;
char *str_hour = NULL;
char *str_min = NULL;
char *str_sec = NULL;
ptr = strtok(str,"'");
str_data = strtok_r(ptr," ",&str_time);
str_year = strtok_r(str_data,"-",&str_mon);
str_mon = strtok_r(str_mon,"-",&str_day);
str_hour = strtok_r(str_time,":",&str_min);
str_min = strtok_r(str_min,":",&str_sec);
stm.tm_year=year-1900;
stm.tm_mon=mon-1;
stm.tm_mday=day;
stm.tm_hour=hour;
stm.tm_min=min;
stm.tm_sec=sec;
time = mktime(&stm);
printf("time = %d",time);
return 0;
}
strtok函数(解析时间格式为时间戳)
最新推荐文章于 2023-04-20 10:14:37 发布