|Please ask about problems and questions regarding this tutorial on answers.ros.org. Don't forget to include in your question the link to this page, the versions of your OS & ROS, and also add appropriate tags.|
Timestamps and DurationsDescription: Timestamping and performing arithmetic on timestamps/durations.
Keywords: ecl timestamps durations
Tutorial Level: INTERMEDIATE
On a posix system, the timestamp class can be used to capture the current time. It does this using the MONOTONIC_CLOCK which calculates the time since the cpu was turned on. It has the advantage in that you are guaranteed that the clock will never jump backwards to correct for drift which is imperative for control systems running high frequency loops.
It can be also used just to store a time/duration in a convenient structure.
These operations can also be performed to set the timestamp after construction:
All the usual comparison (==,!=,<=,>=,<,>) and mathematical (+,-,+=,-=) operations can also be used.
Caution: The only thing to be wary of with timestamps is to remember that they must always be positive. This was a design decision that keeps the timestamp class as light as possible. If negativity was introduced, an extra sign bit would be required, and in almost all timestamp operations, this is not necessary.
Exceptions: The timestamp class will throw exceptions (in debug mode only) whenever a timestamp method is used that would create a negative timestamp.
Durations are intended to intuitively represent the passage of time. Although is is conceptually different from a timestamp, the functionality under the hood is currently identical. Subsequently, the ecl::TimeStamp class is currently typedef'd to the ecl::Duration class.
This might change at some point in the future if we require negativity (as mentioned above).