TimeConversion.h 4.4 KB

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