1

I'm having trouble with a seemingly very simple problem: I want to get a QDateTime from a QString containing a timestamp. I got the timestamp from PostgreSQL, but it doesn't matter. Here is the code that does not work:

QString timestamp = "2010-10-09 19:21:46+02:00";
QString format = "YYYY-MM-DD HH:MM:SSTZD";
QDateTime dt = QDateTime::fromString(timestamp, format);
qDebug() << dt.toString(); // outputs empty string

There must be something very obvious I'm missing. Thanks!

Joseph S.
  • 475
  • 1
  • 6
  • 11
  • Where did you find the "TZD" format information in the fromString, it does not exist. Nothing actually says that QDateTime can parse the +2:00 part of your time string – Harald Scheirich Dec 02 '10 at 01:56
  • "YYYY" must be "yyyy", "DD" -> "dd", "MM:SS" -> "mm:ss". – Frank Osterfeld Dec 02 '10 at 12:31
  • I got the TZD information from [here](http://doc.trolltech.com/latest/qt.html#DateFormat-enum), but I obviously misread it because the format characters are specified in the [QDateTime::fromString()](http://doc.trolltech.com/latest/qdatetime.html#fromString-2) documentation. Your two comments combined led me to the answer, so if you add an answer below, I'll accept it. – Joseph S. Dec 05 '10 at 16:32

1 Answers1

1

There were two mistakes I was making. There is no TZD in the format specifications, so I removed the time zone information since I do not need it in my app by doing:

timeStamp.chop(6);

And then used the following format to get a QDateTime. Note the lowercase format characters:

QDateTime createdAt = QDateTime::fromString(timeStamp, "yyyy-MM-dd HH:mm:ss");

Thanks to everyone above for helping out.

Joseph S.
  • 475
  • 1
  • 6
  • 11