开发者

Preferred way to handle UTC/local time in Qt's QDateTime?

开发者 https://www.devze.com 2023-01-09 08:32 出处:网络
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

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 开发者_JS百科is this the preferred way of doing this? Shouldn't toString() know to write out a Z when the timeSpec is UTC?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消