在 Qt 的 QDateTime 中处理 UTC/本地时间的首选方法?
我有一些代码将 QDateTime 写入文件......
someQDateTime.toUTC().toString(Qt::ISODate)
当我使用 QDateTime::fromString() 读回它时,我得到的时间被解释为系统时区。我可以在写出字符串时手动将“Z”附加到字符串中,或者在读取后使用 setTimeSpec() ,然后一切都很好,但这是首选吗? > 这样做的方法?当 timeSpec 为 UTC 时,toString() 不应该知道写出 Z 吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,至少根据 ISO 8601(第 4.2.4 节,pdf 此处),需要使用
Z
来区分 UTC 和本地时间。似乎QDateTime::toString()
没有遵循这个建议,而QDateTime::fromString()
知道它。 ISO 8601 在第 4.3.2 节中也包含此注释(其中 [T] 是时区指示符,即 Z):“经信息交换合作伙伴双方同意,字符 [T] 可以在
不存在将日期和时间表示与本定义中定义的其他表示混淆的风险的应用程序
国际标准。”
您随时可以提交错误报告 (https://bugreports.qt.io/)告诉 Qt 人员这个小小的不一致,看看他们对此有何评论。
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 ifQDateTime::toString()
doesn't follow this advice, whileQDateTime::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 是关于
QDateTime::toString(Qt:: ISODate)
。那里报告了时区指示符的遗漏。请随意为该错误投票。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.