time_utils.hpp
Go to the documentation of this file.
1 
9 #pragma once
10 
11 #include <ctime>
12 #include <string>
13 
15 #include <ros/duration.h>
16 #include <ros/rate.h>
17 #include <ros/time.h>
18 
19 namespace cras
20 {
21 
22 template<typename T, typename ::std::enable_if_t<
23  ::std::is_same<T, ::ros::Time>::value ||
24  ::std::is_same<T, ::ros::WallTime>::value ||
25  ::std::is_same<T, ::ros::SteadyTime>::value
26  >* = nullptr>
28 {
29 };
30 
31 template<> struct DurationType<ros::Time>
32 {
33  typedef ::ros::Duration value;
34 };
35 
36 template<> struct DurationType<ros::WallTime>
37 {
38  typedef ::ros::WallDuration value;
39 };
40 
41 template<> struct DurationType<ros::SteadyTime>
42 {
43  typedef ::ros::WallDuration value;
44 };
45 
46 template<
47  typename T1,
48  typename T2,
49  typename ::std::enable_if_t<
50  ::std::is_same<T1, ::ros::Time>::value ||
51  ::std::is_same<T1, ::ros::WallTime>::value ||
52  ::std::is_same<T1, ::ros::SteadyTime>::value>* = nullptr,
53  typename ::std::enable_if_t<
54  ::std::is_same<T2, ::ros::Time>::value ||
55  ::std::is_same<T2, ::ros::WallTime>::value ||
56  ::std::is_same<T2, ::ros::SteadyTime>::value>* = nullptr
57 >
58 inline T1 convertTime(const T2& time)
59 {
60  return T1{time.sec, time.nsec};
61 }
62 
63 template<
64  typename D1,
65  typename D2,
66  typename ::std::enable_if_t<
67  ::std::is_same<D1, ::ros::Duration>::value ||
68  ::std::is_same<D1, ::ros::WallDuration>::value>* = nullptr,
69  typename ::std::enable_if_t<
70  ::std::is_same<D2, ::ros::Duration>::value ||
71  ::std::is_same<D2, ::ros::WallDuration>::value>* = nullptr
72 >
73 inline D1 convertDuration(const D2& duration)
74 {
75  return D1{duration.sec, duration.nsec};
76 }
77 
84 ::ros::Duration remainingTime(const ::ros::Time& query, double timeout);
85 
92 ::ros::Duration remainingTime(const ::ros::Time& query, const ::ros::Duration& timeout);
93 
100 double frequency(const ::ros::Rate& rate, bool maxCycleTimeMeansZero = false);
101 
108 double frequency(const ::ros::WallRate& rate, bool maxCycleTimeMeansZero = false);
109 
117 
125 
131 
138 ::ros::Time saturateAdd(const ::ros::Time& time, const ::ros::Duration& duration);
139 
146 ::ros::WallTime saturateAdd(const ::ros::WallTime& time, const ::ros::WallDuration& duration);
147 
154 ::ros::SteadyTime saturateAdd(const ::ros::SteadyTime& time, const ::ros::WallDuration& duration);
155 
162 ::tm toStructTm(const ::ros::Time& time);
163 
170 inline ::tm toStructTm(const ::ros::WallTime& time)
171 {
172  return ::cras::toStructTm(::cras::convertTime<::ros::Time>(time));
173 }
174 
181 inline ::tm toStructTm(const ::ros::SteadyTime& time)
182 {
183  return ::cras::toStructTm(::cras::convertTime<::ros::Time>(time));
184 }
185 
186 ::cras::expected<::ros::Time, ::std::string> fromStructTm(const ::tm& time);
187 
193 int getYear(const ::ros::Time& time);
194 
200 inline int getYear(const ::ros::WallTime& time)
201 {
202  return ::cras::getYear(::cras::convertTime<::ros::Time>(time));
203 }
204 
210 inline int getYear(const ::ros::SteadyTime& time)
211 {
212  return ::cras::getYear(::cras::convertTime<::ros::Time>(time));
213 }
214 
215 }
216 
217 namespace ros
218 {
219 
226 bool operator==(const ::ros::Rate& r1, const ::ros::Rate& r2);
227 
234 bool operator==(const ::ros::WallRate& r1, const ::ros::WallRate& r2);
235 
243 ::ros::Duration operator*(const ::ros::Duration& val1, const ::ros::Duration& val2);
244 
252 ::ros::Duration operator/(const ::ros::Duration& numerator, const ::ros::Duration& denominator);
253 
261 ::ros::WallDuration operator*(const ::ros::WallDuration& val1, const ::ros::WallDuration& val2);
262 
270 ::ros::WallDuration operator/(const ::ros::WallDuration& numerator, const ::ros::WallDuration& denominator);
271 
272 }
cras::fromStructTm
::cras::expected<::ros::Time, ::std::string > fromStructTm(const ::tm &time)
cras
Definition: any.hpp:15
cras::getYear
int getYear(const ::ros::SteadyTime &time)
Get the year represented by the given ROS time when interpreted as UTC time.
Definition: time_utils.hpp:210
ros
time.h
cras::saturateAdd
::ros::Time saturateAdd(const ::ros::Time &time, const ::ros::Duration &duration)
Add the given duration to the given time, but saturate the result instead of throwing exception on ov...
ros::operator==
bool operator==(const ::ros::Rate &r1, const ::ros::Rate &r2)
Test two rates for equality.
ros::operator*
::ros::Duration operator*(const ::ros::Duration &val1, const ::ros::Duration &val2)
Multiply two durations. The units of the result are [s^2].
cras::toStructTm
inline ::tm toStructTm(const ::ros::SteadyTime &time)
Convert the given ROS time to C tm struct representing UTC time.
Definition: time_utils.hpp:181
cras::DurationType< ros::Time >::value
::ros::Duration value
Definition: time_utils.hpp:33
cras::toStructTm
::tm toStructTm(const ::ros::Time &time)
Convert the given ROS time to C tm struct representing UTC time.
cras::DurationType< ros::WallTime >::value
::ros::WallDuration value
Definition: time_utils.hpp:38
cras::frequency
double frequency(const ::ros::Rate &rate, bool maxCycleTimeMeansZero=false)
Return the frequency represented by the given rate.
duration.h
cras::DurationType< ros::SteadyTime >::value
::ros::WallDuration value
Definition: time_utils.hpp:43
ros::SteadyTime
ros::WallRate
cras::convertTime
T1 convertTime(const T2 &time)
Definition: time_utils.hpp:58
cras::getYear
int getYear(const ::ros::Time &time)
Get the year represented by the given ROS time when interpreted as UTC time.
ros::operator/
::ros::Duration operator/(const ::ros::Duration &numerator, const ::ros::Duration &denominator)
Divide duration val1 by val2. The result is unitless.
rate.h
ros::WallTime
cras::safeRate
::ros::Rate safeRate(double frequency)
Return a rate representing the given frequency. If the frequency is zero or too small,...
ros::Time
cras::convertDuration
D1 convertDuration(const D2 &duration)
Definition: time_utils.hpp:73
cras::DurationType
Definition: time_utils.hpp:27
cras::safeWallRate
::ros::WallRate safeWallRate(double frequency)
Return a rate representing the given frequency. If the frequency is zero or too small,...
ros::Rate
expected.hpp
An implementation of the std::expected proposal.
ros::WallDuration
cras::remainingTime
::ros::Duration remainingTime(const ::ros::Time &query, double timeout)
Return remaining time to timeout from the query time.
ros::Duration
cras::nowFallbackToWall
::ros::Time nowFallbackToWall()
Return current ROS time if it has already been initialized, or current wall time.


cras_cpp_common
Author(s): Martin Pecka
autogenerated on Tue Nov 26 2024 03:49:04