summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--src/corelib/tools/qtimestamp.cpp65
-rw-r--r--src/corelib/tools/qtimestamp.h16
-rw-r--r--src/corelib/tools/qtimestamp_generic.cpp8
-rw-r--r--src/corelib/tools/qtimestamp_mac.cpp8
-rw-r--r--src/corelib/tools/qtimestamp_unix.cpp13
-rw-r--r--src/corelib/tools/qtimestamp_win.cpp7
-rw-r--r--src/corelib/tools/tools.pri1
-rw-r--r--tests/auto/qtimestamp/tst_qtimestamp.cpp34
8 files changed, 136 insertions, 16 deletions
diff --git a/src/corelib/tools/qtimestamp.cpp b/src/corelib/tools/qtimestamp.cpp
new file mode 100644
index 0000000..b861099
--- /dev/null
+++ b/src/corelib/tools/qtimestamp.cpp
@@ -0,0 +1,65 @@
+/****************************************************************************
+**
+** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+** All rights reserved.
+** Contact: Nokia Corporation (qt-info@nokia.com)
+**
+** This file is part of the QtCore module of the Qt Toolkit.
+**
+** $QT_BEGIN_LICENSE:LGPL$
+** No Commercial Usage
+** This file contains pre-release code and may not be distributed.
+** You may use this file in accordance with the terms and conditions
+** contained in the Technology Preview License Agreement accompanying
+** this package.
+**
+** GNU Lesser General Public License Usage
+** Alternatively, this file may be used under the terms of the GNU Lesser
+** General Public License version 2.1 as published by the Free Software
+** Foundation and appearing in the file LICENSE.LGPL included in the
+** packaging of this file. Please review the following information to
+** ensure the GNU Lesser General Public License version 2.1 requirements
+** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+**
+** In addition, as a special exception, Nokia gives you certain additional
+** rights. These rights are described in the Nokia Qt LGPL Exception
+** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
+**
+** If you have questions regarding the use of this file, please contact
+** Nokia at qt-info@nokia.com.
+**
+**
+**
+**
+**
+**
+**
+**
+** $QT_END_LICENSE$
+**
+****************************************************************************/
+
+#include "qtimestamp.h"
+
+QT_BEGIN_NAMESPACE
+
+static const qint64 invalidData = Q_INT64_C(0x8000000000000000);
+
+void QTimestamp::invalidate()
+{
+ t1 = t2 = invalidData;
+}
+
+bool QTimestamp::isValid() const
+{
+ return t1 != invalidData && t2 != invalidData;
+}
+
+bool QTimestamp::hasExpired(qint64 timeout) const
+{
+ // if timeout is -1, quint64(timeout) is LLINT_MAX, so this will be
+ // considered as never expired
+ return quint64(elapsed()) > quint64(timeout);
+}
+
+QT_END_NAMESPACE
diff --git a/src/corelib/tools/qtimestamp.h b/src/corelib/tools/qtimestamp.h
index f923cd4..1e913f0 100644
--- a/src/corelib/tools/qtimestamp.h
+++ b/src/corelib/tools/qtimestamp.h
@@ -56,20 +56,12 @@ public:
static bool isMonotonic();
void start();
- qint64 restart()
- {
- qint64 r = elapsed();
- start();
- return r;
- }
+ qint64 restart();
+ void invalidate();
+ bool isValid() const;
qint64 elapsed() const;
- bool hasExpired(qint64 timeout) const
- {
- // if timeout is -1, quint64(timeout) is LLINT_MAX, so this will be
- // considered as never expired
- return quint64(elapsed()) > quint64(timeout);
- }
+ bool hasExpired(qint64 timeout) const;
qint64 msecsTo(const QTimestamp &other) const;
void addMSecs(int ms);
diff --git a/src/corelib/tools/qtimestamp_generic.cpp b/src/corelib/tools/qtimestamp_generic.cpp
index 18c5026..9930932 100644
--- a/src/corelib/tools/qtimestamp_generic.cpp
+++ b/src/corelib/tools/qtimestamp_generic.cpp
@@ -56,6 +56,14 @@ void QTimestamp::start()
t2 = 0;
}
+qint64 QTimestamp::restart()
+{
+ QTime t = QTime::currentTime();
+ qint64 old = t1;
+ t1 = t.mds;
+ return t1 - old;
+}
+
qint64 QTimestamp::elapsed() const
{
QTime t = QTime::currentTime();
diff --git a/src/corelib/tools/qtimestamp_mac.cpp b/src/corelib/tools/qtimestamp_mac.cpp
index e1a7d79..a80e7ee 100644
--- a/src/corelib/tools/qtimestamp_mac.cpp
+++ b/src/corelib/tools/qtimestamp_mac.cpp
@@ -90,6 +90,14 @@ void QTimestamp::start()
t2 = 0;
}
+qint64 QTimestamp::restart()
+{
+ qint64 old = t1;
+ t1 = mach_absolute_time();
+
+ return absoluteToMSecs(t1 - old);
+}
+
qint64 QTimestamp::elapsed() const
{
uint64_t cpu_time = mach_absolute_time();
diff --git a/src/corelib/tools/qtimestamp_unix.cpp b/src/corelib/tools/qtimestamp_unix.cpp
index dbf49c1..75371e7 100644
--- a/src/corelib/tools/qtimestamp_unix.cpp
+++ b/src/corelib/tools/qtimestamp_unix.cpp
@@ -129,6 +129,19 @@ void QTimestamp::start()
t2 = r.second;
}
+qint64 QTimestamp::restart()
+{
+ QPair<long, long> r = do_gettime();
+ qint64 oldt1 = t1;
+ qint64 oldt2 = t2;
+ t1 = r.first;
+ t2 = r.second;
+
+ r.first -= oldt1;
+ r.second -= oldt2;
+ return r.first * 1000 + r.second / fractionAdjustment();
+}
+
qint64 QTimestamp::elapsed() const
{
QTimestamp now;
diff --git a/src/corelib/tools/qtimestamp_win.cpp b/src/corelib/tools/qtimestamp_win.cpp
index 99faebd..72d38d0 100644
--- a/src/corelib/tools/qtimestamp_win.cpp
+++ b/src/corelib/tools/qtimestamp_win.cpp
@@ -87,6 +87,13 @@ void QTimestamp::start()
t2 = 0;
}
+qint64 QTimestamp::restart()
+{
+ qint64 oldt1 = t1;
+ t1 = getTickCount();
+ return t1 - oldt1;
+}
+
qint64 QTimestamp::elapsed() const
{
return getTickCount() - t1;
diff --git a/src/corelib/tools/tools.pri b/src/corelib/tools/tools.pri
index 863f86e..e1097c4 100644
--- a/src/corelib/tools/tools.pri
+++ b/src/corelib/tools/tools.pri
@@ -77,6 +77,7 @@ SOURCES += \
tools/qstringlist.cpp \
tools/qtextboundaryfinder.cpp \
tools/qtimeline.cpp \
+ tools/qtimestamp.cpp \
tools/qvector.cpp \
tools/qvsnprintf.cpp
diff --git a/tests/auto/qtimestamp/tst_qtimestamp.cpp b/tests/auto/qtimestamp/tst_qtimestamp.cpp
index 5a0b230..ab1e959 100644
--- a/tests/auto/qtimestamp/tst_qtimestamp.cpp
+++ b/tests/auto/qtimestamp/tst_qtimestamp.cpp
@@ -3,15 +3,32 @@
#include <QtCore/QTimestamp>
#include <QtTest/QtTest>
+static const int minResolution = 50; // the minimum resolution for the tests
+
class tst_QTimestamp : public QObject
{
Q_OBJECT
private Q_SLOTS:
+ void validity();
void basics();
void elapsed();
};
+void tst_QTimestamp::validity()
+{
+ QTimestamp t;
+
+ t.invalidate();
+ QVERIFY(!t.isValid());
+
+ t.start();
+ QVERIFY(t.isValid());
+
+ t.invalidate();
+ QVERIFY(!t.isValid());
+}
+
void tst_QTimestamp::basics()
{
QTimestamp t1;
@@ -36,6 +53,9 @@ void tst_QTimestamp::basics()
QCOMPARE(t1.secsTo(t2), qint64(1));
QCOMPARE(t2 - t1, qint64(1000));
QCOMPARE(t1 - t2, qint64(-1000));
+
+ qint64 elapsed = t1.restart();
+ QVERIFY(elapsed < minResolution);
}
void tst_QTimestamp::elapsed()
@@ -43,7 +63,7 @@ void tst_QTimestamp::elapsed()
QTimestamp t1;
t1.start();
- QTest::qWait(100);
+ QTest::qWait(4*minResolution);
QTimestamp t2;
t2.start();
@@ -55,12 +75,18 @@ void tst_QTimestamp::elapsed()
QVERIFY(t1 - t2 < 0);
QVERIFY(t1.elapsed() > 0);
- QVERIFY(t1.hasExpired(50));
- QVERIFY(!t1.hasExpired(500));
- QVERIFY(!t2.hasExpired(0));
+ QVERIFY(t1.hasExpired(minResolution));
+ QVERIFY(!t1.hasExpired(8*minResolution));
+ QVERIFY(!t2.hasExpired(minResolution));
QVERIFY(!t1.hasExpired(-1));
QVERIFY(!t2.hasExpired(-1));
+
+ qint64 elapsed = t1.restart();
+ QVERIFY(elapsed > 3*minResolution);
+ QVERIFY(elapsed < 5*minResolution);
+ qint64 diff = t1 - t2;
+ QVERIFY(diff < minResolution);
}
QTEST_MAIN(tst_QTimestamp);