I have a string with the following contents: "1950-01-01 00:00:00".
I have the following code. It should create a time_t object from the string. But it crashes and I don't understand why..
Why does mktime return -1? Is something wrong with the tm instance?Code:tm dbTime;
dbTime.tm_year = atoi((s.substr(0, 4)).c_str()) - 1900;
dbTime.tm_mon = atoi((s.substr(5, 2)).c_str());
dbTime.tm_mday = atoi((s.substr(8, 2)).c_str());
dbTime.tm_hour = atoi((s.substr(11,2)).c_str());
dbTime.tm_min = atoi((s.substr(14, 2)).c_str());
dbTime.tm_sec = atoi((s.substr(7, 2)).c_str());
dbTime.tm_isdst = false;
// Seems to be correct:
cout << "datetime values read: " << endl;
cout << dbTime.tm_year << endl;
cout << dbTime.tm_mon << endl;
cout << dbTime.tm_mday << endl;
cout << dbTime.tm_hour << endl;
cout << dbTime.tm_min << endl;
cout << dbTime.tm_sec << endl;
cout << dbTime.tm_isdst << endl;
time_t t = mktime(&dbTime); // returns -1, error code
cout << ctime(&t) << endl; // crashes
I wasn't sure where to post this. If it should be in the C subforum, please move it there.
Thanks