将包含 PostgreSQL 时间戳的 QString 转换为 QDateTime
我遇到了一个看似非常简单的问题:我想从包含时间戳的 QString 获取 QDateTime。我从 PostgreSQL 获得了时间戳,但这并不重要。这是不起作用的代码:
QString timestamp = "2010-10-09 19:21:46+02:00";
QString format = "YYYY-MM-DD HH:MM:SSTZD";
QDateTime dt = QDateTime::fromString(timestamp, format);
qDebug() << dt.toString(); // outputs empty string
我一定缺少一些非常明显的东西。谢谢!
I'm having trouble with a seemingly very simple problem: I want to get a QDateTime from a QString containing a timestamp. I got the timestamp from PostgreSQL, but it doesn't matter. Here is the code that does not work:
QString timestamp = "2010-10-09 19:21:46+02:00";
QString format = "YYYY-MM-DD HH:MM:SSTZD";
QDateTime dt = QDateTime::fromString(timestamp, format);
qDebug() << dt.toString(); // outputs empty string
There must be something very obvious I'm missing. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我犯了两个错误。格式规范中没有 TZD,因此我删除了时区信息,因为我的应用程序中不需要它,方法是:
然后使用以下格式获取 QDateTime。请注意小写格式字符:
感谢上面大家的帮助。
There were two mistakes I was making. There is no TZD in the format specifications, so I removed the time zone information since I do not need it in my app by doing:
And then used the following format to get a QDateTime. Note the lowercase format characters:
Thanks to everyone above for helping out.