views:

330

answers:

1

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?

+2  A: 

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
Thank you. I cannot wrap my head around this but it works!
danatel
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