From c4a0630e4580c0d246cfb5786b80e09f3a3abf2e Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Fri, 25 Nov 2022 16:01:33 +0100 Subject: [PATCH] QDateTime::to{LocalTime,UTC}(): relocate to among related code These two functions were a long way from their closest relatives. Relocate to make it easier to compare and contrast within the family. Change-Id: I30062c16b318f222b75b527c6af5b443770fe36a Reviewed-by: Thiago Macieira --- src/corelib/time/qdatetime.cpp | 99 +++++++++++++++++----------------- 1 file changed, 48 insertions(+), 51 deletions(-) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 3bac9fe504..f46f789141 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -4454,6 +4454,54 @@ QDateTime QDateTime::toOffsetFromUtc(int offsetSeconds) const return fromMSecsSinceEpoch(toMSecsSinceEpoch(), Qt::OffsetFromUTC, offsetSeconds); } +/*! + Returns a datetime containing the date and time information in + this datetime, but specified using the Qt::LocalTime definition. + + Example: + + \snippet code/src_corelib_time_qdatetime.cpp 17 + + \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()); +} + +/*! + Returns a datetime containing the date and time information in + this datetime, but specified using the Qt::UTC definition. + + Example: + + \snippet code/src_corelib_time_qdatetime.cpp 18 + + \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); +} + #if QT_CONFIG(timezone) /*! \since 5.2 @@ -5179,57 +5227,6 @@ QDateTime QDateTime::fromString(const QString &string, QStringView format, QCale } #endif // datestring -/*! - \fn QDateTime QDateTime::toLocalTime() const - - Returns a datetime containing the date and time information in - this datetime, but specified using the Qt::LocalTime definition. - - Example: - - \snippet code/src_corelib_time_qdatetime.cpp 17 - - \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 - - Returns a datetime containing the date and time information in - this datetime, but specified using the Qt::UTC definition. - - Example: - - \snippet code/src_corelib_time_qdatetime.cpp 18 - - \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