Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qelapsedtimer.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qelapsedtimer.h"
5
7
180{
181 // we use std::chrono::steady_clock
182 return MonotonicClock;
183}
184
198{
199 // We trust std::chrono::steady_clock to be steady (monotonic); if the
200 // Standard Library is lying to us, users must complain to their vendor.
201 return true;
202}
203
214void QElapsedTimer::start() noexcept
215{
216 static_assert(sizeof(t1) == sizeof(Duration::rep));
217
218 // This assignment will work so long as TimePoint uses the same time
219 // duration or one of finer granularity than steady_clock::time_point. That
220 // means it will work until the first steady_clock using picoseconds.
221 TimePoint now = std::chrono::steady_clock::now();
222 t1 = now.time_since_epoch().count();
223 QT6_ONLY(t2 = 0);
224}
225
245{
246 QElapsedTimer old = *this;
247 start();
248 return old.msecsTo(*this);
249}
250
266{
267 TimePoint then{Duration(t1)};
268 return std::chrono::steady_clock::now() - then;
269}
270
286{
287 return durationElapsed().count();
288}
289
300{
301 using namespace std::chrono;
302 return duration_cast<milliseconds>(durationElapsed()).count();
303}
304
321{
322 using namespace std::chrono;
323 return duration_cast<milliseconds>(Duration(t1)).count();
324}
325
339{
340 Duration d1(t1);
341 Duration d2(other.t1);
342 return d2 - d1;
343}
344
356{
357 using namespace std::chrono;
358 return duration_cast<milliseconds>(durationTo(other)).count();
359}
360
372{
373 using namespace std::chrono;
374 return duration_cast<seconds>(durationTo(other)).count();
375}
376
377static const qint64 invalidData = Q_INT64_C(0x8000000000000000);
378
390{
391 t1 = t2 = invalidData;
392}
393
400bool QElapsedTimer::isValid() const noexcept
401{
402 return t1 != invalidData && t2 != invalidData;
403}
404
414{
415 // if timeout is -1, quint64(timeout) is LLINT_MAX, so this will be
416 // considered as never expired
417 return quint64(elapsed()) > quint64(timeout);
418}
419
420bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
421{
422 return lhs.t1 < rhs.t1;
423}
424
\inmodule QtCore
qint64 msecsSinceReference() const noexcept
Returns the number of milliseconds between last time this QElapsedTimer object was started and its re...
void invalidate() noexcept
Marks this QElapsedTimer object as invalid.
Duration durationTo(const QElapsedTimer &other) const noexcept
Duration durationElapsed() const noexcept
qint64 elapsed() const noexcept
Returns the number of milliseconds since this QElapsedTimer was last started.
qint64 restart() noexcept
Restarts the timer and returns the number of milliseconds elapsed since the previous start.
static bool isMonotonic() noexcept
Returns true if this is a monotonic clock, false otherwise.
ClockType
This enum contains the different clock types that QElapsedTimer may use.
bool hasExpired(qint64 timeout) const noexcept
Returns true if this QElapsedTimer has already expired by timeout milliseconds (that is,...
void start() noexcept
Starts this timer.
bool isValid() const noexcept
Returns false if the timer has never been started or invalidated by a call to invalidate().
qint64 secsTo(const QElapsedTimer &other) const noexcept
Returns the number of seconds between this QElapsedTimer and other.
std::chrono::nanoseconds Duration
qint64 nsecsElapsed() const noexcept
static ClockType clockType() noexcept
Returns the clock type that this QElapsedTimer implementation uses.
std::chrono::time_point< std::chrono::steady_clock, Duration > TimePoint
qint64 msecsTo(const QElapsedTimer &other) const noexcept
Returns the number of milliseconds between this QElapsedTimer and other.
Combined button and popup list for selecting options.
bool operator<(const QElapsedTimer &lhs, const QElapsedTimer &rhs) noexcept
static const qint64 invalidData
GLbitfield GLuint64 timeout
[4]
GLuint GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat GLfloat t1
[4]
QT_BEGIN_NAMESPACE class QT6_ONLY(Q_CORE_EXPORT) QReadWriteLock
static double elapsed(qint64 after, qint64 before)
unsigned long long quint64
Definition qtypes.h:56
long long qint64
Definition qtypes.h:55
#define Q_INT64_C(c)
Definition qtypes.h:52
QDate d1(1995, 5, 17)
[0]
QDate d2(1995, 5, 20)
QSharedPointer< T > other(t)
[5]