C++将时间格式转换成秒数
#include <stdio.h>#include <time.h>
#include <string.h>
#include <stdlib.h>
time_t convert_str_to_tm(char * str_time)
{
struct tm tt;
memset(&tt,0,sizeof(tt));
tt.tm_year=atoi(str_time)-1900;
tt.tm_mon=atoi(str_time+5)-1;
tt.tm_mday=atoi(str_time+8);
tt.tm_hour=atoi(str_time+11);
tt.tm_min=atoi(str_time+14);
tt.tm_sec=atoi(str_time+17);
return mktime(&tt) + 28800;//28800是一个偏差。。加上这个。。刚好等于PHP的strtotime
}
int main()
{
char str_time = "2011-12-31 11:43:07";
printf("%d\n", convert_str_to_tm(str_time));
return 0;
}
文档来源:51CTO技术博客https://blog.51cto.com/u_15308480/3142960
页:
[1]