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