views:

365

answers:

2

I'm using Qt to parse an XML file which contains timestamps in UTC. Within the program, of course, I'd like them to change to local time. In the XML file, the timestamps look like this: "2009-07-30T00:32:00Z".

Unfortunately, when using the QDateTime::fromString() method, these timestamps are interpreted as being in the local timezone. The hacky way to solve this is to add or subtract the correct timezone offset from this time to convert it to "true" local time. However, is there any way to make Qt realize that I am importing a UTC timestamp and then automatically convert it to local time?

+1  A: 

try using the setTime_t function.

Marius
Here is the Qt4 version; http://doc.trolltech.com/4.5/qdatetime.html#setTime_t Upmodding parent for the good answer btw!
cartman
Sorry, did a google search and that is the one I found. Didn't check version. Fixed in edit
Marius
+5  A: 

Do it like this:

QDateTime timestamp = QDateTime::fromString(thestring);
timestamp.setTimeSpec(Qt::UTC); // mark the timestamp as UTC (but don't convert it)
timestamp = timestamp.toLocalTime() // convert to local time
Intransigent Parsnip
Exactly what I was looking for. I actually got very close, I used the Qt::LocalTime time spec and expected it to convert.
spectre256