Rclex.Time (Rclex (Experimental) v0.12.0)

Copy Markdown View Source

A point in time, paired with the clock type that produced it.

Stored as nanoseconds since the clock's epoch. The :clock_type field matches the Rclex.Clock.clock_type/0 of the clock that produced the time and is used to prevent meaningless arithmetic between incompatible clocks (e.g. subtracting a steady-time Time from a system-time Time).

ROS 2 messages typically carry timestamps as builtin_interfaces/msg/Time. Use to_msg/1 and from_msg/2 to convert.

Summary

Functions

Add a Duration to a Time, returning a new Time.

Compare two Time values; returns :lt, :eq, or :gt.

Build a Time from a builtin_interfaces/msg/Time-shaped struct or map.

Create a Time from a number of seconds.

Create a Time from an integer number of nanoseconds.

Subtract a Time or Duration from a Time.

Convert to a Rclex.Pkgs.BuiltinInterfaces.Msg.Time struct.

Total nanoseconds since the clock's epoch.

Time in seconds (floating point).

Types

clock_type()

@type clock_type() :: :system_time | :steady_time | :ros_time

t()

@type t() :: %Rclex.Time{clock_type: clock_type(), nanoseconds: integer()}

Functions

add(time, duration)

@spec add(t(), Rclex.Duration.t()) :: t()

Add a Duration to a Time, returning a new Time.

compare(time1, time2)

@spec compare(t(), t()) :: :lt | :eq | :gt

Compare two Time values; returns :lt, :eq, or :gt.

from_msg(map, clock_type \\ :system_time)

@spec from_msg(map(), clock_type()) :: t()

Build a Time from a builtin_interfaces/msg/Time-shaped struct or map.

from_seconds(seconds, clock_type \\ :system_time)

@spec from_seconds(number(), clock_type()) :: t()

Create a Time from a number of seconds.

new(nanoseconds, clock_type \\ :system_time)

@spec new(integer(), clock_type()) :: t()

Create a Time from an integer number of nanoseconds.

sub(arg1, arg2)

@spec sub(t(), t() | Rclex.Duration.t()) :: Rclex.Duration.t() | t()

Subtract a Time or Duration from a Time.

Subtracting two Time values returns a Duration. Both must share the same :clock_type, otherwise raises ArgumentError.

to_msg(time)

@spec to_msg(t()) :: struct()

Convert to a Rclex.Pkgs.BuiltinInterfaces.Msg.Time struct.

Note: ROS message Time uses unsigned nanosec, so negative Time values are not representable. Conversion of a negative Time raises.

to_nanoseconds(time)

@spec to_nanoseconds(t()) :: integer()

Total nanoseconds since the clock's epoch.

to_seconds(time)

@spec to_seconds(t()) :: float()

Time in seconds (floating point).