11

I have some input containing UTC time formatted according to iso8601. I try to parse it using QDateTime:

  const char* s = "2009-11-05T03:54:00";
  d.setTimeSpec(Qt::UTC);
  d = QDateTime::fromString(s, Qt::ISODate);
  Qt::TimeSpec ts = d.timeSpec();

When this fragment ends, ts is set to localTime and d contains 3 hours 54 minutes. Does anyone know how to read the date properly?

danatel
  • 4,844
  • 11
  • 48
  • 62

1 Answers1

15

What about setting the time spec after the fromString method.

const char* s = "2009-11-05T03:54:00";
d = QDateTime::fromString(s, Qt::ISODate);
d.setTimeSpec(Qt::UTC);
Qt::TimeSpec ts = d.timeSpec();
gregseth
  • 12,952
  • 15
  • 63
  • 96
  • When you first declare `d` the default constructor is used, when you write `d = QDateTime::fromString(s, Qt::ISODate);` the current value of `d` is replaced by the return value of `fromString`. So if you set the time spec before calling `fromString` the time spec is defined for the default constructed value. – gregseth Feb 16 '10 at 20:34