ROS 2 rclcpp + rcl - jazzy  jazzy
ROS 2 C++ Client Library with ROS Client Library
time.cpp
1 // Copyright 2017 Open Source Robotics Foundation, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <limits>
16 #include <string>
17 #include <utility>
18 
19 #include "rclcpp/clock.hpp"
20 #include "rclcpp/duration.hpp"
21 
22 #include "builtin_interfaces/msg/time.hpp"
23 
24 #include "rcl/time.h"
25 
26 #include "rclcpp/exceptions.hpp"
27 
28 #include "rcutils/logging_macros.h"
29 
30 #include "rclcpp/utilities.hpp"
31 
32 namespace
33 {
34 
36 init_time_point(rcl_clock_type_t & clock_type)
37 {
38  rcl_time_point_t time_point;
39  time_point.clock_type = clock_type;
40 
41  return time_point;
42 }
43 
44 } // namespace
45 
46 namespace rclcpp
47 {
48 
49 Time::Time(int32_t seconds, uint32_t nanoseconds, rcl_clock_type_t clock_type)
50 : rcl_time_(init_time_point(clock_type))
51 {
52  if (seconds < 0) {
53  throw std::runtime_error("cannot store a negative time point in rclcpp::Time");
54  }
55 
56  rcl_time_.nanoseconds = RCL_S_TO_NS(static_cast<int64_t>(seconds));
57  rcl_time_.nanoseconds += nanoseconds;
58 }
59 
60 Time::Time(int64_t nanoseconds, rcl_clock_type_t clock_type)
61 : rcl_time_(init_time_point(clock_type))
62 {
63  rcl_time_.nanoseconds = nanoseconds;
64 }
65 
66 Time::Time(const Time & rhs) = default;
67 
68 Time::Time(Time && rhs) noexcept = default;
69 
71  const builtin_interfaces::msg::Time & time_msg,
72  rcl_clock_type_t clock_type)
73 : rcl_time_(init_time_point(clock_type))
74 {
75  if (time_msg.sec < 0) {
76  throw std::runtime_error("cannot store a negative time point in rclcpp::Time");
77  }
78 
79  rcl_time_.nanoseconds = RCL_S_TO_NS(static_cast<int64_t>(time_msg.sec));
80  rcl_time_.nanoseconds += time_msg.nanosec;
81 }
82 
83 Time::Time(const rcl_time_point_t & time_point)
84 : rcl_time_(time_point)
85 {
86  // noop
87 }
88 
89 Time::~Time() = default;
90 
91 Time::operator builtin_interfaces::msg::Time() const
92 {
93  return convert_rcl_time_to_sec_nanos(rcl_time_.nanoseconds);
94 }
95 
96 Time &
97 Time::operator=(const Time & rhs) = default;
98 
99 Time &
100 Time::operator=(const builtin_interfaces::msg::Time & time_msg)
101 {
102  *this = Time(time_msg);
103  return *this;
104 }
105 
106 Time &
107 Time::operator=(Time && rhs) noexcept = default;
108 
109 bool
110 Time::operator==(const rclcpp::Time & rhs) const
111 {
112  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
113  throw std::runtime_error("can't compare times with different time sources");
114  }
115 
116  return rcl_time_.nanoseconds == rhs.rcl_time_.nanoseconds;
117 }
118 
119 bool
120 Time::operator!=(const rclcpp::Time & rhs) const
121 {
122  return !(*this == rhs);
123 }
124 
125 bool
126 Time::operator<(const rclcpp::Time & rhs) const
127 {
128  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
129  throw std::runtime_error("can't compare times with different time sources");
130  }
131 
132  return rcl_time_.nanoseconds < rhs.rcl_time_.nanoseconds;
133 }
134 
135 bool
136 Time::operator<=(const rclcpp::Time & rhs) const
137 {
138  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
139  throw std::runtime_error("can't compare times with different time sources");
140  }
141 
142  return rcl_time_.nanoseconds <= rhs.rcl_time_.nanoseconds;
143 }
144 
145 bool
146 Time::operator>=(const rclcpp::Time & rhs) const
147 {
148  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
149  throw std::runtime_error("can't compare times with different time sources");
150  }
151 
152  return rcl_time_.nanoseconds >= rhs.rcl_time_.nanoseconds;
153 }
154 
155 bool
156 Time::operator>(const rclcpp::Time & rhs) const
157 {
158  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
159  throw std::runtime_error("can't compare times with different time sources");
160  }
161 
162  return rcl_time_.nanoseconds > rhs.rcl_time_.nanoseconds;
163 }
164 
165 Time
167 {
168  if (rclcpp::add_will_overflow(rhs.nanoseconds(), this->nanoseconds())) {
169  throw std::overflow_error("addition leads to int64_t overflow");
170  }
171  if (rclcpp::add_will_underflow(rhs.nanoseconds(), this->nanoseconds())) {
172  throw std::underflow_error("addition leads to int64_t underflow");
173  }
174  return Time(this->nanoseconds() + rhs.nanoseconds(), this->get_clock_type());
175 }
176 
177 Duration
178 Time::operator-(const rclcpp::Time & rhs) const
179 {
180  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
181  throw std::runtime_error(
182  std::string("can't subtract times with different time sources [") +
183  std::to_string(rcl_time_.clock_type) + " != " +
184  std::to_string(rhs.rcl_time_.clock_type) + "]");
185  }
186 
187  if (rclcpp::sub_will_overflow(rcl_time_.nanoseconds, rhs.rcl_time_.nanoseconds)) {
188  throw std::overflow_error("time subtraction leads to int64_t overflow");
189  }
190 
191  if (rclcpp::sub_will_underflow(rcl_time_.nanoseconds, rhs.rcl_time_.nanoseconds)) {
192  throw std::underflow_error("time subtraction leads to int64_t underflow");
193  }
194 
195  return Duration::from_nanoseconds(rcl_time_.nanoseconds - rhs.rcl_time_.nanoseconds);
196 }
197 
198 Time
200 {
201  if (rclcpp::sub_will_overflow(rcl_time_.nanoseconds, rhs.nanoseconds())) {
202  throw std::overflow_error("time subtraction leads to int64_t overflow");
203  }
204  if (rclcpp::sub_will_underflow(rcl_time_.nanoseconds, rhs.nanoseconds())) {
205  throw std::underflow_error("time subtraction leads to int64_t underflow");
206  }
207 
208  return Time(rcl_time_.nanoseconds - rhs.nanoseconds(), rcl_time_.clock_type);
209 }
210 
211 int64_t
213 {
214  return rcl_time_.nanoseconds;
215 }
216 
217 double
219 {
220  return std::chrono::duration<double>(std::chrono::nanoseconds(rcl_time_.nanoseconds)).count();
221 }
222 
225 {
226  return rcl_time_.clock_type;
227 }
228 
229 Time
230 operator+(const rclcpp::Duration & lhs, const rclcpp::Time & rhs)
231 {
233  throw std::overflow_error("addition leads to int64_t overflow");
234  }
236  throw std::underflow_error("addition leads to int64_t underflow");
237  }
238  return Time(lhs.nanoseconds() + rhs.nanoseconds(), rhs.get_clock_type());
239 }
240 
241 Time &
243 {
244  if (rclcpp::add_will_overflow(rhs.nanoseconds(), this->nanoseconds())) {
245  throw std::overflow_error("addition leads to int64_t overflow");
246  }
247  if (rclcpp::add_will_underflow(rhs.nanoseconds(), this->nanoseconds())) {
248  throw std::underflow_error("addition leads to int64_t underflow");
249  }
250 
251  rcl_time_.nanoseconds += rhs.nanoseconds();
252 
253  return *this;
254 }
255 
256 Time &
258 {
259  if (rclcpp::sub_will_overflow(rcl_time_.nanoseconds, rhs.nanoseconds())) {
260  throw std::overflow_error("time subtraction leads to int64_t overflow");
261  }
262  if (rclcpp::sub_will_underflow(rcl_time_.nanoseconds, rhs.nanoseconds())) {
263  throw std::underflow_error("time subtraction leads to int64_t underflow");
264  }
265 
266  rcl_time_.nanoseconds -= rhs.nanoseconds();
267 
268  return *this;
269 }
270 
271 Time
273 {
274  return Time(std::numeric_limits<int32_t>::max(), 999999999, clock_type);
275 }
276 
277 builtin_interfaces::msg::Time
279 {
280  builtin_interfaces::msg::Time ret;
281  constexpr rcl_time_point_value_t kRemainder = RCL_S_TO_NS(1);
282  const auto result = std::div(time_point, kRemainder);
283  if (result.rem >= 0) {
284  ret.sec = static_cast<std::int32_t>(result.quot);
285  ret.nanosec = static_cast<std::uint32_t>(result.rem);
286  } else {
287  ret.sec = static_cast<std::int32_t>(result.quot - 1);
288  ret.nanosec = static_cast<std::uint32_t>(kRemainder + result.rem);
289  }
290  return ret;
291 }
292 
293 } // namespace rclcpp
rcl_duration_value_t nanoseconds() const
Get duration in nanosecods.
Definition: duration.cpp:249
static Duration from_nanoseconds(rcl_duration_value_t nanoseconds)
Create a duration object from an integer number representing nanoseconds.
Definition: duration.cpp:314
RCLCPP_PUBLIC Time & operator-=(const rclcpp::Duration &rhs)
Definition: time.cpp:257
RCLCPP_PUBLIC Time & operator+=(const rclcpp::Duration &rhs)
Definition: time.cpp:242
RCLCPP_PUBLIC bool operator>=(const rclcpp::Time &rhs) const
Definition: time.cpp:146
RCLCPP_PUBLIC double seconds() const
Get the seconds since epoch.
Definition: time.cpp:218
virtual RCLCPP_PUBLIC ~Time()
Time destructor.
RCLCPP_PUBLIC Duration operator-(const rclcpp::Time &rhs) const
Definition: time.cpp:178
RCLCPP_PUBLIC rcl_time_point_value_t nanoseconds() const
Get the nanoseconds since epoch.
Definition: time.cpp:212
RCLCPP_PUBLIC bool operator<(const rclcpp::Time &rhs) const
Definition: time.cpp:126
RCLCPP_PUBLIC Time & operator=(const Time &rhs)
RCLCPP_PUBLIC rcl_clock_type_t get_clock_type() const
Get the clock type.
Definition: time.cpp:224
RCLCPP_PUBLIC bool operator<=(const rclcpp::Time &rhs) const
Definition: time.cpp:136
static RCLCPP_PUBLIC Time max(rcl_clock_type_t clock_type=RCL_SYSTEM_TIME)
Get the maximum representable value.
Definition: time.cpp:272
RCLCPP_PUBLIC bool operator==(const rclcpp::Time &rhs) const
Definition: time.cpp:110
RCLCPP_PUBLIC Time operator+(const rclcpp::Duration &rhs) const
Definition: time.cpp:166
RCLCPP_PUBLIC bool operator>(const rclcpp::Time &rhs) const
Definition: time.cpp:156
RCLCPP_PUBLIC Time(int32_t seconds, uint32_t nanoseconds, rcl_clock_type_t clock_type=RCL_SYSTEM_TIME)
Time constructor.
Definition: time.cpp:49
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.
Definition: time.cpp:278
bool add_will_underflow(const T x, const T y)
Safely check if addition will underflow.
Definition: utilities.hpp:265
bool sub_will_overflow(const T x, const T y)
Safely check if subtraction will overflow.
Definition: utilities.hpp:282
bool add_will_overflow(const T x, const T y)
Safely check if addition will overflow.
Definition: utilities.hpp:248
bool sub_will_underflow(const T x, const T y)
Safely check if subtraction will underflow.
Definition: utilities.hpp:299
A single point in time, measured in nanoseconds, the reference point is based on the source.
Definition: time.h:156
rcl_clock_type_t clock_type
Clock type of the point in time.
Definition: time.h:160
rcl_time_point_value_t nanoseconds
Nanoseconds of the point in time.
Definition: time.h:158
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.
Definition: time.h:46
#define RCL_S_TO_NS
Convenience macro to convert seconds to nanoseconds.
Definition: time.h:32