From 09f287befa73ce0c2c4c751c18945233195fcd38 Mon Sep 17 00:00:00 2001 From: "mergify[bot]" <37929162+mergify[bot]@users.noreply.github.com> Date: Thu, 2 Jan 2025 11:54:16 -0800 Subject: [PATCH] Switch to using get_system_time_in_ns() function everywhere. (backport #354) (#380) (#379) * Switch to using get_system_time_in_ns() function everywhere. (#354) That way we have just a single place where this is defined, and everywhere can be sure they are getting the correct value in nanoseconds. * Add get_system_time_in_ns() --------- Signed-off-by: Chris Lalancette Signed-off-by: Yadunund Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com> Co-authored-by: Chris Lalancette Co-authored-by: Yadunund --- rmw_zenoh_cpp/src/detail/rmw_client_data.cpp | 5 +---- rmw_zenoh_cpp/src/detail/rmw_service_data.cpp | 5 +---- rmw_zenoh_cpp/src/detail/rmw_subscription_data.cpp | 5 ++--- rmw_zenoh_cpp/src/detail/zenoh_utils.cpp | 7 +++++++ rmw_zenoh_cpp/src/detail/zenoh_utils.hpp | 2 ++ 5 files changed, 13 insertions(+), 11 deletions(-) diff --git a/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp b/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp index f85dab86..a0463945 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_client_data.cpp @@ -17,7 +17,6 @@ #include #include -#include #include #include #include @@ -413,10 +412,8 @@ rmw_ret_t ClientData::send_request( return; } - std::chrono::time_point now = std::chrono::system_clock::now(); - sub_data->add_new_reply( - std::make_unique(reply, now.time_since_epoch().count())); + std::make_unique(reply, get_system_time_in_ns())); }, zenoh::closures::none, std::move(opts), diff --git a/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp b/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp index 8bbd9d0b..f3df2df6 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_service_data.cpp @@ -161,10 +161,7 @@ std::shared_ptr ServiceData::make( return; } - std::chrono::nanoseconds::rep received_timestamp = - std::chrono::system_clock::now().time_since_epoch().count(); - - sub_data->add_new_query(std::make_unique(query, received_timestamp)); + sub_data->add_new_query(std::make_unique(query, get_system_time_in_ns())); }, zenoh::closures::none, std::move(qable_options), diff --git a/rmw_zenoh_cpp/src/detail/rmw_subscription_data.cpp b/rmw_zenoh_cpp/src/detail/rmw_subscription_data.cpp index b98f0308..279d3aa6 100644 --- a/rmw_zenoh_cpp/src/detail/rmw_subscription_data.cpp +++ b/rmw_zenoh_cpp/src/detail/rmw_subscription_data.cpp @@ -16,7 +16,6 @@ #include -#include #include #include #include @@ -225,7 +224,7 @@ bool SubscriptionData::init() sub_data->add_new_message( std::make_unique( sample.get_payload(), - std::chrono::system_clock::now().time_since_epoch().count(), + get_system_time_in_ns(), std::move(attachment_data)), std::string(sample.get_keyexpr().as_string_view())); }, @@ -308,7 +307,7 @@ bool SubscriptionData::init() sub_data->add_new_message( std::make_unique( sample.get_payload(), - std::chrono::system_clock::now().time_since_epoch().count(), + get_system_time_in_ns(), std::move(attachment_data)), std::string(sample.get_keyexpr().as_string_view())); }, diff --git a/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp b/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp index 18253fa3..922a80fb 100644 --- a/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp +++ b/rmw_zenoh_cpp/src/detail/zenoh_utils.cpp @@ -83,6 +83,13 @@ std::chrono::nanoseconds::rep ZenohReply::get_received_timestamp() const return received_timestamp_; } +int64_t get_system_time_in_ns() +{ + auto now = std::chrono::system_clock::now().time_since_epoch(); + return std::chrono::duration_cast(now).count(); +} + +///============================================================================= Payload::Payload(const zenoh::Bytes & bytes) { // NOTE(fuzzypixelz): `zenoh::Bytes` is an list of reference-couted buffers. When the list of diff --git a/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp b/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp index 734ade92..58ed471d 100644 --- a/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp +++ b/rmw_zenoh_cpp/src/detail/zenoh_utils.hpp @@ -69,6 +69,8 @@ class ZenohQuery final std::chrono::nanoseconds::rep received_timestamp_; }; +int64_t get_system_time_in_ns(); + class Payload { public: