Frage

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?

War es hilfreich?

Lösung

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 (https://bugreports.qt.io/) to tell the Qt people about this small inconsistency and see what they have to say about it.

Andere Tipps

Qt bug 9698 is about the behavior of QDateTime::toString(Qt::ISODate). The omission of the time zone designator is reported there. Feel free to vote for the bug.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top