summaryrefslogtreecommitdiffstats
path: root/src/corelib
diff options
context:
space:
mode:
authorThiago Macieira <thiago.macieira@nokia.com>2010-02-26 15:29:47 (GMT)
committerThiago Macieira <thiago.macieira@nokia.com>2010-03-17 10:54:19 (GMT)
commit047090b2b7af7e5a3b88615dafed9d4d957fbe6e (patch)
treea229dc8559a91adced173c9005c38e3821440ae0 /src/corelib
parentbc88d38905a80b2a025acf5b52858264c0ef0d39 (diff)
downloadQt-047090b2b7af7e5a3b88615dafed9d4d957fbe6e.zip
Qt-047090b2b7af7e5a3b88615dafed9d4d957fbe6e.tar.gz
Qt-047090b2b7af7e5a3b88615dafed9d4d957fbe6e.tar.bz2
Add a couple of extra functions to QElapsedTimer
Diffstat (limited to 'src/corelib')
-rw-r--r--src/corelib/tools/qelapsedtimer.h13
-rw-r--r--src/corelib/tools/qelapsedtimer_generic.cpp29
-rw-r--r--src/corelib/tools/qelapsedtimer_mac.cpp11
-rw-r--r--src/corelib/tools/qelapsedtimer_symbian.cpp10
-rw-r--r--src/corelib/tools/qelapsedtimer_unix.cpp14
-rw-r--r--src/corelib/tools/qelapsedtimer_win.cpp10
6 files changed, 76 insertions, 11 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);