OpenDDS  Snapshot(2023/04/28-20:55)
Time_Helper.inl
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #include "ace/OS_NS_string.h"
9 #include "ace/Truncate.h"
10 
11 #include <cstring>
12 
14 
15 namespace OpenDDS {
16 namespace DCPS {
17 
18 // These operators are used in some inline functions below. Some
19 // compilers require the inline definition to appear before its use.
20 #ifndef OPENDDS_SAFETY_PROFILE
22 bool operator==(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
23 {
24  return t1.sec == t2.sec && t1.nanosec == t2.nanosec;
25 }
26 
28 bool operator!=(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
29 {
30  return !(t1 == t2);
31 }
32 #endif
33 
35 bool operator<(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
36 {
37  // @note We wouldn't have to handle the case for INFINITY explicitly
38  // if both the Duration_t sec and nanosec fields were the
39  // maximum values for their corresponding types.
40  // Unfortunately, the OMG DDS specification defines the
41  // infinite nanosec value to be somewhere in the middle.
42 
43  // We assume that either both the DDS::Duration_t::sec and
44  // DDS::Duration_t::nanosec fields are INFINITY or neither of them
45  // are. It doesn't make sense for only one of the fields to be
46  // INFINITY.
47  return
48  !is_infinite(t1)
49  && (is_infinite(t2)
50  || t1.sec < t2.sec
51  || (t1.sec == t2.sec && t1.nanosec < t2.nanosec));
52 }
53 
55 bool operator<=(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
56 {
57  // If t2 is *not* less than t1, t1 must be less than
58  // or equal to t2.
59  // This is more concise than:
60  // t1 < t2 || t1 == t2
61  return !(t2 < t1);
62 }
63 
65 bool operator>(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
66 {
67  return t2 < t1;
68 }
69 
71 bool operator>=(const DDS::Duration_t& t1, const DDS::Duration_t& t2)
72 {
73  return t2 <= t1;
74 }
75 
77 bool operator!(const DDS::Time_t& t)
78 {
79  return t.sec == DDS::TIME_INVALID_SEC
81 }
82 
83 #ifndef OPENDDS_SAFETY_PROFILE
84 ACE_INLINE bool
85 operator==(const DDS::Time_t& t1, const DDS::Time_t& t2)
86 {
87  return !(t1 < t2) && !(t2 < t1);
88 }
89 
90 ACE_INLINE bool
91 operator!=(const DDS::Time_t& t1, const DDS::Time_t& t2)
92 {
93  return !(t1 == t2);
94 }
95 #endif
96 
97 ACE_INLINE bool
98 operator<(const DDS::Time_t& t1, const DDS::Time_t& t2)
99 {
100  if (!t1 || !t2) return false;
101 
102  return t1.sec < t2.sec
103  || (t1.sec == t2.sec && t1.nanosec < t2.nanosec);
104 }
105 
106 ACE_INLINE bool
107 operator<=(const DDS::Time_t& t1, const DDS::Time_t& t2)
108 {
109  return !(t2 < t1);
110 }
111 
112 ACE_INLINE bool
113 operator>(const DDS::Time_t& t1, const DDS::Time_t& t2)
114 {
115  return t2 < t1;
116 }
117 
118 ACE_INLINE bool
119 operator>=(const DDS::Time_t& t1, const DDS::Time_t& t2)
120 {
121  return t2 <= t1;
122 }
123 
125 operator+(const DDS::Time_t& t1, const DDS::Duration_t& d1)
126 {
127  CORBA::Long sec = static_cast<CORBA::Long>(static_cast<CORBA::ULong>(t1.sec) + static_cast<CORBA::ULong>(d1.sec));
128  CORBA::ULong nanosec = t1.nanosec + d1.nanosec;
129 
130  while (nanosec >= ACE_ONE_SECOND_IN_NSECS) {
131  ++sec;
132  nanosec -= ACE_ONE_SECOND_IN_NSECS;
133  }
134 
135  const DDS::Time_t t = { sec, nanosec };
136  return t;
137 }
138 
140 operator-(const DDS::Time_t& t1, const DDS::Time_t& t2)
141 {
142  DDS::Duration_t t = { t1.sec - t2.sec, t1.nanosec - t2.nanosec };
143 
144  if (t2.nanosec > t1.nanosec) {
146  t.sec = (t1.sec - 1) - t2.sec;
147  }
148 
149  return t;
150 }
151 
153 operator-(const DDS::Time_t& t1, const DDS::Duration_t& t2)
154 {
155  DDS::Time_t t = { t1.sec - t2.sec, t1.nanosec - t2.nanosec };
156 
157  if (t2.nanosec > t1.nanosec) {
159  t.sec = (t1.sec - 1) - t2.sec;
160  }
161 
162  return t;
163 }
164 
167 {
168  DDS::Duration_t t = { t1.sec - t2.sec, t1.nanosec - t2.nanosec };
169 
170  if (t2.nanosec > t1.nanosec) {
172  t.sec = (t1.sec - 1) - t2.sec;
173  }
174 
175  return t;
176 }
177 
178 ACE_INLINE bool
180 {
181  return t1.sec < t2.sec || (t1.sec == t2.sec && t1.nanosec < t2.nanosec);
182 }
183 
184 #ifndef OPENDDS_SAFETY_PROFILE
185 ACE_INLINE bool
187 {
188  return t1.sec == t2.sec && t1.nanosec == t2.nanosec;
189 }
190 #endif
191 
194 {
195  ACE_Time_Value tv(t.sec, t.nanosec / 1000);
196  return tv;
197 }
198 
201 {
202  DDS::Time_t t;
205  return t;
206 }
207 
210 {
211  MonotonicTime_t t;
214  return t;
215 }
216 
219 {
220  if (is_infinite(t)) {
222  }
223 
226 
227  if (sec > ACE_Time_Value::max_time.sec()) {
229  }
230  else {
231  return ACE_Time_Value(ACE_Utils::truncate_cast<time_t>(sec), usec);
232  }
233 }
234 
237  const ACE_Time_Value& now)
238 {
239  CORBA::LongLong sec
240  = t.sec + now.sec() + (t.nanosec/1000 + now.usec())/ACE_ONE_SECOND_IN_USECS;
241  CORBA::ULong usec = (t.nanosec/1000 + now.usec()) % ACE_ONE_SECOND_IN_USECS;
242 
243  if (sec > ACE_Time_Value::max_time.sec()) {
245  }
246  else {
247  return ACE_Time_Value(ACE_Utils::truncate_cast<time_t>(sec), usec);
248  }
249 }
250 
253 {
254  DDS::Duration_t t;
257  return t;
258 }
259 
262 {
263  DDS::Duration_t d = { t.sec, t.nanosec };
264  return d;
265 }
266 
269 {
270  // Only accept infinite or positive finite durations. (Zero
271  // excluded).
272  //
273  // Note that it doesn't make much sense for users to set
274  // durations less than 10 milliseconds since the underlying
275  // timer resolution is generally no better than that.
276  return is_infinite(t) || t.sec > 0 || (t.sec >= 0 && t.nanosec > 0);
277 }
278 
281 {
282  return
283  (t.sec == DDS::DURATION_ZERO_SEC // Allow zero duration.
285  || valid_duration(t);
286 }
287 
289 ACE_UINT32 uint32_fractional_seconds_to_nanoseconds(ACE_UINT32 fraction)
290 {
291  return static_cast<ACE_UINT32>((static_cast<ACE_UINT64>(fraction) * 1000000000) >> 32);
292 }
293 
295 ACE_UINT32 nanoseconds_to_uint32_fractional_seconds(ACE_UINT32 nsec)
296 {
297  return static_cast<ACE_UINT32>((static_cast<ACE_UINT64>(nsec) << 32) / 1000000000);
298 }
299 
301 ACE_UINT32 uint32_fractional_seconds_to_microseconds(ACE_UINT32 fraction)
302 {
303  return static_cast<ACE_UINT32>((static_cast<ACE_UINT64>(fraction) * 1000000) >> 32);
304 }
305 
307 ACE_UINT32 microseconds_to_uint32_fractional_seconds(ACE_UINT32 usec)
308 {
309  return static_cast<ACE_UINT32>((static_cast<ACE_UINT64>(usec) << 32) / 1000000);
310 }
311 
313 {
314  return value.sec == DDS::DURATION_INFINITE_SEC &&
316 }
317 
320 {
321  static const MonotonicTime_t zero = { 0, 0 };
322  return zero;
323 }
324 
326 DDS::Duration_t make_duration_t(int sec, unsigned long nanosec)
327 {
328  DDS::Duration_t x;
329  x.sec = sec;
330  x.nanosec = nanosec;
331  return x;
332 }
333 
335 DDS::Time_t make_time_t(int sec, unsigned long nanosec)
336 {
337  DDS::Time_t x;
338  x.sec = sec;
339  x.nanosec = nanosec;
340  return x;
341 }
342 
343 } // namespace DCPS
344 } // namespace OpenDDS
345 
#define ACE_ONE_SECOND_IN_NSECS
ACE_CDR::Long Long
const LogLevel::Value value
Definition: debug.cpp:61
ACE_INLINE OpenDDS_Dcps_Export ACE_UINT32 uint32_fractional_seconds_to_microseconds(ACE_UINT32 fraction)
static const ACE_Time_Value max_time
ACE_INLINE OpenDDS_Dcps_Export DDS::Duration_t operator-(const DDS::Time_t &t1, const DDS::Time_t &t2)
ACE_INLINE OpenDDS_Dcps_Export bool operator!(const DDS::Time_t &t)
Definition: Time_Helper.inl:77
ACE_INLINE OpenDDS_Dcps_Export DDS::Time_t time_value_to_time(const ACE_Time_Value &tv)
const long DURATION_INFINITE_SEC
Definition: DdsDcpsCore.idl:72
ACE_INLINE OpenDDS_Dcps_Export bool non_negative_duration(const DDS::Duration_t &t)
ACE_CDR::LongLong LongLong
#define OpenDDS_Dcps_Export
Definition: dcps_export.h:24
ACE_INLINE OpenDDS_Dcps_Export bool operator>(const DDS::Duration_t &t1, const DDS::Duration_t &t2)
Definition: Time_Helper.inl:65
const long TIME_INVALID_SEC
bool operator==(const DisjointSequence::OrderedRanges< T > &a, const DisjointSequence::OrderedRanges< T > &b)
ACE_INLINE OpenDDS_Dcps_Export ACE_UINT32 uint32_fractional_seconds_to_nanoseconds(ACE_UINT32 fraction)
TO truncate_cast(FROM val)
ACE_INLINE OpenDDS_Dcps_Export bool operator<=(const DDS::Duration_t &t1, const DDS::Duration_t &t2)
Definition: Time_Helper.inl:55
ACE_CDR::ULong ULong
unsigned long nanosec
Definition: DdsDcpsCore.idl:69
ACE_INLINE OpenDDS_Dcps_Export ACE_UINT32 nanoseconds_to_uint32_fractional_seconds(ACE_UINT32 fraction)
ACE_INLINE OpenDDS_Dcps_Export bool valid_duration(DDS::Duration_t const &t)
ACE_INLINE OpenDDS_Dcps_Export DDS::Duration_t make_duration_t(int sec, unsigned long nanosec)
const unsigned long DURATION_ZERO_NSEC
Definition: DdsDcpsCore.idl:76
ACE_INLINE OpenDDS_Dcps_Export ACE_Time_Value time_to_time_value(const DDS::Time_t &t)
ACE_INLINE OpenDDS_Dcps_Export const MonotonicTime_t & monotonic_time_zero()
ACE_INLINE OpenDDS_Dcps_Export ACE_UINT32 microseconds_to_uint32_fractional_seconds(ACE_UINT32 fraction)
ACE_INLINE OpenDDS_Dcps_Export DDS::Duration_t time_value_to_duration(const ACE_Time_Value &tv)
bool operator>=(const LogLevel &ll, LogLevel::Value value)
Definition: debug.h:61
time_t sec(void) const
SequenceNumber operator+(const SequenceNumber &lhs, int rhs)
ACE_INLINE OpenDDS_Dcps_Export ACE_Time_Value duration_to_time_value(const DDS::Duration_t &t)
const unsigned long DURATION_INFINITE_NSEC
Definition: DdsDcpsCore.idl:73
bool operator!=(const GUID_t &lhs, const GUID_t &rhs)
Definition: GuidUtils.h:125
const unsigned long TIME_INVALID_NSEC
ACE_INLINE OpenDDS_Dcps_Export DDS::Time_t make_time_t(int sec, unsigned long nanosec)
unsigned long long ACE_UINT64
unsigned long nanosec
suseconds_t usec(void) const
ACE_INLINE OpenDDS_Dcps_Export bool is_infinite(const DDS::Duration_t &value)
ACE_INLINE OpenDDS_Dcps_Export MonotonicTime_t time_value_to_monotonic_time(const ACE_Time_Value &tv)
const long DURATION_ZERO_SEC
Definition: DdsDcpsCore.idl:75
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
suseconds_t const ACE_ONE_SECOND_IN_USECS
ACE_INLINE OpenDDS_Dcps_Export DDS::Duration_t time_to_duration(const DDS::Time_t &t)
ACE_INLINE OpenDDS_Dcps_Export ACE_Time_Value duration_to_absolute_time_value(const DDS::Duration_t &t, const ACE_Time_Value &now)
#define ACE_INLINE
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
bool operator<(const GUID_t &lhs, const GUID_t &rhs)
Definition: GuidUtils.h:80