Skip to content

rclcpp: Time

The rclcpp::Time and rclcpp::Duration are a significant departure from their ROS 1 equivalents, but are more closely related to std::chrono. For an in-depth discussion comparing with std::chrono, see this discussion on ROS Discourse.

When porting certain ROS 1 libraries, there may be significant usage of timestamps as floating-point seconds. To get floating point seconds from an _rclcpp::Time_:

// node is instance of rclcpp::Node
rclcpp::Time t = node.now();
double seconds = t.seconds();

There is no constructor for Time from seconds represented by a floating point, so you first need to convert to nanoseconds:

rclcpp::Time t(static_cast<uin64_t>(seconds * 1e9));

rclcpp::Duration does have functions to go both directions:

rclcpp::Duration d = rclcpp::Duration::from_seconds(1.0);
double seconds = d.seconds();

Unlike ROS 1, sleeping does NOT occur from a Duration instance:

rclcpp::sleep_for(std::chrono::milliseconds(50));