19 #include "rclcpp/clock.hpp"
20 #include "rclcpp/duration.hpp"
22 #include "builtin_interfaces/msg/time.hpp"
26 #include "rclcpp/exceptions.hpp"
28 #include "rcutils/logging_macros.h"
30 #include "rclcpp/utilities.hpp"
50 : rcl_time_(init_time_point(clock_type))
53 throw std::runtime_error(
"cannot store a negative time point in rclcpp::Time");
61 : rcl_time_(init_time_point(clock_type))
64 throw std::runtime_error(
"cannot store a negative time point in rclcpp::Time");
75 const builtin_interfaces::msg::Time & time_msg,
77 : rcl_time_(init_time_point(clock_type))
79 if (time_msg.sec < 0) {
80 throw std::runtime_error(
"cannot store a negative time point in rclcpp::Time");
88 : rcl_time_(time_point)
95 Time::operator builtin_interfaces::msg::Time()
const
106 *
this =
Time(time_msg);
117 throw std::runtime_error(
"can't compare times with different time sources");
126 return !(*
this == rhs);
133 throw std::runtime_error(
"can't compare times with different time sources");
143 throw std::runtime_error(
"can't compare times with different time sources");
153 throw std::runtime_error(
"can't compare times with different time sources");
163 throw std::runtime_error(
"can't compare times with different time sources");
173 throw std::overflow_error(
"addition leads to int64_t overflow");
176 throw std::underflow_error(
"addition leads to int64_t underflow");
185 throw std::runtime_error(
186 std::string(
"can't subtract times with different time sources [") +
187 std::to_string(rcl_time_.
clock_type) +
" != " +
188 std::to_string(rhs.rcl_time_.
clock_type) +
"]");
192 throw std::overflow_error(
"time subtraction leads to int64_t overflow");
196 throw std::underflow_error(
"time subtraction leads to int64_t underflow");
206 throw std::overflow_error(
"time subtraction leads to int64_t overflow");
209 throw std::underflow_error(
"time subtraction leads to int64_t underflow");
224 return std::chrono::duration<double>(std::chrono::nanoseconds(rcl_time_.
nanoseconds)).count();
237 throw std::overflow_error(
"addition leads to int64_t overflow");
240 throw std::underflow_error(
"addition leads to int64_t underflow");
249 throw std::overflow_error(
"addition leads to int64_t overflow");
252 throw std::underflow_error(
"addition leads to int64_t underflow");
257 throw std::runtime_error(
"cannot store a negative time point in rclcpp::Time");
267 throw std::overflow_error(
"time subtraction leads to int64_t overflow");
270 throw std::underflow_error(
"time subtraction leads to int64_t underflow");
275 throw std::runtime_error(
"cannot store a negative time point in rclcpp::Time");
284 return Time(std::numeric_limits<int32_t>::max(), 999999999, clock_type);
287 builtin_interfaces::msg::Time
290 builtin_interfaces::msg::Time ret;
292 const auto result = std::div(time_point, kRemainder);
293 if (result.rem >= 0) {
294 ret.sec =
static_cast<std::int32_t
>(result.quot);
295 ret.nanosec =
static_cast<std::uint32_t
>(result.rem);
297 ret.sec =
static_cast<std::int32_t
>(result.quot - 1);
298 ret.nanosec =
static_cast<std::uint32_t
>(kRemainder + result.rem);
rcl_duration_value_t nanoseconds() const
Get duration in nanosecods.
static Duration from_nanoseconds(rcl_duration_value_t nanoseconds)
Create a duration object from an integer number representing nanoseconds.
RCLCPP_PUBLIC Time & operator-=(const rclcpp::Duration &rhs)
RCLCPP_PUBLIC Time & operator+=(const rclcpp::Duration &rhs)
RCLCPP_PUBLIC bool operator>=(const rclcpp::Time &rhs) const
RCLCPP_PUBLIC double seconds() const
Get the seconds since epoch.
virtual RCLCPP_PUBLIC ~Time()
Time destructor.
RCLCPP_PUBLIC Duration operator-(const rclcpp::Time &rhs) const
RCLCPP_PUBLIC rcl_time_point_value_t nanoseconds() const
Get the nanoseconds since epoch.
RCLCPP_PUBLIC bool operator<(const rclcpp::Time &rhs) const
RCLCPP_PUBLIC Time & operator=(const Time &rhs)
RCLCPP_PUBLIC rcl_clock_type_t get_clock_type() const
Get the clock type.
RCLCPP_PUBLIC bool operator<=(const rclcpp::Time &rhs) const
static RCLCPP_PUBLIC Time max(rcl_clock_type_t clock_type=RCL_SYSTEM_TIME)
Get the maximum representable value.
RCLCPP_PUBLIC bool operator==(const rclcpp::Time &rhs) const
RCLCPP_PUBLIC Time operator+(const rclcpp::Duration &rhs) const
RCLCPP_PUBLIC bool operator>(const rclcpp::Time &rhs) const
RCLCPP_PUBLIC Time(int32_t seconds, uint32_t nanoseconds, rcl_clock_type_t clock_type=RCL_SYSTEM_TIME)
Time constructor.
Versions of rosidl_typesupport_cpp::get_message_type_support_handle that handle adapted types.
RCLCPP_PUBLIC builtin_interfaces::msg::Time convert_rcl_time_to_sec_nanos(const rcl_time_point_value_t &time_point)
Convert rcl_time_point_value_t to builtin_interfaces::msg::Time.
bool add_will_underflow(const T x, const T y)
Safely check if addition will underflow.
bool sub_will_overflow(const T x, const T y)
Safely check if subtraction will overflow.
bool add_will_overflow(const T x, const T y)
Safely check if addition will overflow.
bool sub_will_underflow(const T x, const T y)
Safely check if subtraction will underflow.
A single point in time, measured in nanoseconds, the reference point is based on the source.
rcl_clock_type_t clock_type
Clock type of the point in time.
rcl_time_point_value_t nanoseconds
Nanoseconds of the point in time.
enum rcl_clock_type_e rcl_clock_type_t
Time source type, used to indicate the source of a time measurement.
rcutils_time_point_value_t rcl_time_point_value_t
A single point in time, measured in nanoseconds since the Unix epoch.
#define RCL_S_TO_NS
Convenience macro to convert seconds to nanoseconds.