[Documentation] [TitleIndex] [WordIndex

Time and Duration

ROS has builtin time and duration primitive types, which rosserial provides as the ros::Time and ros::Duration classes, respectively. A Time is a specific moment (e.g. "today at 5pm") whereas a Duration is a period of time (e.g. "5 hours"). Durations can be negative.

Times and durations have identical representations:

unsigned long secs
unsigned long nsecs

Getting the Current Time

In order to get the current ROS time, you must be connected to ROS. Then, you can use your NodeHandle object, nh, to query the current time. nh.now()

   1 ros::Time begin = nh.now();

Creating Time and Duration Instances

You can create a Time or Duration to a specific value as well, either using a two-integer constructor:

   1 ros::Time a_little_after_the_beginning(0, 10000);
   2 ros::Duration five_seconds(5, 0);

Converting Time and Duration Instances

Time and Duration objects can also be turned into floating point seconds:

   1 double secs = nh.now().toSec();
   2 
   3 ros::Duration d(1, 0);
   4 secs = d.toSec();

2024-10-05 16:50