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 秒所隐含的时间表示法,构建一个 datetime 对象。
如果date 有效而time 无效,则时间将设为午夜。
如果spec 不是Qt::OffsetFromUTC ,则offsetSeconds 将被忽略。如果spec 是Qt::OffsetFromUTC 且offsetSeconds 为 0,则timeSpec() 将被设置为Qt::UTC ,即偏移量为 0 秒。
如果spec 为Qt::TimeZone ,则 spec 将被设置为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 。
返回一个表示从 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 代替,这相当于使用当前系统时区(但表示方式不同)。
此函数重载了QDateTime::fromMSecsSinceEpoch()。
另请参阅 fromSecsSinceEpoch()、toMSecsSinceEpoch() 和setMSecsSinceEpoch()。
[static, deprecated in 6.9] QDateTime QDateTime::fromSecsSinceEpoch(qint64 secs, Qt::TimeSpec spec, int offsetSeconds = 0)
自 6.9 版本起,此函数已弃用。我们强烈建议不要在新代码中使用它。
请改用QTimeZone 参数,或省略spec 和offsetSeconds 。
返回一个表示从 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 代替,这相当于使用当前系统时区(但表示方式不同)。
此函数重载了QDateTime::fromSecsSinceEpoch 。
另请参阅 fromMSecsSinceEpoch()、toSecsSinceEpoch() 和setSecsSinceEpoch()。
[deprecated in 6.9] void QDateTime::setOffsetFromUtc(int offsetSeconds)
该函数自 6.9 版起已弃用。我们强烈建议不要在新代码中使用它。
请改用setTimeZone(QTimeZone::fromSecondsAheadOfUtc(offsetSeconds))。
将 `timeSpec()` 设置为 `Qt::OffsetFromUTC `,并将偏移量设置为 `offsetSeconds`。该日期时间可能指代另一个时间点。
偏移量的最大值和最小值均为正负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 将设置为 ,即当前系统时区。
示例:
QDateTime local(QDateTime::currentDateTime());
qDebug() << "Local time is:" << local;
QDateTime UTC(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()。
示例:
QDateTime local(QDateTime::currentDateTime());
QDateTime UTC(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()。
© 2026 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.