From 0757c74b4823b5fe3acbea7eedaf94959d464eb4 Mon Sep 17 00:00:00 2001 From: Edward Welbourne Date: Tue, 3 May 2022 16:03:16 +0200 Subject: [PATCH] Simplify msecsToTime() and two callers by using QRoundingDown MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The formerly cumbersome arithmetic, to adjust for division rounding towards zero rather than down, is so much simpler this way. Break out the conversion for only date and for only time, for the sake of (for now) two callers that only need one; and inline QRoundingDown::qMod() to save repeating the qDiv() call. Include the mapping from millis to raw Julian Day as a separate function, as I'll be using it shortly. Change-Id: I0ee74ea68421a347ed618fa34142bd034844351e Reviewed-by: MÃ¥rten Nordheim Reviewed-by: Thiago Macieira --- src/corelib/time/qdatetime.cpp | 47 +++++++++++++++------------------- 1 file changed, 21 insertions(+), 26 deletions(-) diff --git a/src/corelib/time/qdatetime.cpp b/src/corelib/time/qdatetime.cpp index 99c747e0ed..9b3a5a1a42 100644 --- a/src/corelib/time/qdatetime.cpp +++ b/src/corelib/time/qdatetime.cpp @@ -2692,30 +2692,29 @@ static bool qt_localtime(qint64 msecsSinceEpoch, QDate *localDate, QTime *localT } } -// Converts an msecs value into a date and time +// Converts milliseconds since the start of 1970 into a date and/or time: +static qint64 msecsToJulianDay(qint64 msecs) +{ + return JULIAN_DAY_FOR_EPOCH + QRoundingDown::qDiv(msecs, MSECS_PER_DAY); +} + +static QDate msecsToDate(qint64 msecs) +{ + return QDate::fromJulianDay(msecsToJulianDay(msecs)); +} + +static QTime msecsToTime(qint64 msecs) +{ + return QTime::fromMSecsSinceStartOfDay(QRoundingDown::qMod(msecs, MSECS_PER_DAY)); +} + static void msecsToTime(qint64 msecs, QDate *date, QTime *time) { - qint64 jd = JULIAN_DAY_FOR_EPOCH; - qint64 ds = 0; - - if (msecs >= MSECS_PER_DAY || msecs <= -MSECS_PER_DAY) { - jd += msecs / MSECS_PER_DAY; - msecs %= MSECS_PER_DAY; - } - - if (msecs < 0) { - ds = MSECS_PER_DAY - msecs - 1; - jd -= ds / MSECS_PER_DAY; - ds = ds % MSECS_PER_DAY; - ds = MSECS_PER_DAY - ds - 1; - } else { - ds = msecs; - } - + qint64 days = QRoundingDown::qDiv(msecs, MSECS_PER_DAY); if (date) - *date = QDate::fromJulianDay(jd); + *date = QDate::fromJulianDay(JULIAN_DAY_FOR_EPOCH + days); if (time) - *time = QTime::fromMSecsSinceStartOfDay(ds); + *time = QTime::fromMSecsSinceStartOfDay(msecs - days * MSECS_PER_DAY); } // Converts a date/time value into msecs @@ -3790,9 +3789,7 @@ QDate QDateTime::date() const auto status = getStatus(d); if (!status.testFlag(QDateTimePrivate::ValidDate)) return QDate(); - QDate dt; - msecsToTime(getMSecs(d), &dt, nullptr); - return dt; + return msecsToDate(getMSecs(d)); } /*! @@ -3806,9 +3803,7 @@ QTime QDateTime::time() const auto status = getStatus(d); if (!status.testFlag(QDateTimePrivate::ValidTime)) return QTime(); - QTime tm; - msecsToTime(getMSecs(d), nullptr, &tm); - return tm; + return msecsToTime(getMSecs(d)); } /*!