From 612c345108dca3e6c67e75bcf08912641f3f6f13 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 23 Aug 2022 19:11:09 +0200 Subject: [PATCH] Reverse toLocalTime()'s and toUTC()'s delegation to toTimeSpec() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Qt CI Bot Reviewed-by: Thiago Macieira --- src/corelib/time/qdatetime.cpp | 52 +++++++++++++++++++++++----------- src/corelib/time/qdatetime.h | 4 +-- 2 files changed, 37 insertions(+), 19 deletions(-) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index f96aa5d3fd..611eb6bd8c 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -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 diff --git a/src/corelib/time/qdatetime.h b/src/corelib/time/qdatetime.h index 58dff850db..a2f6198d08 100644 --- a/src/corelib/time/qdatetime.h +++ b/src/corelib/time/qdatetime.h @@ -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;