Skip to content

Commit

Permalink
Switch to using get_system_time_in_ns() function everywhere. (backport
Browse files Browse the repository at this point in the history
…#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 <[email protected]>
Signed-off-by: Yadunund <[email protected]>
Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: Chris Lalancette <[email protected]>
Co-authored-by: Yadunund <[email protected]>
  • Loading branch information
3 people authored Jan 2, 2025
1 parent 257e3d5 commit 09f287b
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 11 deletions.
5 changes: 1 addition & 4 deletions rmw_zenoh_cpp/src/detail/rmw_client_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
#include <fastcdr/FastBuffer.h>

#include <array>
#include <chrono>
#include <cinttypes>
#include <limits>
#include <memory>
Expand Down Expand Up @@ -413,10 +412,8 @@ rmw_ret_t ClientData::send_request(
return;
}

std::chrono::time_point<std::chrono::system_clock> now = std::chrono::system_clock::now();

sub_data->add_new_reply(
std::make_unique<rmw_zenoh_cpp::ZenohReply>(reply, now.time_since_epoch().count()));
std::make_unique<rmw_zenoh_cpp::ZenohReply>(reply, get_system_time_in_ns()));
},
zenoh::closures::none,
std::move(opts),
Expand Down
5 changes: 1 addition & 4 deletions rmw_zenoh_cpp/src/detail/rmw_service_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -161,10 +161,7 @@ std::shared_ptr<ServiceData> 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<ZenohQuery>(query, received_timestamp));
sub_data->add_new_query(std::make_unique<ZenohQuery>(query, get_system_time_in_ns()));
},
zenoh::closures::none,
std::move(qable_options),
Expand Down
5 changes: 2 additions & 3 deletions rmw_zenoh_cpp/src/detail/rmw_subscription_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@

#include <fastcdr/FastBuffer.h>

#include <chrono>
#include <cinttypes>
#include <limits>
#include <memory>
Expand Down Expand Up @@ -225,7 +224,7 @@ bool SubscriptionData::init()
sub_data->add_new_message(
std::make_unique<SubscriptionData::Message>(
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()));
},
Expand Down Expand Up @@ -308,7 +307,7 @@ bool SubscriptionData::init()
sub_data->add_new_message(
std::make_unique<SubscriptionData::Message>(
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()));
},
Expand Down
7 changes: 7 additions & 0 deletions rmw_zenoh_cpp/src/detail/zenoh_utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::chrono::nanoseconds>(now).count();
}

///=============================================================================
Payload::Payload(const zenoh::Bytes & bytes)
{
// NOTE(fuzzypixelz): `zenoh::Bytes` is an list of reference-couted buffers. When the list of
Expand Down
2 changes: 2 additions & 0 deletions rmw_zenoh_cpp/src/detail/zenoh_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ class ZenohQuery final
std::chrono::nanoseconds::rep received_timestamp_;
};

int64_t get_system_time_in_ns();

class Payload
{
public:
Expand Down

0 comments on commit 09f287b

Please sign in to comment.