| Fast DDS Version 3.3.0 Fast DDS

Loading...
Searching...
No Matches
Time_t.hpp
1// Copyright 2024 Proyectos y Sistemas de Mantenimiento SL (eProsima).
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
18
19#ifndef FASTDDS_DDS_CORE__TIME_T_HPP
20#define FASTDDS_DDS_CORE__TIME_T_HPP
21
22#include <fastdds/fastdds_dll.hpp>
23
24#include <cmath>
25#include <cstdint>
26#include <iostream>
27
28 namespace eprosima {
29 namespace fastdds {
30 namespace dds {
31
35 struct FASTDDS_EXPORTED_API Time_t
36{
37 static constexpr int32_t INFINITE_SECONDS = 0x7fffffff;
38 static constexpr uint32_t INFINITE_NANOSECONDS = 0xffffffffu;
39
40 int32_t seconds;
41 uint32_t nanosec;
42
44 Time_t();
45
50 Time_t(
51 int32_t sec,
52 uint32_t nsec);
53
57 Time_t(
58 long double sec);
59
60 void fraction(
61 uint32_t frac);
62
63 uint32_t fraction() const;
64
68 int64_t to_ns() const;
69
70 inline bool is_infinite() const noexcept
71 {
72 return is_infinite(*this);
73 }
74
80 static void now(
81 Time_t& ret);
82
83 static inline constexpr bool is_infinite(
84 const Time_t& t) noexcept
85 {
86 return (INFINITE_SECONDS == t.seconds) || (INFINITE_NANOSECONDS == t.nanosec);
87 }
88
89};
90
91 using Duration_t = Time_t;
92
93#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
94
101 static inline bool operator ==(
102 const Time_t& t1,
103 const Time_t& t2)
104{
105 if (t1.seconds != t2.seconds)
106 {
107 return false;
108 }
109 if (t1.nanosec != t2.nanosec)
110 {
111 return false;
112 }
113 return true;
114}
115
122 static inline bool operator !=(
123 const Time_t& t1,
124 const Time_t& t2)
125{
126 if (t1.seconds != t2.seconds)
127 {
128 return true;
129 }
130 if (t1.nanosec != t2.nanosec)
131 {
132 return true;
133 }
134 return false;
135}
136
143 static inline bool operator <(
144 const Time_t& t1,
145 const Time_t& t2)
146{
147 if (t1.seconds < t2.seconds)
148 {
149 return true;
150 }
151 else if (t1.seconds > t2.seconds)
152 {
153 return false;
154 }
155 else
156 {
157 if (t1.nanosec < t2.nanosec)
158 {
159 return true;
160 }
161 else
162 {
163 return false;
164 }
165 }
166}
167
174 static inline bool operator >(
175 const Time_t& t1,
176 const Time_t& t2)
177{
178 if (t1.seconds > t2.seconds)
179 {
180 return true;
181 }
182 else if (t1.seconds < t2.seconds)
183 {
184 return false;
185 }
186 else
187 {
188 if (t1.nanosec > t2.nanosec)
189 {
190 return true;
191 }
192 else
193 {
194 return false;
195 }
196 }
197}
198
205 static inline bool operator <=(
206 const Time_t& t1,
207 const Time_t& t2)
208{
209 if (t1.seconds < t2.seconds)
210 {
211 return true;
212 }
213 else if (t1.seconds > t2.seconds)
214 {
215 return false;
216 }
217 else
218 {
219 if (t1.nanosec <= t2.nanosec)
220 {
221 return true;
222 }
223 else
224 {
225 return false;
226 }
227 }
228}
229
236 static inline bool operator >=(
237 const Time_t& t1,
238 const Time_t& t2)
239{
240 if (t1.seconds > t2.seconds)
241 {
242 return true;
243 }
244 else if (t1.seconds < t2.seconds)
245 {
246 return false;
247 }
248 else
249 {
250 if (t1.nanosec >= t2.nanosec)
251 {
252 return true;
253 }
254 else
255 {
256 return false;
257 }
258 }
259}
260
261 inline std::ostream& operator <<(
262 std::ostream& output,
263 const Time_t& t)
264{
265 long double t_aux = t.seconds + (((long double)t.nanosec) / 1000000000ULL);
266 return output << t_aux;
267}
268
275 static inline Time_t operator +(
276 const Time_t& ta,
277 const Time_t& tb)
278{
279 Time_t result(ta.seconds + tb.seconds, ta.nanosec + tb.nanosec);
280 if (result.nanosec < ta.nanosec) // Overflow is detected by any of them
281 {
282 ++result.seconds;
283 }
284 return result;
285}
286
293 static inline Time_t operator -(
294 const Time_t& ta,
295 const Time_t& tb)
296{
297 Time_t result(ta.seconds - tb.seconds, ta.nanosec - tb.nanosec);
298 if (result.nanosec > ta.nanosec) // Overflow is detected by ta
299 {
300 --result.seconds;
301 }
302 return result;
303}
304
305#endif // ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
306
308 const Time_t c_TimeInfinite{Time_t::INFINITE_SECONDS, Time_t::INFINITE_NANOSECONDS};
310 const Time_t c_TimeZero{0, 0};
312 const Time_t c_TimeInvalid{-1, Time_t::INFINITE_NANOSECONDS};
313
314} // namespace dds
315} // namespace fastdds
316} // namespace eprosima
317
318#endif // FASTDDS_DDS_CORE__TIME_T_HPP
eprosima::fastdds::dds
Definition BuiltinTopicKey.hpp:26
eprosima::fastdds::dds::c_TimeZero
const Time_t c_TimeZero
Time_t (dds::Duration_t) representing a zero time. DONT USE IT IN CONSTRUCTORS.
Definition Time_t.hpp:310
eprosima::fastdds::dds::c_TimeInvalid
const Time_t c_TimeInvalid
Time_t (dds::Duration_t) representing an invalid time. DONT USE IT IN CONSTRUCTORS.
Definition Time_t.hpp:312
eprosima::fastdds::dds::operator<=
static bool operator<=(const Time_t &t1, const Time_t &t2)
Checks if a Time_t is less or equal than other.
Definition Time_t.hpp:205
eprosima::fastdds::dds::c_TimeInfinite
const Time_t c_TimeInfinite
Time_t (dds::Duration_t) representing an infinite time. DONT USE IT IN CONSTRUCTORS.
Definition Time_t.hpp:308
eprosima::fastdds::dds::operator<<
std::ostream & operator<<(std::ostream &output, const Time_t &t)
Definition Time_t.hpp:261
eprosima::fastdds::dds::operator!=
static bool operator!=(const Time_t &t1, const Time_t &t2)
Comparison assignment.
Definition Time_t.hpp:122
eprosima::fastdds::dds::operator+
static Time_t operator+(const Time_t &ta, const Time_t &tb)
Adds two Time_t.
Definition Time_t.hpp:275
eprosima::fastdds::dds::operator-
static Time_t operator-(const Time_t &ta, const Time_t &tb)
Subtracts two Time_t.
Definition Time_t.hpp:293
eprosima::fastdds::dds::operator==
static bool operator==(const Time_t &t1, const Time_t &t2)
Comparison assignment.
Definition Time_t.hpp:101
eprosima::fastdds::dds::operator>=
static bool operator>=(const Time_t &t1, const Time_t &t2)
Checks if a Time_t is greater or equal than other.
Definition Time_t.hpp:236
eprosima::fastdds::dds::operator>
static bool operator>(const Time_t &t1, const Time_t &t2)
Checks if a Time_t is greater than other.
Definition Time_t.hpp:174
eprosima::fastdds::dds::Duration_t
Time_t Duration_t
Definition Time_t.hpp:91
eprosima::fastdds::dds::operator<
static bool operator<(const Time_t &t1, const Time_t &t2)
Checks if a Time_t is less than other.
Definition Time_t.hpp:143
eprosima::fastdds
eprosima
eProsima namespace.
eprosima::fastdds::dds::Time_t
Structure Time_t, used to describe times at a DDS level.
Definition Time_t.hpp:36
eprosima::fastdds::dds::Time_t::Time_t
Time_t(long double sec)
eprosima::fastdds::dds::Time_t::seconds
int32_t seconds
Definition Time_t.hpp:40
eprosima::fastdds::dds::Time_t::fraction
void fraction(uint32_t frac)
eprosima::fastdds::dds::Time_t::is_infinite
bool is_infinite() const noexcept
Definition Time_t.hpp:70
eprosima::fastdds::dds::Time_t::INFINITE_SECONDS
static constexpr int32_t INFINITE_SECONDS
Definition Time_t.hpp:37
eprosima::fastdds::dds::Time_t::now
static void now(Time_t &ret)
Fills a Time_t struct with a representation of the current time.
eprosima::fastdds::dds::Time_t::is_infinite
static constexpr bool is_infinite(const Time_t &t) noexcept
Definition Time_t.hpp:83
eprosima::fastdds::dds::Time_t::fraction
uint32_t fraction() const
eprosima::fastdds::dds::Time_t::Time_t
Time_t()
Default constructor. Sets values to zero.
eprosima::fastdds::dds::Time_t::INFINITE_NANOSECONDS
static constexpr uint32_t INFINITE_NANOSECONDS
Definition Time_t.hpp:38
eprosima::fastdds::dds::Time_t::nanosec
uint32_t nanosec
Definition Time_t.hpp:41
eprosima::fastdds::dds::Time_t::to_ns
int64_t to_ns() const
Returns stored time as nanoseconds (including seconds).
eprosima::fastdds::dds::Time_t::Time_t
Time_t(int32_t sec, uint32_t nsec)