【发布时间】:2018-06-04 22:13:00
【问题描述】:
给定的函数是用于处理日期和时间的类的一部分。我解析的文件需要将给定的字符串数据转换为 time_t 但 mktime 不起作用。为什么?
struct tm DateTimeUtils::makeTime(string arrTime)//accepts in format"2315"means 11.15 pm
{
struct tm neww;
string hour = arrTime.substr(0,2);
int hour_int = stoi(hour);
neww.tm_hour=hour_int;//when this is directly printed generates correct value
string minute = arrTime.substr(2,2);
int minute_int = stoi(minute);
neww.tm_min=(minute_int);//when this is directly printed generates correct value
time_t t1 = mktime(&neww);//only returns -1
cout<<t1;
return neww;
}
【问题讨论】:
-
tm 结构中不仅有 tm_min 和 tm_hour。见这里:cplusplus.com/reference/ctime/tm
-
我通过添加以下代码修改了代码: struct tm DateTimeUtils::makeTime(string arrTime) { struct tm neww;新w.tm_sec=0-61;新w.tm_mday=1-31;新w.tm_mon=0-11;新w.tm_year=1900;新w.tm_wday=0-6;新w.tm_yday=0-365;字符串小时 = arrTime.substr(0,2); int hour_int = stoi(小时); neww.tm_hour=hour_int;字符串分钟 = arrTime.substr(2,2); int minute_int = stoi(分钟); neww.tm_min=(minute_int); time_t t1 = mktime(&neww); cout
-
“我修改了代码...” - 太好了。现在modify your question 在您的更改中包含一个附录。代码墙不属于 cmets;它们属于已发布的问题,理想情况下,它们会被最小化以仅展示与您的问题相关的问题。