I have to do two conversion functions, from time_t to tm and from tm to time_t.
This is the first:
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);
}
and this is the second:
time2str(time_t t){
tm* myT;
myT = localtime(&t);
//here i have to explore myT structure in order to build a proper string
}
I got wrong values anyway..starting with 2013-03-10 00:00:00 in the tm structure I get 2013-04-21 18:16:29 ... why?
edit: made some progress! This code works all time BUT when hour is 00!
解决了,只是字符串转换中的一个错误...很抱歉。
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.