Results 1 to 2 of 2

Thread: QDateTime not working

  1. #1

    Default QDateTime not working

    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
    Qt Code:
    1. // Read files until one of the files ends
    2. for (int i=0; i<noOfFiles; i++)
    3. {
    4. QStringList rowVec;
    5. QString value,timestamp,row;
    6. QDateTime timestampH;
    7. while (!(*finVec[i]).eof())
    8. {
    9. string str;
    10. getline(*(finVec[i]),str);
    11. row = QString::fromLocal8Bit(str.c_str());
    12. rowVec = row.split(",");
    13. timestamp = rowVec.at(0);
    14. value = rowVec.at(1);
    15.  
    16. timestampH = QDateTime::fromString(timestamp,"yyyy-MM-dd HH:mm:ss");
    17. timestampH.setTimeSpec(Qt::UTC);
    18.  
    19. cout << timestampH.isNull() << endl;
    20. }
    21. }
    To copy to clipboard, switch view to plain text mode 

    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

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: QDateTime not working

    Your input is not what you think it is.
    Qt Code:
    1. #include <QCoreApplication>
    2. #include <QDateTime>
    3. #include <QStringList>
    4. #include <QDebug>
    5.  
    6. int main(int argc, char **argv) {
    7. QCoreApplication app(argc, argv);
    8.  
    9. // Mimics your reading via std::string
    10. std::string str("2013-10-14 23:59:59,some,other,stuff");
    11. QString row = QString::fromLocal8Bit(str.c_str());
    12. QStringList rowVec = row.split(",");
    13. QString timestamp = rowVec.at(0);
    14. QDateTime timestampH = QDateTime::fromString(timestamp,"yyyy-MM-dd HH:mm:ss");
    15. timestampH.setTimeSpec(Qt::UTC);
    16. qDebug() << timestampH << timestampH.toString(Qt::ISODate) << timestampH.isNull();
    17.  
    18. return 0;
    19. }
    To copy to clipboard, switch view to plain text mode 
    Output:
    Qt Code:
    1. QDateTime("Mon Oct 14 23:59:59 2013") "2013-10-14T23:59:59Z" false
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. QDateTime::currentMSecsSinceEpoch ()
    By fantom in forum Newbie
    Replies: 2
    Last Post: 16th May 2012, 00:18
  2. invalidate QDateTime
    By Markus_AC in forum Qt Programming
    Replies: 2
    Last Post: 7th September 2011, 12:14
  3. Milliseconds from QDateTime
    By scamE in forum Qt Programming
    Replies: 3
    Last Post: 12th September 2010, 20:46
  4. QDateTime fromString
    By mklieber in forum Qt Programming
    Replies: 1
    Last Post: 12th March 2008, 22:15
  5. QDateTime without Day
    By raphaelf in forum Qt Programming
    Replies: 4
    Last Post: 16th February 2006, 12:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.