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))
69 const builtin_interfaces::msg::Time & time_msg,
71 : rcl_time_(init_time_point(clock_type))
73 if (time_msg.sec < 0) {
74 throw std::runtime_error(
"cannot store a negative time point in rclcpp::Time");
82 : rcl_time_(time_point)
91 Time::operator builtin_interfaces::msg::Time()
const
93 builtin_interfaces::msg::Time msg_time;
95 const auto result = std::div(rcl_time_.nanoseconds, kRemainder);
96 if (result.rem >= 0) {
97 msg_time.sec =
static_cast<std::int32_t
>(result.quot);
98 msg_time.nanosec =
static_cast<std::uint32_t
>(result.rem);
100 msg_time.sec =
static_cast<std::int32_t
>(result.quot - 1);
101 msg_time.nanosec =
static_cast<std::uint32_t
>(kRemainder + result.rem);
112 *
this =
Time(time_msg);
120 throw std::runtime_error(
"can't compare times with different time sources");
129 return !(*
this == rhs);
136 throw std::runtime_error(
"can't compare times with different time sources");
146 throw std::runtime_error(
"can't compare times with different time sources");
156 throw std::runtime_error(
"can't compare times with different time sources");
166 throw std::runtime_error(
"can't compare times with different time sources");
176 throw std::overflow_error(
"addition leads to int64_t overflow");
179 throw std::underflow_error(
"addition leads to int64_t underflow");
188 throw std::runtime_error(
189 std::string(
"can't subtract times with different time sources [") +
190 std::to_string(rcl_time_.
clock_type) +
" != " +
191 std::to_string(rhs.rcl_time_.
clock_type) +
"]");
195 throw std::overflow_error(
"time subtraction leads to int64_t overflow");
199 throw std::underflow_error(
"time subtraction leads to int64_t underflow");
209 throw std::overflow_error(
"time subtraction leads to int64_t overflow");
212 throw std::underflow_error(
"time subtraction leads to int64_t underflow");
227 return std::chrono::duration<double>(std::chrono::nanoseconds(rcl_time_.
nanoseconds)).count();
240 throw std::overflow_error(
"addition leads to int64_t overflow");
243 throw std::underflow_error(
"addition leads to int64_t underflow");
252 throw std::overflow_error(
"addition leads to int64_t overflow");
255 throw std::underflow_error(
"addition leads to int64_t underflow");
267 throw std::overflow_error(
"time subtraction leads to int64_t overflow");
270 throw std::underflow_error(
"time subtraction leads to int64_t underflow");
281 return Time(std::numeric_limits<int32_t>::max(), 999999999);
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)
static RCLCPP_PUBLIC Time max()
Get the maximum representable value.
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
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.
bool add_will_underflow(const T x, const T y)
Safely check if addition will underflow.
RCLCPP_PUBLIC Time operator+(const rclcpp::Duration &lhs, const rclcpp::Time &rhs)
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.