所使用的的头文件如下:
#include "iostream"
#include "time.h"
#include "string.h"
using namespace std;
代码如下:
int standard_to_stamp(char *str_time)
{
struct tm stm;
int iY, iM, iD, iH, iMin, iS;
memset(&stm,0,sizeof(stm));
iY = atoi(str_time);
iM = atoi(str_time+5);
iD = atoi(str_time+8);
iH = atoi(str_time+11);
iMin = atoi(str_time+14);
iS = atoi(str_time+17);
stm.tm_year=iY-1900;
stm.tm_mon=iM-1;
stm.tm_mday=iD;
stm.tm_hour=iH;
stm.tm_min=iMin;
stm.tm_sec=iS;
printf("%d-%0d-%0d %0d:%0d:%0d\n", iY, iM, iD, iH, iMin, iS); //标准时间格式例如:2016:08:02 12:12:30
return (int)mktime(&stm);
}
int main()
{
int stampTime = standard_to_stamp("2016:08:02 12:12:30");
cout << stampTime << endl;
return 0;
}
注:里面的atoi(s+5); +8、+11、+14、+17表示的是传入参数的格式中每个数字距离字符串开头的长度;
运行结果如下: