[英]Wrong values in conversion from time_t and tm
I have to do two conversion functions, from time_t to tm and from tm to time_t. 我必须做两个转换功能,从time_t到tm和从tm到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? 无论如何我都得到了错误的值..从2013-03-10 00:00:00开始在tm结构中我得到2013-04-21 18:16:29 ...为什么?
edit: made some progress! 编辑:取得了一些进展! This code works all time BUT when hour is 00! 该代码在小时为00时始终有效!
解决了,只是字符串转换中的一个错误...很抱歉。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.