C++ Qt QDateTime来自带时区和夏令时的字符串

C++ Qt QDateTime来自带时区和夏令时的字符串,c++,qt,timezone,dst,C++,Qt,Timezone,Dst,我正在从字符串插入时间 QDateTime time =QDateTime::fromString("Wed Mar 26 22:37:40 2019 GMT-08"); qDebug()<<time.toLocalTime().toString(); qDebug()<<time.toUTC().toString(); qDebug()<<time.isDaylightTime(); QDateTime time=QDateTime::fromString

我正在从字符串插入时间

QDateTime time =QDateTime::fromString("Wed Mar 26 22:37:40 2019 GMT-08");
qDebug()<<time.toLocalTime().toString();
qDebug()<<time.toUTC().toString();
qDebug()<<time.isDaylightTime();

QDateTime time=QDateTime::fromString(“Wed Mar 26 22:37:40 2019 GMT-08”);

qDebug()如果你查看官方文件,它会说:

如果Qt::TimeSpec不是Qt::LocalTime或Qt::TimeZone,则将始终返回false

因此,首先,检查是否返回了您期望的内容

如果您事先知道格式,请尝试使用等效函数指定要解析的字符串的格式

结合这两件事,您可以执行以下操作:

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();
const char*s=“2009-11-05T03:54:00”; QDateTime d=QDateTime::fromString(s,Qt::ISODate).toLocalTime(); d、 setTimeSpec(Qt::LocalTime);//只是为了确保时间规范是正确的,我认为不需要
qDebug()首先,UTC时间“Wed Mar 27 06:37:40 2019 GMT”在从“Wed Mar 26 22:37:40 2019 GMT-08”计算时绝对正确。你觉得5点37分怎么样

解释为什么GMT或UTC不包括DST:

UTC和GMT都不会因夏令时(DST)而改变。 然而,一些使用格林尼治标准时间的国家会切换到不同的时间 DST期间的分区。例如,AKDT(阿拉斯加日光) 时间)是其DST(夏季日光)期间GMT-8时区之一 节省时间)在2019年3月10日至11月3日之间。在 冬季,使用的是AKST(阿拉斯加标准时间),即GMT-9

其次,正如在另一个回答中已经指出的,如果Qt::TimeSpec不是Qt::LocalTime或Qt::TimeZone,则time
始终返回false

当您在代码示例中将
QDateTime::fromString
与时区信息一起使用时,timespec正确设置为
Qt::offsetfromtc
。您可以将另一个QDateTime对象实例化为同一时间,但将TimeSpec设置为Qt::LocalTime或Qt::TimeZone。例如,您可以使用Qt::LocalTime或Qt::TimeZone转换为本地时间,使用Qt::LocalTime或Qt::TimeZone接受时区的偏移秒数

请参见下面的示例代码。我位于芬兰,夏时制从3月31日开始,所以当使用标准时间和夏时制时,您可以看到当地时间的差异:

QDateTime time = QDateTime::fromString("Wed Mar 26 22:37:40 2019 GMT-08");

qDebug()<<"\nLocal time EET:";
QDateTime localTime = time.toLocalTime();
// This works too, here to local time:
//QDateTime localTime = QDateTime::fromSecsSinceEpoch(time.toSecsSinceEpoch());
qDebug()<<localTime.timeSpec();
qDebug()<<localTime.timeZone();
qDebug()<<localTime.timeZoneAbbreviation();
qDebug()<<localTime.toLocalTime().toString();
qDebug()<<localTime.toUTC().toString();
qDebug()<<localTime.isDaylightTime();

time = QDateTime::fromString("Wed Apr 26 22:37:40 2019 GMT-08");

qDebug()<<"\nLocal time EEST:";
localTime = time.toLocalTime();
qDebug()<<localTime.timeSpec();
qDebug()<<localTime.timeZone();
qDebug()<<localTime.timeZoneAbbreviation();
qDebug()<<localTime.toLocalTime().toString();
qDebug()<<localTime.toUTC().toString();
qDebug()<<localTime.isDaylightTime();

“Wed Mar 26 22:37:40 2019 GMT-08”包括夏令时,我也在谷歌上查看了它显示的5:37,我更新了我的答案,解释了GMT不包括夏令时。我以目前在格林尼治标准时间8点的阿拉斯加为例。希望它能澄清。
Local time EET:
Qt::LocalTime
QTimeZone("Europe/Helsinki")
"EET"
"Wed Mar 27 08:37:40 2019"
"Wed Mar 27 06:37:40 2019 GMT"
false

Local time EEST:
Qt::LocalTime
QTimeZone("Europe/Helsinki")
"EEST"
"Sat Apr 27 09:37:40 2019"
"Sat Apr 27 06:37:40 2019 GMT"
true