Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Hi @ballubuoy,

In the first example of the ros cookbook, the time is not extracted using a bag object. The time is obtained by calling a subprocess executing the command robag info my_bag.bag and redirecting the output into a yaml object. In the second example, after searching on the C++ API, it seems there is no way to directly get the info from a function like in Python with _get_yaml_info(), but what you can do, instead of using a subprocess is using the View class like this:

#include <rosbag/bag.h>
#include <rosbag/view.h>
#include <std_msgs/Int32.h>
#include <std_msgs/String.h>

int main(int argc, char **argv)
    rosbag::Bag bag;"/path/to/my_bag.bag", rosbag::bagmode::Read);

    rosbag::View view(bag);

    ros::Time bag_begin_time = view.getBeginTime();
    ros::Time bag_end_time = view.getEndTime();

    std::cout << "ROS bag time: " << (bag_end_time-bag_begin_time).toSec() << "(s)" << std::endl;


    return 0;

Since the bag may be not beginning at time 0 we just extract the first time and the end time to compute the time elapsed in the bag, the convert that ros::Time into secs.

Hope that can help you.