[英]Wrong values in conversion from time_t and tm
我必須做兩個轉換功能,從time_t到tm和從tm到time_t。
這是第一個:
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-03-10 00:00:00開始在tm結構中我得到2013-04-21 18:16:29 ...為什么?
編輯:取得了一些進展! 該代碼在小時為00時始終有效!
解決了,只是字符串轉換中的一個錯誤...很抱歉。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.