123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- #ifndef TIMECONVERSION_H_
- #define TIMECONVERSION_H_
- #include <cstdint>
- #include <fastdds/rtps/common/Time_t.h>
- namespace eprosima {
- namespace fastrtps{
- namespace rtps {
- namespace TimeConv{
- inline double Time_t2SecondsDouble(const rtps::Time_t& t)
- {
- return (double)t.seconds() + (double)(t.fraction()/pow(2.0,32));
- }
- inline int64_t Time_t2MicroSecondsInt64(const rtps::Time_t& t)
- {
- return (int64_t)(t.fraction()/pow(2.0,32)*pow(10.0,6)) + t.seconds()*(int64_t)pow(10.0,6);
- }
- inline int64_t Duration_t2MicroSecondsInt64(const Duration_t& t)
- {
- return (int64_t)(t.nanosec/1000.0)+t.seconds*(int64_t)pow(10.0,6);
- }
- inline double Time_t2MicroSecondsDouble(const rtps::Time_t& t)
- {
- return ((double)t.fraction()/pow(2.0,32)*pow(10.0,6)) + (double)t.seconds()*pow(10.0,6);
- }
- inline int64_t Time_t2MilliSecondsInt64(const rtps::Time_t& t)
- {
- return (int64_t)(t.fraction()/pow(2.0,32)*pow(10.0,3)) + t.seconds()*(int64_t)pow(10.0,3);
- }
- inline double Time_t2MilliSecondsDouble(const rtps::Time_t& t)
- {
- return ((double)t.fraction()/pow(2.0,32)*pow(10.0,3)) + (double)t.seconds()*pow(10.0,3);
- }
- inline double Duration_t2MilliSecondsDouble(const Duration_t& t)
- {
- return ((double)t.nanosec/1000000.0)+(double)t.seconds*pow(10.0,3);
- }
- inline rtps::Time_t MilliSeconds2Time_t(double millisec)
- {
- rtps::Time_t t;
- t.seconds((int32_t)(millisec/pow(10.0,3)));
- t.fraction((uint32_t)((millisec-(double)t.seconds()*pow(10.0,3))/pow(10.0,3)*pow(2.0,32)));
- return t;
- }
- inline rtps::Time_t MicroSeconds2Time_t(double microsec)
- {
- rtps::Time_t t;
- t.seconds((int32_t)(microsec/pow(10.0,6)));
- t.fraction((uint32_t)((microsec-(double)t.seconds()*pow(10.0,6))/pow(10.0,6)*pow(2.0,32)));
- return t;
- }
- inline rtps::Time_t Seconds2Time_t(double seconds)
- {
- rtps::Time_t t;
- t.seconds((int32_t)seconds);
- t.fraction((uint32_t)((seconds-(double)t.seconds())*pow(2.0,32)));
- return t;
- }
- inline double Time_tAbsDiff2DoubleMillisec(const rtps::Time_t& t1, const rtps::Time_t& t2)
- {
- double result = 0;
- result +=(double)abs((t2.seconds()-t1.seconds())*1000);
- result +=(double)std::abs((t2.fraction()-t1.fraction())/pow(2.0,32)*1000);
- return result;
- }
- inline rtps::Time_t MilliSecondsWithRandOffset2Time_t(double millisec, double randoff)
- {
- randoff = std::abs(randoff);
- millisec = millisec + (-randoff) + static_cast <double> (rand()) /( static_cast <double> (RAND_MAX/(2*randoff)));
- return MilliSeconds2Time_t(millisec);
- }
- inline rtps::Time_t MicroSecondsWithRandOffset2Time_t(double microsec, double randoff)
- {
- randoff = std::abs(randoff);
- microsec = microsec + (-randoff) + static_cast <double> (rand()) /( static_cast <double> (RAND_MAX/(2*randoff)));
- return MicroSeconds2Time_t(microsec);
- }
- inline rtps::Time_t SecondsWithRandOffset2Time_t(double sec, double randoff)
- {
- randoff = std::abs(randoff);
- sec = sec + (-randoff) + static_cast <double> (rand()) /( static_cast <double> (RAND_MAX/(2*randoff)));
- return Seconds2Time_t(sec);
- }
- }
- }
- }
- }
- #endif
|