Skip to content

Commit b9f2345

Browse files
authored
RSDK-6574: Fix use of time_point (#335)
1 parent 21e7d2f commit b9f2345

File tree

6 files changed

+31
-37
lines changed

6 files changed

+31
-37
lines changed

src/viam/sdk/common/utils.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@
2222
namespace viam {
2323
namespace sdk {
2424

25-
using time_point = std::chrono::time_point<long long, std::chrono::nanoseconds>;
26-
2725
std::vector<unsigned char> string_to_bytes(const std::string& s) {
2826
std::vector<unsigned char> bytes(s.begin(), s.end());
2927
return bytes;
@@ -34,21 +32,23 @@ std::string bytes_to_string(const std::vector<unsigned char>& b) {
3432
return img_string;
3533
};
3634

37-
time_point timestamp_to_time_pt(const google::protobuf::Timestamp& timestamp) {
38-
const std::chrono::seconds seconds(timestamp.seconds());
39-
const std::chrono::nanoseconds nanos(timestamp.nanos());
40-
return time_point(std::chrono::duration_cast<std::chrono::system_clock::duration>(seconds) +
41-
nanos);
35+
time_pt timestamp_to_time_pt(const google::protobuf::Timestamp& timestamp) {
36+
return time_pt{std::chrono::seconds{timestamp.seconds()} +
37+
std::chrono::nanoseconds{timestamp.nanos()}};
4238
}
4339

44-
google::protobuf::Timestamp time_pt_to_timestamp(const time_point& time_pt) {
45-
const std::chrono::seconds duration_s =
46-
std::chrono::duration_cast<std::chrono::seconds>(time_pt.time_since_epoch());
47-
const std::chrono::nanoseconds duration_ns = time_pt.time_since_epoch() - duration_s;
48-
google::protobuf::Timestamp timestamp;
49-
timestamp.set_seconds(duration_s.count());
50-
timestamp.set_nanos(static_cast<int32_t>(duration_ns.count()));
51-
return timestamp;
40+
google::protobuf::Timestamp time_pt_to_timestamp(time_pt tp) {
41+
const std::chrono::nanoseconds since_epoch = tp.time_since_epoch();
42+
43+
const auto sec_floor = std::chrono::duration_cast<std::chrono::seconds>(since_epoch);
44+
const std::chrono::nanoseconds nano_part = since_epoch - sec_floor;
45+
46+
google::protobuf::Timestamp result;
47+
48+
result.set_seconds(sec_floor.count());
49+
result.set_nanos(static_cast<int32_t>(nano_part.count()));
50+
51+
return result;
5252
}
5353

5454
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) {
8080
return from_seconds + from_nanos;
8181
}
8282

83-
google::protobuf::Duration to_proto(const std::chrono::microseconds& duration) {
83+
google::protobuf::Duration to_proto(std::chrono::microseconds duration) {
8484
namespace sc = std::chrono;
8585

8686
const sc::seconds seconds = sc::duration_cast<sc::seconds>(duration);

src/viam/sdk/common/utils.hpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,30 +18,28 @@ const std::string kService = "service";
1818
const std::string kRDK = "rdk";
1919
const std::string kBuiltin = "builtin";
2020

21+
using time_pt = std::chrono::time_point<std::chrono::system_clock, std::chrono::nanoseconds>;
22+
2123
struct response_metadata {
22-
std::chrono::time_point<long long, std::chrono::nanoseconds> captured_at;
24+
time_pt captured_at;
2325

2426
static response_metadata from_proto(const viam::common::v1::ResponseMetadata& proto);
2527
static viam::common::v1::ResponseMetadata to_proto(const response_metadata& metadata);
2628
};
2729

2830
bool operator==(const response_metadata& lhs, const response_metadata& rhs);
2931

30-
/// @brief convert a google::protobuf::Timestamp to
31-
/// std::chrono::time_point<long long, std::chrono::nanoseconds>
32-
std::chrono::time_point<long long, std::chrono::nanoseconds> timestamp_to_time_pt(
33-
const google::protobuf::Timestamp& timestamp);
32+
/// @brief convert a google::protobuf::Timestamp to time_pt
33+
time_pt timestamp_to_time_pt(const google::protobuf::Timestamp& timestamp);
3434

35-
/// @brief convert a std::chrono::time_point<long long, std::chrono::nanoseconds> to
36-
/// a google::protobuf::Timestamp.
37-
google::protobuf::Timestamp time_pt_to_timestamp(
38-
const std::chrono::time_point<long long, std::chrono::nanoseconds>& time_pt);
35+
/// @brief convert a time_pt to a google::protobuf::Timestamp.
36+
google::protobuf::Timestamp time_pt_to_timestamp(time_pt);
3937

4038
std::vector<unsigned char> string_to_bytes(std::string const& s);
4139
std::string bytes_to_string(std::vector<unsigned char> const& b);
4240

4341
std::chrono::microseconds from_proto(const google::protobuf::Duration& proto);
44-
google::protobuf::Duration to_proto(const std::chrono::microseconds& duration);
42+
google::protobuf::Duration to_proto(std::chrono::microseconds duration);
4543

4644
// the authority on a grpc::ClientContext is sometimes set to an invalid uri on mac, causing
4745
// `rust-utils` to fail to process gRPC requests. This class provides a convenience wrapper around a

src/viam/sdk/robot/client.hpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,7 @@ class RobotClient {
5757
struct status {
5858
boost::optional<Name> name;
5959
ProtoStruct status_map;
60-
// TODO: RSDK-6574: revisit time_point
61-
boost::optional<std::chrono::time_point<long long, std::chrono::nanoseconds>>
62-
last_reconfigured;
60+
boost::optional<time_pt> last_reconfigured;
6361
friend bool operator==(const status& lhs, const status& rhs);
6462
};
6563

@@ -68,8 +66,7 @@ class RobotClient {
6866
std::string method;
6967
boost::optional<std::string> session_id;
7068
ProtoStruct arguments;
71-
// TODO: RSDK-6574: revisit time_point
72-
boost::optional<std::chrono::time_point<long long, std::chrono::nanoseconds>> started;
69+
boost::optional<time_pt> started;
7370
friend bool operator==(const operation& lhs, const operation& rhs);
7471
};
7572

src/viam/sdk/services/motion.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ class Motion : public Service {
8585
plan_state state;
8686

8787
/// @brief The time the executing plan transitioned to the state.
88-
std::chrono::time_point<long long, std::chrono::nanoseconds> timestamp;
88+
time_pt timestamp;
8989

9090
/// @brief The reason for the state change. The error message if the plan failed, or the
9191
/// re-plan reason if re-planning was necessary.

src/viam/sdk/tests/mocks/camera_mocks.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ Camera::image_collection fake_raw_images() {
5656
std::chrono::seconds seconds(12345);
5757
std::chrono::nanoseconds nanos(0);
5858
collection.images = images;
59-
collection.metadata.captured_at = std::chrono::time_point<long long, std::chrono::nanoseconds>(
60-
std::chrono::duration_cast<std::chrono::system_clock::duration>(seconds) + nanos);
59+
collection.metadata.captured_at =
60+
time_pt{std::chrono::duration_cast<std::chrono::system_clock::duration>(seconds) + nanos};
6161
return collection;
6262
}
6363

src/viam/sdk/tests/mocks/mock_motion.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,8 @@ pose_in_frame fake_pose() {
135135
}
136136

137137
Motion::plan_status MockMotion::fake_plan_status() {
138-
return {Motion::plan_state::k_succeeded,
139-
std::chrono::time_point<long long, std::chrono::nanoseconds>::max(),
140-
boost::optional<std::string>("reason")};
138+
return {
139+
Motion::plan_state::k_succeeded, time_pt::max(), boost::optional<std::string>("reason")};
141140
}
142141

143142
Motion::plan_status_with_id MockMotion::fake_plan_status_with_id() {

0 commit comments

Comments
 (0)