#include <stdio.h>
#include <time.h>
#include <iostream>
using namespace std;
time_t transStrToTimes(string str){
struct tm time_fields;
time_t seconds;
time_fields.tm_year = atoi(str.substr(0,4).c_str())-1900;
time_fields.tm_mon = atoi(str.substr(4,2).c_str());
time_fields.tm_mday = atoi(str.substr(6,2).c_str());
time_fields.tm_hour = atoi(str.substr(8,2).c_str());
time_fields.tm_min = atoi(str.substr(10,2).c_str());
time_fields.tm_sec = atoi(str.substr(12,2).c_str());
seconds=mktime(&time_fields);
printf("--------------seconds = %ld",seconds);
return seconds;
}
int main(int argc, char* argv[])
{
string startTime="";
if(argc!=2)
{
printf("參數錯誤,請輸入開始和結束時間:\n");
printf("%s startTiem endTiem\n",argv[0]);
return -1;
}
else{
startTime=argv[1];
if( startTime.length()!=14 )
{
printf("時間位數不夠,請輸入14位的時間!\n");
return -1;
}
}
time_t start_Seconds;
start_Seconds = transStrToTimes(startTime);
printf("start_Seconds=%ld===%f \n",start_Seconds, difftime(start_Seconds,0));
return 1;
}