C++ string 转换为 time_t

来源:百度知道 编辑:UC知道 时间:2024/06/11 18:08:33
#include<iostream>
#include<string>
#include<time.h>
#include<stdio.h>

using namespace std;

time_t stringTOtime_t(char * szTime);

int main()
{
string str="2009 1 1 1 2 3 4 1";
string str2="2009 1 2 2 3 5 5 2";
char ch1[100],ch2[100];
strcpy(ch1,str.c_str());
strcpy(ch2,str.c_str());

time_t time1,time2;
time1=stringTOtime_t(ch1);
time2=stringTOtime_t(ch2);
double dd=difftime(time2,time1);
cout<<"dd= "<<dd<<endl;

system("pause");
return 0;
}

time_t stringTOtime_t(char * szTime)
{
struct tm tm1;
time_t time1;
int i=sscanf(szTime,"%d%d%d%d%d%d%d%d" ,
&(tm1.tm_year),
&(tm1.tm_mon),
&(

int main()
{
string str="2009 1 1 1 2 3 4 1";
string str2="2009 1 2 2 3 5 5 2";
char ch1[100],ch2[100];
strcpy(ch1,str.c_str());
strcpy(ch2,str2.c_str());//这里改成str2 你还是吧str1给了ch2 所以都一样了

time_t time1,time2;
................