ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question
1

Convience methods to convert rospy.Time to datetime.datetime

asked 2018-03-26 14:38:02 -0600

ivaughn gravatar image

I couldn't find any convenience methods to convert rospy.Time to datetime.datetime. Timezones and other issues make this less trivial than it appears, especially for novice users.

Am I missing something obvious, or is this a textbook case of "nope, not done yet, please submit a patch?"

Thanks!

edit retag flag offensive close merge delete

Comments

I would say this is indeed something that just hasn't been done yet. Shouldn't be too difficult though: rospy.Time is a Unix stamp, so conversions should be against UTC, correct?

gvdhoorn gravatar image gvdhoorn  ( 2018-03-27 02:39:06 -0600 )edit

1 Answer

Sort by ยป oldest newest most voted
4

answered 2020-11-10 23:47:16 -0600

rgov gravatar image

updated 2022-12-08 08:46:32 -0600

lucasw gravatar image

datetime.datetime to rospy.Time:

import datetime

import rospy

dt0 = datetime.datetime.strptime('2018-03-26-14:38:02.012345', '%Y-%m-%d-%H:%M:%S.%f').replace(tzinfo=datetime.timezone.utc)

rt0 = rospy.Time.from_sec(dt0.timestamp())

rospy.Time to datetime.datetime and back:

dt1 = datetime.datetime.utcfromtimestamp(rt0.to_sec())

rt1 = rospy.Time.from_sec(dt1.replace(tzinfo=datetime.timezone.utc).timestamp())
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-03-26 14:38:02 -0600

Seen: 4,736 times

Last updated: Dec 08 '22