我正在从字符串中插入时间
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”
- 错误
应该给
- “2019 年 3 月 26 日星期二 23:37:40”
- “格林威治标准时间 2019 年 3 月 27 日星期三 05:37:40”
- 正确
如何使用时间戳字符串传递夏令时?
最佳答案
如果你查看官方文档,它说:
If the Qt::TimeSpec is not Qt::LocalTime or Qt::TimeZone then will always return false.
所以首先,检查 QDateTime::timeSpec
正在返回您所期望的。
如果您事先知道格式,请尝试使用等效函数QDateTime::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();
关于c++ - 来自带有时区和夏令时的字符串的 Qt QDateTime,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55370667/