From 5e4694abba4236f5cf858a038af7eb3375b77f9c Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 19 May 2023 14:23:56 +0200 Subject: [PATCH] util: uvw_clock_id and gettime --- src/uvw/util.cpp | 6 ++++++ src/uvw/util.h | 12 ++++++++++++ test/uvw/util.cpp | 1 + 3 files changed, 19 insertions(+) diff --git a/src/uvw/util.cpp b/src/uvw/util.cpp index b0390b7b..ccf2ad55 100644 --- a/src/uvw/util.cpp +++ b/src/uvw/util.cpp @@ -322,6 +322,12 @@ UVW_INLINE resource_usage utilities::rusage() noexcept { return err ? resource_usage{} : ru; } +UVW_INLINE timespec64 utilities::gettime(clock_id source) noexcept { + timespec64 ts; + auto err = uv_clock_gettime(static_cast(source), &ts); + return err ? timespec64{} : ts; +} + UVW_INLINE uint64_t utilities::hrtime() noexcept { return uv_hrtime(); } diff --git a/src/uvw/util.h b/src/uvw/util.h index ed8c276e..288e6334 100644 --- a/src/uvw/util.h +++ b/src/uvw/util.h @@ -38,6 +38,11 @@ enum class uvw_handle_type : std::underlying_type_t { FILE = UV_FILE }; +enum class uvw_clock_id : std::underlying_type_t { + MONOTONIC = UV_CLOCK_MONOTONIC, + REALTIME = UV_CLOCK_REALTIME +}; + template struct uv_type_wrapper { using Type = T; @@ -603,6 +608,13 @@ struct utilities { */ static resource_usage rusage() noexcept; + /** + * @brief Gets the current system time from a high-resolution clock source. + * @param source Clock source, either real-time or monotonic. + * @return Current system time from the given high-resolution clock source. + */ + static timespec64 gettime(clock_id source) noexcept; + /** * @brief Gets the current high-resolution real time. * diff --git a/test/uvw/util.cpp b/test/uvw/util.cpp index 9a5ba91a..de4d2a9c 100644 --- a/test/uvw/util.cpp +++ b/test/uvw/util.cpp @@ -83,6 +83,7 @@ TEST(Util, Utilities) { ASSERT_NE(uvw::utilities::total_memory(), decltype(uvw::utilities::total_memory()){0}); ASSERT_NE(uvw::utilities::uptime(), decltype(uvw::utilities::uptime()){0}); ASSERT_NO_THROW(uvw::utilities::rusage()); + ASSERT_NO_THROW(uvw::utilities::gettime(uvw::clock_id::MONOTONIC)); ASSERT_NE(uvw::utilities::hrtime(), decltype(uvw::utilities::hrtime()){0}); ASSERT_FALSE(uvw::utilities::path().empty()); ASSERT_FALSE(uvw::utilities::cwd().empty());