Use the QTime API less clumsily

Various patterns seem to have been copied, notably counting time from
QTime(0, 0) rather than using QTime::msecsSinceStartOfDay() and its
setter.  Unsuitable value types also put in an appearance, and
QTime()'s parameters after the first two default to 0 anyway.

Corrected a lie in QTime()'s default constructor doc; it does not work
the same as QTime(0, 0) at all.

Change-Id: Icf1a10052a049e68fd0f665958f36dbe75ac46d5
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Edward Welbourne 2019-03-26 19:20:24 +01:00
parent ad313595e0
commit f3002b6e20
4 changed files with 15 additions and 15 deletions

View File

@ -1464,9 +1464,8 @@ bool QDate::isLeapYear(int y)
/*!
\fn QTime::QTime()
Constructs a null time object. A null time can be a QTime(0, 0, 0, 0)
(i.e., midnight) object, except that isNull() returns \c true and isValid()
returns \c false.
Constructs a null time object. For a null time, isNull() returns \c true and
isValid() returns \c false. If you need a zero time, use QTime(0, 0).
\sa isNull(), isValid()
*/
@ -3143,7 +3142,7 @@ QDateTime::QDateTime() Q_DECL_NOEXCEPT_EXPR(Data::CanBeSmall)
*/
QDateTime::QDateTime(const QDate &date)
: d(QDateTimePrivate::create(date, QTime(0, 0, 0), Qt::LocalTime, 0))
: d(QDateTimePrivate::create(date, QTime(0, 0), Qt::LocalTime, 0))
{
}

View File

@ -2030,8 +2030,8 @@ QVariant operator+(const QVariant &arg1, const QVariant &arg2)
#if QT_CONFIG(datetimeparser)
case QVariant::DateTime: {
QDateTime a2 = arg2.toDateTime();
QDateTime a1 = arg1.toDateTime().addDays(QDATETIMEEDIT_DATETIME_MIN.daysTo(a2));
a1.setTime(a1.time().addMSecs(QTime().msecsTo(a2.time())));
QDateTime a1 = arg1.toDateTime().addDays(QDATETIMEEDIT_DATE_MIN.daysTo(a2.date()));
a1.setTime(a1.time().addMSecs(a2.time().msecsSinceStartOfDay()));
ret = QVariant(a1);
break;
}
@ -2093,11 +2093,11 @@ QVariant operator*(const QVariant &arg1, double multiplier)
#if QT_CONFIG(datetimeparser)
case QVariant::DateTime: {
double days = QDATETIMEEDIT_DATE_MIN.daysTo(arg1.toDateTime().date()) * multiplier;
int daysInt = (int)days;
const qint64 daysInt = qint64(days);
days -= daysInt;
long msecs = (long)((QDATETIMEEDIT_TIME_MIN.msecsTo(arg1.toDateTime().time()) * multiplier)
+ (days * (24 * 3600 * 1000)));
ret = QDateTime(QDate().addDays(int(days)), QTime().addMSecs(msecs));
qint64 msecs = qint64(arg1.toDateTime().time().msecsSinceStartOfDay() * multiplier
+ days * (24 * 3600 * 1000));
ret = QDateTime(QDATETIMEEDIT_DATE_MIN.addDays(daysInt), QTime::fromMSecsSinceStartOfDay(msecs));
break;
}
#endif // datetimeparser
@ -2127,8 +2127,8 @@ double operator/(const QVariant &arg1, const QVariant &arg2)
case QVariant::DateTime:
a1 = QDATETIMEEDIT_DATE_MIN.daysTo(arg1.toDate());
a2 = QDATETIMEEDIT_DATE_MIN.daysTo(arg2.toDate());
a1 += (double)QDATETIMEEDIT_TIME_MIN.msecsTo(arg1.toDateTime().time()) / (long)(3600 * 24 * 1000);
a2 += (double)QDATETIMEEDIT_TIME_MIN.msecsTo(arg2.toDateTime().time()) / (long)(3600 * 24 * 1000);
a1 += arg1.toDateTime().time().msecsSinceStartOfDay() / (36e5 * 24);
a2 += arg2.toDateTime().time().msecsSinceStartOfDay() / (36e5 * 24);
break;
#endif // datetimeparser
default: break;

View File

@ -95,8 +95,9 @@ void tst_QTime::addSecs_data()
QTest::newRow("Data0") << QTime(0,0,0) << 200 << QTime(0,3,20);
QTest::newRow("Data1") << QTime(0,0,0) << 20 << QTime(0,0,20);
QTest::newRow("overflow") << QTime(0,0,0) << (INT_MAX / 1000 + 1)
<< QTime(0,0,0).addSecs((INT_MAX / 1000 + 1) % 86400);
QTest::newRow("overflow")
<< QTime(0,0,0) << (INT_MAX / 1000 + 1)
<< QTime::fromMSecsSinceStartOfDay(((INT_MAX / 1000 + 1) % 86400) * 1000);
}
void tst_QTime::addSecs()

View File

@ -123,7 +123,7 @@ class tst_QNetworkReply: public QObject
if (!seedCreated) {
seedCreated = true; // not thread-safe, but who cares
}
return QString::number(QTime(0, 0, 0).msecsTo(QTime::currentTime()))
return QString::number(QTime::currentTime().msecsSinceStartOfDay())
+ QLatin1Char('-') + QString::number(QCoreApplication::applicationPid())
+ QLatin1Char('-') + QString::number(QRandomGenerator::global()->generate());
}