PDA

View Full Version : QDateTime not working



kgdauaa88
14th October 2013, 02:24
So, I have a csv file where every row contains a timestamp (UTC) and a sensor reading. I have been trying to leverage the Qt libraries to aid me however, I have been having a lot of trouble. The following is my code



// Read files until one of the files ends
for (int i=0; i<noOfFiles; i++)
{
QStringList rowVec;
QString value,timestamp,row;
QDateTime timestampH;
while (!(*finVec[i]).eof())
{
string str;
getline(*(finVec[i]),str);
row = QString::fromLocal8Bit(str.c_str());
rowVec = row.split(",");
timestamp = rowVec.at(0);
value = rowVec.at(1);

timestampH = QDateTime::fromString(timestamp,"yyyy-MM-dd HH:mm:ss");
timestampH.setTimeSpec(Qt::UTC);

cout << timestampH.isNull() << endl;
}
}


The result of the code is always such that timestampH is NULL! I am 100% sure that the data in my csv file is the same exact format as "yyyy-MM-dd HH:mm:ss". In fact if I copy and paste a single timestamp into my code replacing the variable (QString timestamp) i dont get any problems. Can someone please help me on this I have tried everything the past 2 days and got no where

ChrisW67
14th October 2013, 06:44
Your input is not what you think it is.


#include <QCoreApplication>
#include <QDateTime>
#include <QStringList>
#include <QDebug>

int main(int argc, char **argv) {
QCoreApplication app(argc, argv);

// Mimics your reading via std::string
std::string str("2013-10-14 23:59:59,some,other,stuff");
QString row = QString::fromLocal8Bit(str.c_str());
QStringList rowVec = row.split(",");
QString timestamp = rowVec.at(0);
QDateTime timestampH = QDateTime::fromString(timestamp,"yyyy-MM-dd HH:mm:ss");
timestampH.setTimeSpec(Qt::UTC);
qDebug() << timestampH << timestampH.toString(Qt::ISODate) << timestampH.isNull();

return 0;
}

Output:


QDateTime("Mon Oct 14 23:59:59 2013") "2013-10-14T23:59:59Z" false