我正在从字符串插入时间
QDateTime time =QDateTime::fromString("Wed Mar 26 22:37:40 2019 GMT-08");
qDebug()<<time.toLocalTime().toString();
qDebug()<<time.toUTC().toString();
qDebug()<<time.isDaylightTime();
我得到的输出为
- “2019 年 3 月 26 日星期二 23:37:40”
- “2019 年 3 月 27 日星期三 06:37:40 GMT”
- false
它应该给
- “2019 年 3 月 26 日星期二 23:37:40”
- “2019 年 3 月 27 日星期三 05:37:40 GMT”
- true
我如何使用时间戳字符串传递夏令时?
如果你看一下官方文档,它说:
如果 Qt::TimeSpec 不是 Qt::LocalTime 或 Qt::TimeZone 那么将始终返回 false。
所以首先,检查QDateTime::timeSpec https://doc.qt.io/archives/qt-5.5/qdatetime.html#timeSpec正在返回您期望的内容。
如果您事先知道格式,请尝试使用等效函数指定要解析的字符串的格式QDateTime::fromString https://doc.qt.io/archives/qt-5.5/qdatetime.html#fromString.
结合这两件事你可以做这样的事情:
const char* s = "2009-11-05T03:54:00";
QDateTime d = QDateTime::fromString(s, Qt::ISODate).toLocalTime();
d.setTimeSpec(Qt::LocalTime); // Just to ensure that the time spec are the proper one, i think is not needed
qDebug() << d.isDaylightTime();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)