While I was looking for a way to convert an rclpy.Duration object to it's equivalent in seconds, I came across this OP from ROS-Answers: https://answers.ros.org/question/415959/is-there-a-more-elegant-way-to-get-the-time-in-seconds-in-a-ros-2-node/


1 Answer 1


It seems that the rclpy.Duration() object only has a nanoseconds API. However, to get seconds from an rclpy.Duration() object, we can leverage the to_msg() API from rclpy.Duration() and then use the sec interface like so:

import rclpy

node = rclpy.create_node("my_node")
t1 = node.get_clock().now()
# Do something here that takes some time.
t2 = node.get_clock().now()
time_diff_in_sec = (t2 - t1).to_msg().sec

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.