diff --git a/src/viam/sdk/common/utils.cpp b/src/viam/sdk/common/utils.cpp index dbf1846fa..098188ba9 100644 --- a/src/viam/sdk/common/utils.cpp +++ b/src/viam/sdk/common/utils.cpp @@ -22,8 +22,6 @@ namespace viam { namespace sdk { -using time_point = std::chrono::time_point; - std::vector string_to_bytes(const std::string& s) { std::vector bytes(s.begin(), s.end()); return bytes; @@ -34,21 +32,23 @@ std::string bytes_to_string(const std::vector& b) { return img_string; }; -time_point timestamp_to_time_pt(const google::protobuf::Timestamp& timestamp) { - const std::chrono::seconds seconds(timestamp.seconds()); - const std::chrono::nanoseconds nanos(timestamp.nanos()); - return time_point(std::chrono::duration_cast(seconds) + - nanos); +time_pt timestamp_to_time_pt(const google::protobuf::Timestamp& timestamp) { + return time_pt{std::chrono::seconds{timestamp.seconds()} + + std::chrono::nanoseconds{timestamp.nanos()}}; } -google::protobuf::Timestamp time_pt_to_timestamp(const time_point& time_pt) { - const std::chrono::seconds duration_s = - std::chrono::duration_cast(time_pt.time_since_epoch()); - const std::chrono::nanoseconds duration_ns = time_pt.time_since_epoch() - duration_s; - google::protobuf::Timestamp timestamp; - timestamp.set_seconds(duration_s.count()); - timestamp.set_nanos(static_cast(duration_ns.count())); - return timestamp; +google::protobuf::Timestamp time_pt_to_timestamp(time_pt tp) { + const std::chrono::nanoseconds since_epoch = tp.time_since_epoch(); + + const auto sec_floor = std::chrono::duration_cast(since_epoch); + const std::chrono::nanoseconds nano_part = since_epoch - sec_floor; + + google::protobuf::Timestamp result; + + result.set_seconds(sec_floor.count()); + result.set_nanos(static_cast(nano_part.count())); + + return result; } response_metadata response_metadata::from_proto(const viam::common::v1::ResponseMetadata& proto) { @@ -80,7 +80,7 @@ std::chrono::microseconds from_proto(const google::protobuf::Duration& proto) { return from_seconds + from_nanos; } -google::protobuf::Duration to_proto(const std::chrono::microseconds& duration) { +google::protobuf::Duration to_proto(std::chrono::microseconds duration) { namespace sc = std::chrono; const sc::seconds seconds = sc::duration_cast(duration); diff --git a/src/viam/sdk/common/utils.hpp b/src/viam/sdk/common/utils.hpp index 7a83e1929..41fc6b91a 100644 --- a/src/viam/sdk/common/utils.hpp +++ b/src/viam/sdk/common/utils.hpp @@ -18,8 +18,10 @@ const std::string kService = "service"; const std::string kRDK = "rdk"; const std::string kBuiltin = "builtin"; +using time_pt = std::chrono::time_point; + struct response_metadata { - std::chrono::time_point captured_at; + time_pt captured_at; static response_metadata from_proto(const viam::common::v1::ResponseMetadata& proto); static viam::common::v1::ResponseMetadata to_proto(const response_metadata& metadata); @@ -27,21 +29,17 @@ struct response_metadata { bool operator==(const response_metadata& lhs, const response_metadata& rhs); -/// @brief convert a google::protobuf::Timestamp to -/// std::chrono::time_point -std::chrono::time_point timestamp_to_time_pt( - const google::protobuf::Timestamp& timestamp); +/// @brief convert a google::protobuf::Timestamp to time_pt +time_pt timestamp_to_time_pt(const google::protobuf::Timestamp& timestamp); -/// @brief convert a std::chrono::time_point to -/// a google::protobuf::Timestamp. -google::protobuf::Timestamp time_pt_to_timestamp( - const std::chrono::time_point& time_pt); +/// @brief convert a time_pt to a google::protobuf::Timestamp. +google::protobuf::Timestamp time_pt_to_timestamp(time_pt); std::vector string_to_bytes(std::string const& s); std::string bytes_to_string(std::vector const& b); std::chrono::microseconds from_proto(const google::protobuf::Duration& proto); -google::protobuf::Duration to_proto(const std::chrono::microseconds& duration); +google::protobuf::Duration to_proto(std::chrono::microseconds duration); // the authority on a grpc::ClientContext is sometimes set to an invalid uri on mac, causing // `rust-utils` to fail to process gRPC requests. This class provides a convenience wrapper around a diff --git a/src/viam/sdk/robot/client.hpp b/src/viam/sdk/robot/client.hpp index 948f2c9cd..d1f92597d 100644 --- a/src/viam/sdk/robot/client.hpp +++ b/src/viam/sdk/robot/client.hpp @@ -57,9 +57,7 @@ class RobotClient { struct status { boost::optional name; ProtoStruct status_map; - // TODO: RSDK-6574: revisit time_point - boost::optional> - last_reconfigured; + boost::optional last_reconfigured; friend bool operator==(const status& lhs, const status& rhs); }; @@ -68,8 +66,7 @@ class RobotClient { std::string method; boost::optional session_id; ProtoStruct arguments; - // TODO: RSDK-6574: revisit time_point - boost::optional> started; + boost::optional started; friend bool operator==(const operation& lhs, const operation& rhs); }; diff --git a/src/viam/sdk/services/motion.hpp b/src/viam/sdk/services/motion.hpp index f7005ae64..32055cdb5 100644 --- a/src/viam/sdk/services/motion.hpp +++ b/src/viam/sdk/services/motion.hpp @@ -85,7 +85,7 @@ class Motion : public Service { plan_state state; /// @brief The time the executing plan transitioned to the state. - std::chrono::time_point timestamp; + time_pt timestamp; /// @brief The reason for the state change. The error message if the plan failed, or the /// re-plan reason if re-planning was necessary. diff --git a/src/viam/sdk/tests/mocks/camera_mocks.cpp b/src/viam/sdk/tests/mocks/camera_mocks.cpp index 6af908a6e..1840f6a0f 100644 --- a/src/viam/sdk/tests/mocks/camera_mocks.cpp +++ b/src/viam/sdk/tests/mocks/camera_mocks.cpp @@ -56,8 +56,8 @@ Camera::image_collection fake_raw_images() { std::chrono::seconds seconds(12345); std::chrono::nanoseconds nanos(0); collection.images = images; - collection.metadata.captured_at = std::chrono::time_point( - std::chrono::duration_cast(seconds) + nanos); + collection.metadata.captured_at = + time_pt{std::chrono::duration_cast(seconds) + nanos}; return collection; } diff --git a/src/viam/sdk/tests/mocks/mock_motion.cpp b/src/viam/sdk/tests/mocks/mock_motion.cpp index 9d2eb9754..8279409ce 100644 --- a/src/viam/sdk/tests/mocks/mock_motion.cpp +++ b/src/viam/sdk/tests/mocks/mock_motion.cpp @@ -135,9 +135,8 @@ pose_in_frame fake_pose() { } Motion::plan_status MockMotion::fake_plan_status() { - return {Motion::plan_state::k_succeeded, - std::chrono::time_point::max(), - boost::optional("reason")}; + return { + Motion::plan_state::k_succeeded, time_pt::max(), boost::optional("reason")}; } Motion::plan_status_with_id MockMotion::fake_plan_status_with_id() {