ROS 2 rclcpp + rcl - humble  humble
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 
69  const builtin_interfaces::msg::Time & time_msg,
70  rcl_clock_type_t clock_type)
71 : rcl_time_(init_time_point(clock_type))
72 {
73  if (time_msg.sec < 0) {
74  throw std::runtime_error("cannot store a negative time point in rclcpp::Time");
75  }
76 
77  rcl_time_.nanoseconds = RCL_S_TO_NS(static_cast<int64_t>(time_msg.sec));
78  rcl_time_.nanoseconds += time_msg.nanosec;
79 }
80 
81 Time::Time(const rcl_time_point_t & time_point)
82 : rcl_time_(time_point)
83 {
84  // noop
85 }
86 
88 {
89 }
90 
91 Time::operator builtin_interfaces::msg::Time() const
92 {
93  builtin_interfaces::msg::Time msg_time;
94  constexpr rcl_time_point_value_t kRemainder = RCL_S_TO_NS(1);
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);
99  } else {
100  msg_time.sec = static_cast<std::int32_t>(result.quot - 1);
101  msg_time.nanosec = static_cast<std::uint32_t>(kRemainder + result.rem);
102  }
103  return msg_time;
104 }
105 
106 Time &
107 Time::operator=(const Time & rhs) = default;
108 
109 Time &
110 Time::operator=(const builtin_interfaces::msg::Time & time_msg)
111 {
112  *this = Time(time_msg);
113  return *this;
114 }
115 
116 bool
117 Time::operator==(const rclcpp::Time & rhs) const
118 {
119  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
120  throw std::runtime_error("can't compare times with different time sources");
121  }
122 
123  return rcl_time_.nanoseconds == rhs.rcl_time_.nanoseconds;
124 }
125 
126 bool
127 Time::operator!=(const rclcpp::Time & rhs) const
128 {
129  return !(*this == rhs);
130 }
131 
132 bool
133 Time::operator<(const rclcpp::Time & rhs) const
134 {
135  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
136  throw std::runtime_error("can't compare times with different time sources");
137  }
138 
139  return rcl_time_.nanoseconds < rhs.rcl_time_.nanoseconds;
140 }
141 
142 bool
143 Time::operator<=(const rclcpp::Time & rhs) const
144 {
145  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
146  throw std::runtime_error("can't compare times with different time sources");
147  }
148 
149  return rcl_time_.nanoseconds <= rhs.rcl_time_.nanoseconds;
150 }
151 
152 bool
153 Time::operator>=(const rclcpp::Time & rhs) const
154 {
155  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
156  throw std::runtime_error("can't compare times with different time sources");
157  }
158 
159  return rcl_time_.nanoseconds >= rhs.rcl_time_.nanoseconds;
160 }
161 
162 bool
163 Time::operator>(const rclcpp::Time & rhs) const
164 {
165  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
166  throw std::runtime_error("can't compare times with different time sources");
167  }
168 
169  return rcl_time_.nanoseconds > rhs.rcl_time_.nanoseconds;
170 }
171 
172 Time
174 {
175  if (rclcpp::add_will_overflow(rhs.nanoseconds(), this->nanoseconds())) {
176  throw std::overflow_error("addition leads to int64_t overflow");
177  }
178  if (rclcpp::add_will_underflow(rhs.nanoseconds(), this->nanoseconds())) {
179  throw std::underflow_error("addition leads to int64_t underflow");
180  }
181  return Time(this->nanoseconds() + rhs.nanoseconds(), this->get_clock_type());
182 }
183 
184 Duration
185 Time::operator-(const rclcpp::Time & rhs) const
186 {
187  if (rcl_time_.clock_type != rhs.rcl_time_.clock_type) {
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) + "]");
192  }
193 
194  if (rclcpp::sub_will_overflow(rcl_time_.nanoseconds, rhs.rcl_time_.nanoseconds)) {
195  throw std::overflow_error("time subtraction leads to int64_t overflow");
196  }
197 
198  if (rclcpp::sub_will_underflow(rcl_time_.nanoseconds, rhs.rcl_time_.nanoseconds)) {
199  throw std::underflow_error("time subtraction leads to int64_t underflow");
200  }
201 
202  return Duration::from_nanoseconds(rcl_time_.nanoseconds - rhs.rcl_time_.nanoseconds);
203 }
204 
205 Time
207 {
208  if (rclcpp::sub_will_overflow(rcl_time_.nanoseconds, rhs.nanoseconds())) {
209  throw std::overflow_error("time subtraction leads to int64_t overflow");
210  }
211  if (rclcpp::sub_will_underflow(rcl_time_.nanoseconds, rhs.nanoseconds())) {
212  throw std::underflow_error("time subtraction leads to int64_t underflow");
213  }
214 
215  return Time(rcl_time_.nanoseconds - rhs.nanoseconds(), rcl_time_.clock_type);
216 }
217 
218 int64_t
220 {
221  return rcl_time_.nanoseconds;
222 }
223 
224 double
226 {
227  return std::chrono::duration<double>(std::chrono::nanoseconds(rcl_time_.nanoseconds)).count();
228 }
229 
232 {
233  return rcl_time_.clock_type;
234 }
235 
236 Time
237 operator+(const rclcpp::Duration & lhs, const rclcpp::Time & rhs)
238 {
240  throw std::overflow_error("addition leads to int64_t overflow");
241  }
243  throw std::underflow_error("addition leads to int64_t underflow");
244  }
245  return Time(lhs.nanoseconds() + rhs.nanoseconds(), rhs.get_clock_type());
246 }
247 
248 Time &
250 {
251  if (rclcpp::add_will_overflow(rhs.nanoseconds(), this->nanoseconds())) {
252  throw std::overflow_error("addition leads to int64_t overflow");
253  }
254  if (rclcpp::add_will_underflow(rhs.nanoseconds(), this->nanoseconds())) {
255  throw std::underflow_error("addition leads to int64_t underflow");
256  }
257 
258  rcl_time_.nanoseconds += rhs.nanoseconds();
259 
260  return *this;
261 }
262 
263 Time &
265 {
266  if (rclcpp::sub_will_overflow(rcl_time_.nanoseconds, rhs.nanoseconds())) {
267  throw std::overflow_error("time subtraction leads to int64_t overflow");
268  }
269  if (rclcpp::sub_will_underflow(rcl_time_.nanoseconds, rhs.nanoseconds())) {
270  throw std::underflow_error("time subtraction leads to int64_t underflow");
271  }
272 
273  rcl_time_.nanoseconds -= rhs.nanoseconds();
274 
275  return *this;
276 }
277 
278 Time
280 {
281  return Time(std::numeric_limits<int32_t>::max(), 999999999);
282 }
283 
284 
285 } // namespace rclcpp
rcl_duration_value_t nanoseconds() const
Get duration in nanosecods.
Definition: duration.cpp:226
static Duration from_nanoseconds(rcl_duration_value_t nanoseconds)
Create a duration object from an integer number representing nanoseconds.
Definition: duration.cpp:291
RCLCPP_PUBLIC Time & operator-=(const rclcpp::Duration &rhs)
Definition: time.cpp:264
static RCLCPP_PUBLIC Time max()
Get the maximum representable value.
Definition: time.cpp:279
RCLCPP_PUBLIC Time & operator+=(const rclcpp::Duration &rhs)
Definition: time.cpp:249
RCLCPP_PUBLIC bool operator>=(const rclcpp::Time &rhs) const
Definition: time.cpp:153
RCLCPP_PUBLIC double seconds() const
Get the seconds since epoch.
Definition: time.cpp:225
virtual RCLCPP_PUBLIC ~Time()
Time destructor.
Definition: time.cpp:87
RCLCPP_PUBLIC Duration operator-(const rclcpp::Time &rhs) const
Definition: time.cpp:185
RCLCPP_PUBLIC rcl_time_point_value_t nanoseconds() const
Get the nanoseconds since epoch.
Definition: time.cpp:219
RCLCPP_PUBLIC bool operator<(const rclcpp::Time &rhs) const
Definition: time.cpp:133
RCLCPP_PUBLIC Time & operator=(const Time &rhs)
RCLCPP_PUBLIC rcl_clock_type_t get_clock_type() const
Get the clock type.
Definition: time.cpp:231
RCLCPP_PUBLIC bool operator<=(const rclcpp::Time &rhs) const
Definition: time.cpp:143
RCLCPP_PUBLIC bool operator==(const rclcpp::Time &rhs) const
Definition: time.cpp:117
RCLCPP_PUBLIC Time operator+(const rclcpp::Duration &rhs) const
Definition: time.cpp:173
RCLCPP_PUBLIC bool operator>(const rclcpp::Time &rhs) const
Definition: time.cpp:163
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.
bool add_will_underflow(const T x, const T y)
Safely check if addition will underflow.
Definition: utilities.hpp:265
RCLCPP_PUBLIC Time operator+(const rclcpp::Duration &lhs, const rclcpp::Time &rhs)
Definition: time.cpp:237
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