vendredi 23 juin 2017

Getting one unit less when using std::stoi C++11

My problem seems quite simple. A have a string filename, eg. '20170425-121017_M-A351_000000', and I am using the following to convert date and hour to a tm struct:

struct tm time_struct;

time_struct.tm_year = stoi(filename.substr(0, 4)) - 1900;  // year
time_struct.tm_mon = stoi(filename.substr(4, 2));  // month
time_struct.tm_mday = stoi(filename.substr(6, 2));  // day
time_struct.tm_hour = stoi(filename.substr(9, 2));  // hour
//time_struct.tm_hour = stoi(filename.substr(9, 2));  // hour -- IDK WHY IT ONLY WORKS DUPLICATED
time_struct.tm_min = stoi(filename.substr(11, 2));  // minute
time_struct.tm_sec = stoi(filename.substr(13, 2));  // second

time_t end_time = mktime(&time_struct);

I don't know why but the tm_hour member is being saved with one unit less. It is 11 when it should be 12, and the same happens when I change 12 for any other integer. All other entries are ok. But when I duplicate the assignment declaration, it works fine.

I don't want to use this solution cause it doesn't seem reliable. Any ideas why this could be happening?

I'm using Dec-C++ 5.11 with TDM-GCC 4.9.2 64-bit, adding "-std=c++11" when calling compiler.

Thank you in advance!

Aucun commentaire:

Enregistrer un commentaire