Reverse toLocalTime()'s and toUTC()'s delegation to toTimeSpec()

The implementations of toLocalTime() and toUTC() get marginally more
efficient and toTimeSpec() now manifestly does what its documentation
declares it does. Previously, passing TimeZone would have produced a
warning about passing it to toTimeSpec(), which was less than helpful;
it now gives a more appropriate warning.

Rationalize the \sa lines between these functions and their close
relatives in the process.

Change-Id: Ie94c63cbea8ef3d1d14c2f1febdc10f0e53023c0
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2022-08-23 19:11:09 +02:00
parent 47bafe6253
commit 612c345108
2 changed files with 37 additions and 19 deletions

View File

@ -4386,21 +4386,14 @@ qint64 QDateTime::msecsTo(const QDateTime &other) const
Example:
\snippet code/src_corelib_time_qdatetime.cpp 16
\sa timeSpec(), toTimeZone(), toOffsetFromUtc()
\sa timeSpec(), toUTC(), toLocalTime(), toOffsetFromUtc(), toTimeZone()
*/
QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const
{
if (getSpec(d) == spec && (spec == Qt::UTC || spec == Qt::LocalTime))
return *this;
if (!isValid()) {
QDateTime ret = *this;
ret.setTimeSpec(spec);
return ret;
}
return fromMSecsSinceEpoch(toMSecsSinceEpoch(), spec, 0);
if (spec == Qt::TimeZone) // No zone supplied, so treating as LocalTime.
qWarning("Use toTimeZone() rather than toTimeSpec(Qt::TimeZone)");
return spec == Qt::UTC || spec == Qt::OffsetFromUTC ? toUTC() : toLocalTime();
}
/*!
@ -4413,13 +4406,12 @@ QDateTime QDateTime::toTimeSpec(Qt::TimeSpec spec) const
If the \a offsetSeconds equals 0 then a UTC datetime will be returned
\sa setOffsetFromUtc(), offsetFromUtc(), toTimeSpec()
\sa setOffsetFromUtc(), offsetFromUtc(), toUTC(), toLocalTime(), toTimeZone(), toTimeSpec()
*/
QDateTime QDateTime::toOffsetFromUtc(int offsetSeconds) const
{
if (getSpec(d) == Qt::OffsetFromUTC
&& d->m_offsetFromUtc == offsetSeconds)
if (getSpec(d) == Qt::OffsetFromUTC && d->m_offsetFromUtc == offsetSeconds)
return *this;
if (!isValid()) {
@ -4437,7 +4429,7 @@ QDateTime QDateTime::toOffsetFromUtc(int offsetSeconds) const
Returns a copy of this datetime converted to the given \a timeZone
\sa timeZone(), toTimeSpec()
\sa timeZone(), toUTC(), toLocalTime(), toOffsetFromUtc(), toTimeSpec()
*/
QDateTime QDateTime::toTimeZone(const QTimeZone &timeZone) const
@ -5138,8 +5130,21 @@ QDateTime QDateTime::fromString(const QString &string, QStringView format, QCale
\snippet code/src_corelib_time_qdatetime.cpp 17
\sa toTimeSpec()
\sa toUTC(), toOffsetFromUtc(), toTimeZone(), toTimeSpec()
*/
QDateTime QDateTime::toLocalTime() const
{
if (getSpec(d) == Qt::LocalTime)
return *this;
if (!isValid()) {
QDateTime ret = *this;
ret.setTimeSpec(Qt::LocalTime);
return ret;
}
return fromMSecsSinceEpoch(toMSecsSinceEpoch());
}
/*!
\fn QDateTime QDateTime::toUTC() const
@ -5151,8 +5156,21 @@ QDateTime QDateTime::fromString(const QString &string, QStringView format, QCale
\snippet code/src_corelib_time_qdatetime.cpp 18
\sa toTimeSpec()
\sa toLocalTime(), toOffsetFromUtc(), toTimeZone(), toTimeSpec()
*/
QDateTime QDateTime::toUTC() const
{
if (getSpec(d) == Qt::UTC)
return *this;
if (!isValid()) {
QDateTime ret = *this;
ret.setTimeSpec(Qt::UTC);
return ret;
}
return fromMSecsSinceEpoch(toMSecsSinceEpoch(), Qt::UTC);
}
/*****************************************************************************
Date/time stream functions

View File

@ -355,8 +355,8 @@ public:
}
QDateTime toTimeSpec(Qt::TimeSpec spec) const;
inline QDateTime toLocalTime() const { return toTimeSpec(Qt::LocalTime); }
inline QDateTime toUTC() const { return toTimeSpec(Qt::UTC); }
QDateTime toLocalTime() const;
QDateTime toUTC() const;
QDateTime toOffsetFromUtc(int offsetSeconds) const;
#if QT_CONFIG(timezone)
QDateTime toTimeZone(const QTimeZone &toZone) const;