Obsolete Members for QDateTime
类QDateTime 的以下成员已被弃用。提供这些成员是为了保持旧源代码的正常运行。我们强烈建议不要在新代码中使用它们。
公共函数
(deprecated in 6.9) | QDateTime(QDate date, QTime time, Qt::TimeSpec spec, int offsetSeconds = 0) |
(deprecated in 6.9) void | setOffsetFromUtc(int offsetSeconds) |
(deprecated in 6.9) void | setTimeSpec(Qt::TimeSpec spec) |
(deprecated in 6.9) QDateTime | toTimeSpec(Qt::TimeSpec spec) const |
静态公共成员
(deprecated in 6.9) QDateTime | fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetSeconds = 0) |
(deprecated in 6.9) QDateTime | fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetSeconds = 0) |
成员函数文档
[deprecated in 6.9]
QDateTime::QDateTime(QDate date, QTime time, Qt::TimeSpec spec, int offsetSeconds = 0)
自 6.9 版起,该函数已被弃用。我们强烈建议不要在新代码中使用该函数。
请使用QDateTime(date, time)
或QDateTime(date, time, QTimeZone::fromSecondsAheadOfUtc(offsetSeconds))
。
使用给定的date 和time 构建一个日期时间,使用spec 和offsetSeconds 秒隐含的时间表示法。
如果date 有效,而time 无效,则时间将设为午夜。
如果spec 不是Qt::OffsetFromUTC ,则offsetSeconds 将被忽略。如果spec 是Qt::OffsetFromUTC ,而offsetSeconds 是 0,那么timeSpec() 将被设置为Qt::UTC ,即偏移 0 秒。
如果spec 是Qt::TimeZone ,则规格将设置为Qt::LocalTime ,即当前系统时区。要创建Qt::TimeZone 日期时间,请使用正确的构造函数。
如果date 位于 QDateTime 可表示的日期范围之外,则结果无效。如果spec 是Qt::LocalTime ,而系统时区跳过了给定的日期和时间,则结果无效。
[static, deprecated in 6.9]
QDateTime QDateTime::fromMSecsSinceEpoch(qint64 msecs, Qt::TimeSpec spec, int offsetSeconds = 0)
自 6.9 版起,该函数已被弃用。我们强烈建议不要在新代码中使用该函数。
这是一个重载函数。
请使用QTimeZone 或省略spec 和offsetSeconds 。
返回一个日期时间(datetime),代表 1970 年(UTC 时)开始后msecs 毫秒数的给定时刻,如spec 和offsetSeconds 所述。
请注意,msecs 的值有可能超出QDateTime 的有效范围,包括负值和正值。对于这些值,该函数的行为是未定义的。
如果spec 不是Qt::OffsetFromUTC ,则offsetSeconds 将被忽略。如果spec 是Qt::OffsetFromUTC ,而offsetSeconds 是 0,那么Qt::UTC 将被用作spec ,因为 UTC 的偏移量为零。
如果spec 是Qt::TimeZone ,则将使用Qt::LocalTime 代替,相当于使用当前系统时区(但表示方法不同)。
另请参阅 fromSecsSinceEpoch()、toMSecsSinceEpoch() 和setMSecsSinceEpoch()。
[static, deprecated in 6.9]
QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetSeconds = 0)
自 6.9 版起,该函数已被弃用。我们强烈建议不要在新代码中使用该函数。
这是一个重载函数。
请使用QTimeZone 或省略spec 和offsetSeconds 。
返回一个日期时间(datetime),该日期时间代表 1970 年(UTC)开始后secs 的给定秒数,如spec 和offsetSeconds 所述。
请注意,secs 的值有可能超出QDateTime 的有效范围,包括负值和正值。对于这些值,该函数的行为是未定义的。
如果spec 不是Qt::OffsetFromUTC ,则offsetSeconds 将被忽略。如果spec 是Qt::OffsetFromUTC ,而offsetSeconds 是 0,那么Qt::UTC 将被用作spec ,因为 UTC 的偏移量为零。
如果spec 是Qt::TimeZone ,则将使用Qt::LocalTime 代替,相当于使用当前系统时区(但表示方法不同)。
另请参阅 fromMSecsSinceEpoch()、toSecsSinceEpoch() 和setSecsSinceEpoch()。
[deprecated in 6.9]
void QDateTime::setOffsetFromUtc(int offsetSeconds)
自 6.9 版起,该函数已被弃用。我们强烈建议不要在新代码中使用该函数。
请使用setTimeZone(QTimeZone::fromSecondsAheadOfUtc(offsetSeconds)) 代替它。
将timeSpec() 设为Qt::OffsetFromUTC ,将偏移量设为offsetSeconds 。datetime 可以指不同的时间点。
最大和最小偏移量为 14 个正或负小时。如果offsetSeconds 大于或小于该值,则结果未定义。
如果offsetSeconds 为 0,则timeSpec() 将设置为Qt::UTC 。
另请参见 setTimeZone()、isValid()、offsetFromUtc() 和toOffsetFromUtc()。
[deprecated in 6.9]
void QDateTime::setTimeSpec(Qt::TimeSpec spec)
自 6.9 版起,该函数已被弃用。我们强烈建议不要在新代码中使用该函数。
请使用setTimeZone() 代替
将此 datetime 中使用的时间规格设置为spec 。该 datetime 可以指代不同的时间点。
如果spec 是Qt::OffsetFromUTC ,那么timeSpec() 将被设置为Qt::UTC ,即有效偏移为 0。
如果spec 是Qt::TimeZone ,那么规格将设置为Qt::LocalTime ,即当前系统时区。
例如
QDateTimelocal(QDateTime::currentDateTime());qDebug() << "Local time is:" << local; QDateTimeUTC(local);UTC.setTimeSpec(Qt::UTC);qDebug() << "UTC time is:" << UTC; qDebug() << "There are" << local.secsTo(UTC) << "seconds difference between the datetimes.";
另请参见 setTimeZone(),timeSpec(),toTimeSpec(),setDate() 和setTime().
[deprecated in 6.9]
QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const
自 6.9 版起,该函数已被弃用。我们强烈建议不要在新代码中使用该函数。
请使用toTimeZone() 代替。
返回将此日期时间转换为给定时间的副本spec 。
结果代表与此日期时间相同的时间点,并且等于此日期时间。
如果spec 是Qt::OffsetFromUTC ,那么它将被设置为Qt::UTC 。要设置为与 UTC 的固定偏移量,请使用toTimeZone() 或toOffsetFromUtc()。
如果spec 是Qt::TimeZone ,则设置为Qt::LocalTime ,即本地时区。要设置指定时区,请使用toTimeZone() 。
例如
QDateTimelocal(QDateTime::currentDateTime());QDateTimeUTC(local.toTimeSpec(Qt::UTC));qDebug() << "Local time is:" << local; qDebug() << "UTC time is:" << UTC; qDebug() << "No difference between times:" << local.secsTo(UTC);
另请参见 setTimeSpec()、timeSpec() 和toTimeZone()。
© 2025 The Qt Company Ltd. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd. in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.