diff options
author | Ahmad Samir <a.samirh78@gmail.com> | 2023-01-27 22:34:46 +0200 |
---|---|---|
committer | Ahmad Samir <a.samirh78@gmail.com> | 2023-02-20 21:28:38 +0200 |
commit | dd82f399106a66ffabdc05d55cc72d46f8bca1c7 (patch) | |
tree | c78ca0096c68031cc5d57bd4b4c33dab85049fc7 /src/corelib/tools/qtools_p.h | |
parent | 37032b15900d2ae777d83e61a521aa6a0d01836a (diff) | |
download | qtbase-dd82f399106a66ffabdc05d55cc72d46f8bca1c7.tar.gz |
QtMiscUtils: add std::chrono::duration <-> timespec helpers
Change-Id: I91f36a3d651fd57443072fde4c3e8f811682328e
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Diffstat (limited to 'src/corelib/tools/qtools_p.h')
-rw-r--r-- | src/corelib/tools/qtools_p.h | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/src/corelib/tools/qtools_p.h b/src/corelib/tools/qtools_p.h index 1423a672d6..2c7ad6f0de 100644 --- a/src/corelib/tools/qtools_p.h +++ b/src/corelib/tools/qtools_p.h @@ -16,7 +16,10 @@ // #include "QtCore/private/qglobal_p.h" + +#include <chrono> #include <limits.h> +#include <time.h> QT_BEGIN_NAMESPACE @@ -104,8 +107,32 @@ constexpr inline int qt_lencmp(qsizetype lhs, qsizetype rhs) noexcept lhs > rhs ? 1 : /* else */ -1 ; } + +inline timespec durationToTimespec(std::chrono::nanoseconds timeout) noexcept +{ + using namespace std::chrono; + const seconds secs = duration_cast<seconds>(timeout); + const nanoseconds frac = timeout - secs; + struct timespec ts; + ts.tv_sec = secs.count(); + ts.tv_nsec = frac.count(); + return ts; +} + +template <typename Duration> +inline Duration timespecToChrono(struct timespec *ts) noexcept +{ + using namespace std::chrono; + return duration_cast<Duration>(seconds{ts->tv_sec} + nanoseconds{ts->tv_nsec}); +} + +inline std::chrono::milliseconds timespecToChronoMs(struct timespec *ts) noexcept +{ + return timespecToChrono<std::chrono::milliseconds>(ts); } +} // namespace QtMiscUtils + // We typically need an extra bit for qNextPowerOfTwo when determining the next allocation size. constexpr qsizetype MaxAllocSize = (std::numeric_limits<qsizetype>::max)(); |