3

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?

Jake Petroules
  • 23,472
  • 35
  • 144
  • 225

2 Answers2

3

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.

MBach
  • 1,647
  • 16
  • 30
Greg S
  • 12,333
  • 2
  • 41
  • 48
  • 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 Jul 28 '10 at 06:35
  • Something else to add - apparently .NET's `DateTime` doesn't print out a Z either for UTC times. Interesting. – Jake Petroules Jul 29 '10 at 22:57
0

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.

drescherjm
  • 10,365
  • 5
  • 44
  • 64
Mihai Capotă
  • 2,271
  • 2
  • 32
  • 24