c++ - 来自带有时区和夏令时的字符串的 Qt QDateTime

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

我得到的输出

  • “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/

相关文章:

java - SimpleDateFormat 忽略 Android 中的 DST 偏移量

c++ - QVector(or std::vector) reserve(or resize) down 不释放内存

ruby-on-rails - Rails 4 - 如何为 strftime 设置时区?

django - 查询 django 时不是幼稚的时区

c++ - QSettings中的QStrings isNull是错误的

python - 无法在 PyQt5 中导入 QtWebKitWidgets

mysql - Qt 中未找到驱动程序错误

c++ - 像素路径性能警告 : Pixel transfer is synchronized with 3D rendering

c++ - _finddata_t、_findfirst、_findnext 适用于 Mac OS-X

c++ - 当小部件尺寸发生变化时如何获取信号