diff options
-rw-r--r-- | src/corelib/tools/qelapsedtimer.h | 13 | ||||
-rw-r--r-- | src/corelib/tools/qelapsedtimer_generic.cpp | 29 | ||||
-rw-r--r-- | src/corelib/tools/qelapsedtimer_mac.cpp | 11 | ||||
-rw-r--r-- | src/corelib/tools/qelapsedtimer_symbian.cpp | 10 | ||||
-rw-r--r-- | src/corelib/tools/qelapsedtimer_unix.cpp | 14 | ||||
-rw-r--r-- | src/corelib/tools/qelapsedtimer_win.cpp | 10 | ||||
-rw-r--r-- | tests/auto/qelapsedtimer/tst_qelapsedtimer.cpp | 33 |
7 files changed, 103 insertions, 17 deletions
diff --git a/src/corelib/tools/qelapsedtimer.h b/src/corelib/tools/qelapsedtimer.h index 4fa0ca5..827359b 100644 --- a/src/corelib/tools/qelapsedtimer.h +++ b/src/corelib/tools/qelapsedtimer.h @@ -53,8 +53,20 @@ QT_MODULE(Core) class Q_CORE_EXPORT QElapsedTimer { public: + enum ClockType { + SystemTime, + MonotonicClock, + TickCounter, + MachAbsoluteTime + }; + static ClockType clockType(); static bool isMonotonic(); + static inline QElapsedTimer started() + { QElapsedTimer t; t.start(); return t; } + static inline QElapsedTimer invalid() + { QElapsedTimer t; t.invalidate(); return t; } + void start(); qint64 restart(); void invalidate(); @@ -63,6 +75,7 @@ public: qint64 elapsed() const; bool hasExpired(qint64 timeout) const; + qint64 msecsSinceReference() const; qint64 msecsTo(const QElapsedTimer &other) const; qint64 secsTo(const QElapsedTimer &other) const; diff --git a/src/corelib/tools/qelapsedtimer_generic.cpp b/src/corelib/tools/qelapsedtimer_generic.cpp index a9c2233..db6da46 100644 --- a/src/corelib/tools/qelapsedtimer_generic.cpp +++ b/src/corelib/tools/qelapsedtimer_generic.cpp @@ -44,6 +44,17 @@ QT_BEGIN_NAMESPACE +static qint64 currentDateTimeMsec() +{ + QDateTime t = QDateTime::currentDateTime(); + return t.toTime_t() * Q_INT64_C(1000) + t.time().msec(); +} + +QElapsedTimer::ClockType QElapsedTimer::clockType() +{ + return SystemTime; +} + bool QElapsedTimer::isMonotonic() { return false; @@ -51,30 +62,30 @@ bool QElapsedTimer::isMonotonic() void QElapsedTimer::start() { - QTime t = QTime::currentTime(); - t1 = t.mds; - t2 = 0; + restart(); } qint64 QElapsedTimer::restart() { - QTime t = QTime::currentTime(); qint64 old = t1; - t1 = t.mds; + t1 = currentDateTimeMsec(); + t2 = 0; return t1 - old; } qint64 QElapsedTimer::elapsed() const { - QTime t = QTime::currentTime(); - return t.mds - t1; + return currentDateTimeMsec() - t1; +} + +qint64 QElapsedTimer::msecsSinceReference() const +{ + return t1; } qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const { qint64 diff = other.t1 - t1; - if (diff < 0) // passed midnight - diff += 86400 * 1000; return diff; } diff --git a/src/corelib/tools/qelapsedtimer_mac.cpp b/src/corelib/tools/qelapsedtimer_mac.cpp index 21a6d1b..4a62761 100644 --- a/src/corelib/tools/qelapsedtimer_mac.cpp +++ b/src/corelib/tools/qelapsedtimer_mac.cpp @@ -47,6 +47,11 @@ QT_BEGIN_NAMESPACE +QElapsedTimer::ClockType QElapsedTimer::clockType() +{ + return MachAbsoluteTime; +} + bool QElapsedTimer::isMonotonic() { return true; @@ -87,6 +92,7 @@ qint64 QElapsedTimer::restart() { qint64 old = t1; t1 = mach_absolute_time(); + t2 = 0; return absoluteToMSecs(t1 - old); } @@ -97,6 +103,11 @@ qint64 QElapsedTimer::elapsed() const return absoluteToMSecs(cpu_time - t1); } +qint64 QElapsedTimer::msecsSinceReference() +{ + return absoluteToMSecs(t1); +} + qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const { return absoluteToMSecs(other.t1 - t1); diff --git a/src/corelib/tools/qelapsedtimer_symbian.cpp b/src/corelib/tools/qelapsedtimer_symbian.cpp index 1fbba65..06e6f99 100644 --- a/src/corelib/tools/qelapsedtimer_symbian.cpp +++ b/src/corelib/tools/qelapsedtimer_symbian.cpp @@ -53,6 +53,11 @@ static quint64 getMillisecondFromTick() return nanokernel_tick_period * User::NTickCount(); } +QElapsedTimer::ClockType QElapsedTimer::clockType() +{ + return TickCounter; +} + static qint64 difference(qint64 a, qint64 b) { qint64 retval = a - b; @@ -88,6 +93,11 @@ qint64 QElapsedTimer::elapsed() const return difference(getMillisecondFromTick(), t1); } +qint64 QElapsedTimer::msecsSinceReference() const +{ + return t1; +} + qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const { return difference(other.t1, t1); diff --git a/src/corelib/tools/qelapsedtimer_unix.cpp b/src/corelib/tools/qelapsedtimer_unix.cpp index b8215f4..85d7fa8 100644 --- a/src/corelib/tools/qelapsedtimer_unix.cpp +++ b/src/corelib/tools/qelapsedtimer_unix.cpp @@ -87,6 +87,11 @@ bool QElapsedTimer::isMonotonic() #endif } +QElapsedTimer::ClockType QElapsedTimer::clockType() +{ + return isMonotonic() ? MonotonicClock : SystemTime; +} + static inline QPair<long, long> do_gettime() { #if (_POSIX_MONOTONIC_CLOCK-0 > 0) @@ -139,7 +144,7 @@ qint64 QElapsedTimer::restart() r.first -= oldt1; r.second -= oldt2; - return r.first * 1000 + r.second / fractionAdjustment(); + return r.first * Q_INT64_C(1000) + r.second / fractionAdjustment(); } qint64 QElapsedTimer::elapsed() const @@ -149,11 +154,16 @@ qint64 QElapsedTimer::elapsed() const return msecsTo(now); } +qint64 QElapsedTimer::msecsSinceReference() const +{ + return t1 * Q_INT64_C(1000) + t2 / fractionAdjustment(); +} + qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const { qint64 secs = other.t1 - t1; qint64 fraction = other.t2 - t2; - return secs * 1000 + fraction / fractionAdjustment(); + return secs * Q_INT64_C(1000) + fraction / fractionAdjustment(); } qint64 QElapsedTimer::secsTo(const QElapsedTimer &other) const diff --git a/src/corelib/tools/qelapsedtimer_win.cpp b/src/corelib/tools/qelapsedtimer_win.cpp index e384dd8..ce37fa2 100644 --- a/src/corelib/tools/qelapsedtimer_win.cpp +++ b/src/corelib/tools/qelapsedtimer_win.cpp @@ -76,6 +76,11 @@ static quint64 getTickCount() return GetTickCount(); } +QElapsedTimer::ClockType QElapsedTimer::clockType() +{ + return TickCounter; +} + static qint64 difference(qint64 a, qint64 b) { qint64 retval = a - b; @@ -111,6 +116,11 @@ qint64 QElapsedTimer::elapsed() const return difference(getTickCount(), t1); } +qint64 QElapsedTimer::msecsSinceReference() const +{ + return t1; +} + qint64 QElapsedTimer::msecsTo(const QElapsedTimer &other) const { return difference(other.t1, t1); diff --git a/tests/auto/qelapsedtimer/tst_qelapsedtimer.cpp b/tests/auto/qelapsedtimer/tst_qelapsedtimer.cpp index d671d1b..5f797a3 100644 --- a/tests/auto/qelapsedtimer/tst_qelapsedtimer.cpp +++ b/tests/auto/qelapsedtimer/tst_qelapsedtimer.cpp @@ -10,11 +10,19 @@ class tst_QElapsedTimer : public QObject Q_OBJECT private Q_SLOTS: + void statics(); void validity(); void basics(); void elapsed(); }; +void tst_QElapsedTimer::statics() +{ + qDebug() << "Clock type is" << QElapsedTimer::clockType(); + qDebug() << "Said clock is" << (QElapsedTimer::isMonotonic() ? "monotonic" : "not monotonic"); + qDebug() << "Current time is" << QElapsedTimer::started().msecsSinceReference(); +} + void tst_QElapsedTimer::validity() { QElapsedTimer t; @@ -27,6 +35,12 @@ void tst_QElapsedTimer::validity() t.invalidate(); QVERIFY(!t.isValid()); + + t = QElapsedTimer::started(); + QVERIFY(t.isValid()); + + t = QElapsedTimer::invalid(); + QVERIFY(!t.isValid()); } void tst_QElapsedTimer::basics() @@ -34,14 +48,17 @@ void tst_QElapsedTimer::basics() QElapsedTimer t1; t1.start(); + QVERIFY(t1.msecsSinceReference() != 0); + QCOMPARE(t1, t1); QVERIFY(!(t1 != t1)); QVERIFY(!(t1 < t1)); QCOMPARE(t1.msecsTo(t1), qint64(0)); QCOMPARE(t1.secsTo(t1), qint64(0)); - QCOMPARE(t1 + 0, t1); - QCOMPARE(t1 - 0, t1); +// QCOMPARE(t1 + 0, t1); +// QCOMPARE(t1 - 0, t1); +#if 0 QElapsedTimer t2 = t1; t2 += 1000; // so we can use secsTo @@ -51,10 +68,14 @@ void tst_QElapsedTimer::basics() QVERIFY(!(t2 < t1)); QCOMPARE(t1.msecsTo(t2), qint64(1000)); QCOMPARE(t1.secsTo(t2), qint64(1)); - QCOMPARE(t2 - t1, qint64(1000)); - QCOMPARE(t1 - t2, qint64(-1000)); +// QCOMPARE(t2 - t1, qint64(1000)); +// QCOMPARE(t1 - t2, qint64(-1000)); +#endif + quint64 value1 = t1.msecsSinceReference(); qint64 elapsed = t1.restart(); + quint64 value2 = t1.msecsSinceReference(); + QCOMPARE(elapsed, qint64(value2 - value1)); QVERIFY(elapsed < minResolution); } @@ -72,7 +93,7 @@ void tst_QElapsedTimer::elapsed() QVERIFY(t1 < t2); QVERIFY(t1.msecsTo(t2) > 0); // don't check: t1.secsTo(t2) - QVERIFY(t1 - t2 < 0); +// QVERIFY(t1 - t2 < 0); QVERIFY(t1.elapsed() > 0); QVERIFY(t1.hasExpired(minResolution)); @@ -85,7 +106,7 @@ void tst_QElapsedTimer::elapsed() qint64 elapsed = t1.restart(); QVERIFY(elapsed > 3*minResolution); QVERIFY(elapsed < 5*minResolution); - qint64 diff = t1 - t2; + qint64 diff = t2.msecsTo(t1); QVERIFY(diff < minResolution); } |