/src/Fast-DDS/src/cpp/utils/TimeConversion.hpp
Line | Count | Source |
1 | | // Copyright 2016 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 | | |
15 | | /** |
16 | | * @file TimeConversion.h |
17 | | * |
18 | | */ |
19 | | |
20 | | #ifndef _FASTDDS_TIMECONVERSION_H_ |
21 | | #define _FASTDDS_TIMECONVERSION_H_ |
22 | | |
23 | | #include <cstdint> |
24 | | #include <fastdds/rtps/common/Time_t.hpp> |
25 | | |
26 | | namespace eprosima { |
27 | | namespace fastdds { |
28 | | namespace rtps { |
29 | | |
30 | | namespace TimeConv { |
31 | | |
32 | | /** |
33 | | * Convert Time_t to seconds as a double |
34 | | */ |
35 | | inline double Time_t2SecondsDouble( |
36 | | const Time_t& t) |
37 | 0 | { |
38 | 0 | return (double)t.seconds() + (double)(t.fraction() / pow(2.0, 32)); |
39 | 0 | } |
40 | | |
41 | | /** |
42 | | * Convert Time_t to seconds as an int64 |
43 | | */ |
44 | | inline int64_t Time_t2MicroSecondsInt64( |
45 | | const Time_t& t) |
46 | 0 | { |
47 | 0 | return (int64_t)(t.fraction() / pow(2.0, 32) * pow(10.0, 6)) + t.seconds() * (int64_t)pow(10.0, 6); |
48 | 0 | } |
49 | | |
50 | | /** |
51 | | * Convert Duration_t to seconds as an int64 |
52 | | */ |
53 | | inline int64_t Duration_t2MicroSecondsInt64( |
54 | | const fastdds::dds::Duration_t& t) |
55 | 353 | { |
56 | 353 | return (int64_t)(t.nanosec / 1000.0) + t.seconds * (int64_t)pow(10.0, 6); |
57 | 353 | } |
58 | | |
59 | | /** |
60 | | * Convert Time_t to microseconds as a double |
61 | | */ |
62 | | inline double Time_t2MicroSecondsDouble( |
63 | | const Time_t& t) |
64 | 0 | { |
65 | 0 | return ((double)t.fraction() / pow(2.0, 32) * pow(10.0, 6)) + (double)t.seconds() * pow(10.0, 6); |
66 | 0 | } |
67 | | |
68 | | /** |
69 | | * Convert Time_t to milliseconds as an int64 |
70 | | */ |
71 | | inline int64_t Time_t2MilliSecondsInt64( |
72 | | const Time_t& t) |
73 | 0 | { |
74 | 0 | return (int64_t)(t.fraction() / pow(2.0, 32) * pow(10.0, 3)) + t.seconds() * (int64_t)pow(10.0, 3); |
75 | 0 | } |
76 | | |
77 | | /** |
78 | | * Convert Time_t to milliseconds as a double |
79 | | */ |
80 | | inline double Time_t2MilliSecondsDouble( |
81 | | const Time_t& t) |
82 | 0 | { |
83 | 0 | return ((double)t.fraction() / pow(2.0, 32) * pow(10.0, 3)) + (double)t.seconds() * pow(10.0, 3); |
84 | 0 | } |
85 | | |
86 | | /** |
87 | | * Convert Duration_t to milliseconds as a double |
88 | | */ |
89 | | inline double Duration_t2MilliSecondsDouble( |
90 | | const fastdds::dds::Duration_t& t) |
91 | 0 | { |
92 | 0 | return ((double)t.nanosec / 1000000.0) + (double)t.seconds * pow(10.0, 3); |
93 | 0 | } |
94 | | |
95 | | /** |
96 | | * Convert milliseconds to Time_t |
97 | | */ |
98 | | inline Time_t MilliSeconds2Time_t( |
99 | | double millisec) |
100 | 0 | { |
101 | 0 | Time_t t; |
102 | 0 | t.seconds((int32_t)(millisec / pow(10.0, 3))); |
103 | 0 | t.fraction((uint32_t)((millisec - (double)t.seconds() * pow(10.0, 3)) / pow(10.0, 3) * pow(2.0, 32))); |
104 | 0 | return t; |
105 | 0 | } |
106 | | |
107 | | /** |
108 | | * Convert microseconds to Time_t |
109 | | */ |
110 | | inline Time_t MicroSeconds2Time_t( |
111 | | double microsec) |
112 | 0 | { |
113 | 0 | Time_t t; |
114 | 0 | t.seconds((int32_t)(microsec / pow(10.0, 6))); |
115 | 0 | t.fraction((uint32_t)((microsec - (double)t.seconds() * pow(10.0, 6)) / pow(10.0, 6) * pow(2.0, 32))); |
116 | 0 | return t; |
117 | 0 | } |
118 | | |
119 | | /** |
120 | | * Convert seconds to Time_t |
121 | | */ |
122 | | inline Time_t Seconds2Time_t( |
123 | | double seconds) |
124 | 0 | { |
125 | 0 | Time_t t; |
126 | 0 | t.seconds((int32_t)seconds); |
127 | 0 | t.fraction((uint32_t)((seconds - (double)t.seconds()) * pow(2.0, 32))); |
128 | 0 | return t; |
129 | 0 | } |
130 | | |
131 | | /** |
132 | | * Get the absolute difference between two Time_t in milliseconds as double |
133 | | */ |
134 | | inline double Time_tAbsDiff2DoubleMillisec( |
135 | | const Time_t& t1, |
136 | | const Time_t& t2) |
137 | 0 | { |
138 | 0 | double result = 0; |
139 | 0 | result += (double)abs((t2.seconds() - t1.seconds()) * 1000); |
140 | 0 | result += (double)std::abs((t2.fraction() - t1.fraction()) / pow(2.0, 32) * 1000); |
141 | 0 | return result; |
142 | 0 | } |
143 | | |
144 | | //! Create a random Time_t that is millisec + [-randoff,randoff] |
145 | | inline Time_t MilliSecondsWithRandOffset2Time_t( |
146 | | double millisec, |
147 | | double randoff) |
148 | 0 | { |
149 | 0 | randoff = std::abs(randoff); |
150 | 0 | millisec = millisec + (-randoff) + static_cast<double> (rand()) / |
151 | 0 | ( static_cast<double> (RAND_MAX / (2 * randoff))); |
152 | 0 | return MilliSeconds2Time_t(millisec); |
153 | 0 | } |
154 | | |
155 | | //! Create a random Time_t that is microsec + [-randoff,randoff] |
156 | | inline Time_t MicroSecondsWithRandOffset2Time_t( |
157 | | double microsec, |
158 | | double randoff) |
159 | 0 | { |
160 | 0 | randoff = std::abs(randoff); |
161 | 0 | microsec = microsec + (-randoff) + static_cast<double> (rand()) / |
162 | 0 | ( static_cast<double> (RAND_MAX / (2 * randoff))); |
163 | 0 | return MicroSeconds2Time_t(microsec); |
164 | 0 | } |
165 | | |
166 | | //! Create a random Time_t that is sec + [-randoff,randoff] |
167 | | inline Time_t SecondsWithRandOffset2Time_t( |
168 | | double sec, |
169 | | double randoff) |
170 | 0 | { |
171 | 0 | randoff = std::abs(randoff); |
172 | 0 | sec = sec + (-randoff) + static_cast<double> (rand()) / ( static_cast<double> (RAND_MAX / (2 * randoff))); |
173 | 0 | return Seconds2Time_t(sec); |
174 | 0 | } |
175 | | |
176 | | } // namespace TimeConv |
177 | | } /* namespace rtps */ |
178 | | } /* namespace fastdds */ |
179 | | } /* namespace eprosima */ |
180 | | |
181 | | #endif /* _FASTDDS_TIMECONVERSION_H_ */ |