I'm trying to convert a time stamp in string format to msecs since epoch, like so:
QString startTime("20131030 21923"); //2013-10-30 02:19:23
auto date = QDateTime::fromString(startTime, "yyyyMMdd hmmss");
auto secsSinceEpoch = date.toTime_t();
The result (secsSinceEpoch) is 1383163763, which converts to 2013-10-30 21:09:23. So it seems like my format string is interpreted incorrectly (as "yyyyMMdd hhmss"), why is that so and what can I do to solve this?
see the caveat here: http://qt-project.org/doc/qt-5.0/qtcore/qdate.html#fromString-2
The expressions that don't expect leading zeroes (d, M) will be greedy. This means that they will use two digits even if this will put them outside the accepted range of values and leaves too few digits for other sections. For example, the following format string could have meant January 30 but the M will grab two digits, resulting in an invalid date:
so your best be it to split them up and parse separately
edit: for example you can get the date and time with
QDate date = QDate::fromString(startTime.left(8), "yyyMMdd");
startTime[8]=0;//replacing the " " with a 0
QTime time = QTime::fromString(startTime.right(6), "hhmmss");//hour becomes a 2 digit
QDateTime fullDate(date,time);
auto secsSinceEpoch = fullDate.toTime_t();