ros2 / rclpy

rclpy (ROS Client Library for Python)
Apache License 2.0
268 stars 221 forks source link

Add utility function to get datetime.datetime from Time #1269

Open tonynajjar opened 2 months ago

tonynajjar commented 2 months ago

Feature request

Feature description

I found myself needing to convert from rclpy's Time to datetime.datetime to input that in a third-party library. Would it make sense to add such a utility function? If so I could open a PR

Implementation considerations

I think it would just look like that:

return datetime.datetime.fromtimestamp(self._time_handle.nanoseconds / 1e9)

gordon-n-stevenson commented 2 months ago

This is an excellent idea, as I think it is a common issue when trying to timestamp out things and the management of the seconds/nanoseconds into a correct datetime can be challenging for new users.

It wouldn't be far off something like this SO answer - https://stackoverflow.com/questions/15649942/how-to-convert-epoch-time-with-nanoseconds-to-human-readable - but with the use of rclpy.Time right?