时间字符串转换成秒

#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;
 }

猜你喜欢

转载自blog.csdn.net/buguoerer/article/details/49047911