Qt QDateTime从字符串与时区和夏时制 [英] Qt QDateTime from string with timezone and daylight saving

查看:347
本文介绍了Qt QDateTime从字符串与时区和夏时制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从字符串中插入时间

i am inserting time from string

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日星期三GMT"
  • false

应该给的

  • "2019年3月26日星期二23:37:40"
  • "2019年3月27日星期三GMT"
  • true

如何通过时间戳字符串传递夏令时?

how can i pass daylight saving with the timestamp string?

推荐答案

首先,UTC时间"2019年3月27日星期三06:37:40 GMT"从"2019年3月26日星期三22:37:40 GMT"算起绝对正确-08.您如何看待5:37?

First, UTC time "Wed Mar 27 06:37:40 2019 GMT" is definitely right when calculated from "Wed Mar 26 22:37:40 2019 GMT-08". How do you think it could be 5:37?

说明GMT或UTC不包含DST的原因:

夏时制(DST)的UTC和GMT都不会更改.但是,某些使用格林尼治标准时间的国家/地区切换到了不同的时间夏令时期间的区域.例如,AKDT(阿拉斯加夏时制时间)是夏令时(夏令时)的GMT-8时区之一节省时间)在2019年3月10日至11月3日之间.冬天,正在使用GMT-9的AKST(阿拉斯加标准时间).

Neither UTC nor GMT ever change for Daylight Saving Time (DST). However, some of the countries that use GMT switch to different time zones during their DST period. For example, AKDT (Alaska Daylight Time) is one of GMT-8 time zones during their DST (summer daylight saving time) between 10 March and 3 November in 2019. During the winter, AKST (Alaska Standard Time) which is GMT-9 is in use.

第二,如其他回答时间中所述 QDateTime :: isDaylightTime 如果Qt :: TimeSpec不是Qt :: LocalTime或Qt :: TimeZone ,则始终返回false.

Second, as was already pointed in the other answer time QDateTime::isDaylightTime always returns false if the Qt::TimeSpec is not Qt::LocalTime or Qt::TimeZone.

当您将 QDateTime :: fromString 与时区信息一起使用时(如代码示例中那样),将timespec正确设置为 Qt :: OffsetFromUTC .您可以在同一时间实例化另一个QDateTime对象,但是将TimeSpec设置为Qt :: LocalTime或Qt :: TimeZone.您可以例如使用 QDateTime :: toLocalTime 转换为本地时间或转换为Qt::带有 QDateTime :: fromSecsSinceEpoch 的Qt :: TimeZone:LocalTime或Qt :: TimeZone偏移时区的秒数.

When you use QDateTime::fromString with time zone information as in your code example timespec is correctly set to Qt::OffsetFromUTC. You can instantiate another QDateTime object to the same time but having the TimeSpec as Qt::LocalTime or Qt::TimeZone. You can e.g. convert to local time with QDateTime::toLocalTime or to either Qt::LocalTime or Qt::TimeZone with QDateTime::fromSecsSinceEpoch which accepts offset seconds for time zone.

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

See example code below. I'm located in Finland where daylight savings time starts in March 31 so you can see difference of local time when standard time is in use and when daylight time is in use:

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();

输出:

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

这篇关于Qt QDateTime从字符串与时区和夏时制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆