将包含 PostgreSQL 时间戳的 QString 转换为 QDateTime

发布于 2024-10-05 03:44:15 字数 351 浏览 0 评论 0原文

我遇到了一个看似非常简单的问题:我想从包含时间戳的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一抹微笑 2024-10-12 03:44:15

我犯了两个错误。格式规范中没有 TZD,因此我删除了时区信息,因为我的应用程序中不需要它,方法是:

timeStamp.chop(6);

然后使用以下格式获取 QDateTime。请注意小写格式字符:

QDateTime createdAt = QDateTime::fromString(timeStamp, "yyyy-MM-dd HH:mm:ss");

感谢上面大家的帮助。

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:

timeStamp.chop(6);

And then used the following format to get a QDateTime. Note the lowercase format characters:

QDateTime createdAt = QDateTime::fromString(timeStamp, "yyyy-MM-dd HH:mm:ss");

Thanks to everyone above for helping out.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文