QTime - Add public api for get/set msecs since start of day
Add new public api to get and set the number of msecs since the start of the day. Modify QDateTime to use the new msecs api. [ChangeLog][QtCore][QTime] Added new methods fromMSecsSinceStartOfDay() to create a new QTime from an msecs value, and msecsSinceStartOfDay() to return the QTime as the number of msecs since the start of the day. Change-Id: I285b725b883f1f5524fda87ca81bd64ed99fe6f4 Reviewed-by: Lars Knoll <lars.knoll@digia.com> Reviewed-by: Mitch Curtis <mitch.curtis@digia.com>
This commit is contained in:
parent
63a382a930
commit
92b0a7fa3e
@ -1863,6 +1863,25 @@ int QTime::msecsTo(const QTime &t) const
|
||||
otherwise returns false.
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn QTime QTime::fromMSecsSinceStartOfDay(int msecs)
|
||||
|
||||
Returns a new QTime instance with the time set to the number of \a msecs
|
||||
since the start of the day, i.e. since 00:00:00.
|
||||
|
||||
If \a msecs falls outside the valid range an invalid QTime will be returned.
|
||||
|
||||
\sa msecsSinceStartOfDay()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn int QTime::msecsSinceStartOfDay() const
|
||||
|
||||
Returns the number of msecs since the start of the day, i.e. since 00:00:00.
|
||||
|
||||
\sa fromMSecsSinceStartOfDay()
|
||||
*/
|
||||
|
||||
/*!
|
||||
\fn QTime::currentTime()
|
||||
|
||||
@ -2695,7 +2714,7 @@ void QDateTime::setMSecsSinceEpoch(qint64 msecs)
|
||||
}
|
||||
|
||||
d->date = QDate(1970, 1, 1).addDays(ddays);
|
||||
d->time = QTime(0, 0, 0).addMSecs(msecs);
|
||||
d->time = QTime::fromMSecsSinceStartOfDay(msecs);
|
||||
|
||||
if (d->spec == QDateTimePrivate::OffsetFromUTC)
|
||||
utcToOffset(&d->date, &d->time, d->m_offsetFromUtc);
|
||||
@ -4449,7 +4468,7 @@ uint qHash(const QDate &key, uint seed) Q_DECL_NOTHROW
|
||||
*/
|
||||
uint qHash(const QTime &key, uint seed) Q_DECL_NOTHROW
|
||||
{
|
||||
return qHash(QTime(0, 0, 0, 0).msecsTo(key), seed);
|
||||
return qHash(key.msecsSinceStartOfDay(), seed);
|
||||
}
|
||||
|
||||
QT_END_NAMESPACE
|
||||
|
@ -168,6 +168,9 @@ public:
|
||||
bool operator>(const QTime &other) const { return mds > other.mds; }
|
||||
bool operator>=(const QTime &other) const { return mds >= other.mds; }
|
||||
|
||||
static inline QTime fromMSecsSinceStartOfDay(int msecs) { QTime t; t.mds = msecs; return t; }
|
||||
inline int msecsSinceStartOfDay() const { return mds == NullTime ? 0 : mds; }
|
||||
|
||||
static QTime currentTime();
|
||||
#ifndef QT_NO_DATESTRING
|
||||
static QTime fromString(const QString &s, Qt::DateFormat f = Qt::TextDate);
|
||||
|
@ -75,6 +75,8 @@ private slots:
|
||||
void toStringFormat_data();
|
||||
void toStringFormat();
|
||||
void toStringLocale();
|
||||
void msecsSinceStartOfDay_data();
|
||||
void msecsSinceStartOfDay();
|
||||
|
||||
private:
|
||||
QTime invalidTime() { return QTime(-1, -1, -1); }
|
||||
@ -732,5 +734,49 @@ void tst_QTime::toStringLocale()
|
||||
QLocale().toString(time, QLocale::ShortFormat));
|
||||
}
|
||||
|
||||
void tst_QTime::msecsSinceStartOfDay_data()
|
||||
{
|
||||
QTest::addColumn<int>("msecs");
|
||||
QTest::addColumn<bool>("isValid");
|
||||
QTest::addColumn<int>("hour");
|
||||
QTest::addColumn<int>("minute");
|
||||
QTest::addColumn<int>("second");
|
||||
QTest::addColumn<int>("msec");
|
||||
|
||||
QTest::newRow("00:00:00.000") << 0 << true
|
||||
<< 0 << 0 << 0 << 0;
|
||||
QTest::newRow("01:00:00.001") << ((1 * 3600 * 1000) + 1) << true
|
||||
<< 1 << 0 << 0 << 1;
|
||||
QTest::newRow("03:04:05.678") << ((3 * 3600 + 4 * 60 + 5) * 1000 + 678) << true
|
||||
<< 3 << 4 << 5 << 678;
|
||||
QTest::newRow("23:59:59.999") << ((23 * 3600 + 59 * 60 + 59) * 1000 + 999) << true
|
||||
<< 23 << 59 << 59 << 999;
|
||||
QTest::newRow("24:00:00.000") << ((24 * 3600) * 1000) << false
|
||||
<< -1 << -1 << -1 << -1;
|
||||
QTest::newRow("-1 invalid") << -1 << false
|
||||
<< -1 << -1 << -1 << -1;
|
||||
}
|
||||
|
||||
void tst_QTime::msecsSinceStartOfDay()
|
||||
{
|
||||
QFETCH(int, msecs);
|
||||
QFETCH(bool, isValid);
|
||||
QFETCH(int, hour);
|
||||
QFETCH(int, minute);
|
||||
QFETCH(int, second);
|
||||
QFETCH(int, msec);
|
||||
|
||||
QTime time = QTime::fromMSecsSinceStartOfDay(msecs);
|
||||
QCOMPARE(time.isValid(), isValid);
|
||||
if (msecs >= 0)
|
||||
QCOMPARE(time.msecsSinceStartOfDay(), msecs);
|
||||
else
|
||||
QCOMPARE(time.msecsSinceStartOfDay(), 0);
|
||||
QCOMPARE(time.hour(), hour);
|
||||
QCOMPARE(time.minute(), minute);
|
||||
QCOMPARE(time.second(), second);
|
||||
QCOMPARE(time.msec(), msec);
|
||||
}
|
||||
|
||||
QTEST_APPLESS_MAIN(tst_QTime)
|
||||
#include "tst_qtime.moc"
|
||||
|
Loading…
Reference in New Issue
Block a user