0
我必須做兩個轉換函數,從time_t到tm和從tm到time_t。從time_t和tm轉換錯誤的值
這是第一個:
string2time(string str){
time_t rawtime;
time(&rawtime);
tm* timeInfo = localtime(&rawtime);
stringstream ss(str);
string date;
string time;
getline(ss,date,' ');
getline(ss,time,' ');
string word;
//now we work with date..
stringstream sdate(date);
getline(sdate,word,'-');
timeInfo->tm_year = atoi(word.c_str()) -1900;
getline(sdate,word,'-');
timeInfo->tm_mon = atoi(word.c_str())-1;
getline(sdate,word,'-');
timeInfo->tm_mday = atoi(word.c_str());
//and time...
stringstream stime(time);
getline(stime,word,':');
timeInfo->tm_hour = atoi(word.c_str());
getline(stime,word,':');
timeInfo->tm_min = atoi(word.c_str());
getline(stime,word,':');
timeInfo->tm_sec = atoi(word.c_str());
return mktime(timeInfo);
}
,這是第二個:
time2str(time_t t){
tm* myT;
myT = localtime(&t);
//here i have to explore myT structure in order to build a proper string
}
我錯誤的值與2013年3月10日00:00:00 在anyway..starting我得到的tm結構2013-04-21 18:16:29 ...爲什麼?
編輯: 取得了一些進展!這段代碼一直工作,但小時爲00!
這是一個提示:110似乎是*年*不是一天中的當天(當天) – akhisp 2013-04-21 16:11:09
是的我打電話給錯誤的領域,反正它仍然不工作... – Phate 2013-04-21 16:17:17
你打電話給你怎麼打電話這些功能? – akhisp 2013-04-21 16:22:36