views:

62

answers:

1

I have some code where I write a QDateTime to a file...

someQDateTime.toUTC().toString(Qt::ISODate)

and when I read it back using QDateTime::fromString(), I get the time interpreted as being in the system's time zone. I can manually append "Z" to the string when I write it out, or use setTimeSpec() after I read it, and then everything is fine, but is this the preferred way of doing this? Shouldn't toString() know to write out a Z when the timeSpec is UTC?

+2  A: 

Well, at least according to ISO 8601 (section 4.2.4, pdf here), a Z is needed to differentiate between UTC and local time. Seems as if QDateTime::toString() doesn't follow this advice, while QDateTime::fromString() knows about it. ISO 8601 also contains this note in section 4.3.2 (where [T] is the time zone indicator, i.e. Z):

"By mutual agreement of the partners in information interchange, the character [T] may be omitted in applications where there is no risk of confusing a date and time of day representation with others defined in this International Standard."

You could always file a bug report (http://bugreports.qt.nokia.com/) to tell the Qt people about this small inconsistency and see what they have to say about it.

Greg S
It doesn't look like it's a bug; looks like it's by design. Apparently they want to keep that information in `QDateTimePrivate`. I'll have to ask them why. :P
Jake Petroules
Something else to add - apparently .NET's `DateTime` doesn't print out a Z either for UTC times. Interesting.
Jake Petroules