Fast DDS  Version 3.0.1
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
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
28namespace eprosima {
29namespace fastdds {
30namespace dds {
31
35struct 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
45
51 int32_t sec,
52 uint32_t nsec);
53
58 long double sec);
59
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
92
93#ifndef DOXYGEN_SHOULD_SKIP_THIS_PUBLIC
94
101static 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
122static 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
143static 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
174static 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
205static 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
236static 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
261inline 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
275static 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
293static 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
310const Time_t c_TimeZero{0, 0};
313
314} // namespace dds
315} // namespace fastdds
316} // namespace eprosima
317
318#endif // FASTDDS_DDS_CORE__TIME_T_HPP
Definition DomainParticipant.hpp:45
const Time_t c_TimeZero
Time_t (dds::Duration_t) representing a zero time. DONT USE IT IN CONSTRUCTORS.
Definition Time_t.hpp:310
const Time_t c_TimeInvalid
Time_t (dds::Duration_t) representing an invalid time. DONT USE IT IN CONSTRUCTORS.
Definition Time_t.hpp:312
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
const Time_t c_TimeInfinite
Time_t (dds::Duration_t) representing an infinite time. DONT USE IT IN CONSTRUCTORS.
Definition Time_t.hpp:308
std::ostream & operator<<(std::ostream &output, const Time_t &t)
Definition Time_t.hpp:261
static bool operator!=(const Time_t &t1, const Time_t &t2)
Comparison assignment.
Definition Time_t.hpp:122
static Time_t operator+(const Time_t &ta, const Time_t &tb)
Adds two Time_t.
Definition Time_t.hpp:275
static Time_t operator-(const Time_t &ta, const Time_t &tb)
Subtracts two Time_t.
Definition Time_t.hpp:293
static bool operator==(const Time_t &t1, const Time_t &t2)
Comparison assignment.
Definition Time_t.hpp:101
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
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
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 namespace.
Structure Time_t, used to describe times at a DDS level.
Definition Time_t.hpp:36
int32_t seconds
Definition Time_t.hpp:40
void fraction(uint32_t frac)
bool is_infinite() const noexcept
Definition Time_t.hpp:70
static constexpr int32_t INFINITE_SECONDS
Definition Time_t.hpp:37
static void now(Time_t &ret)
Fills a Time_t struct with a representation of the current time.
static constexpr bool is_infinite(const Time_t &t) noexcept
Definition Time_t.hpp:83
Time_t()
Default constructor. Sets values to zero.
static constexpr uint32_t INFINITE_NANOSECONDS
Definition Time_t.hpp:38
uint32_t nanosec
Definition Time_t.hpp:41
int64_t to_ns() const
Returns stored time as nanoseconds (including seconds)
Time_t(int32_t sec, uint32_t nsec)