fsfw/src/fsfw/globalfunctions/timevalOperations.cpp

95 lines
2.7 KiB
C++
Raw Normal View History

2021-07-13 20:22:54 +02:00
#include "fsfw/globalfunctions/timevalOperations.h"
2020-09-18 13:15:14 +02:00
2022-11-09 17:54:08 +01:00
#include <cstdio>
2020-09-18 13:15:14 +02:00
timeval& operator+=(timeval& lhs, const timeval& rhs) {
2022-11-09 17:54:08 +01:00
int64_t sum = static_cast<int64_t>(lhs.tv_sec) * 1000000. + lhs.tv_usec;
sum += static_cast<int64_t>(rhs.tv_sec) * 1000000. + rhs.tv_usec;
int64_t tmp = sum / 1000000;
lhs.tv_sec = tmp;
2022-02-02 10:29:30 +01:00
lhs.tv_usec = sum - lhs.tv_sec * 1000000;
return lhs;
2020-09-18 13:15:14 +02:00
}
timeval operator+(timeval lhs, const timeval& rhs) {
2022-02-02 10:29:30 +01:00
lhs += rhs;
return lhs;
2020-09-18 13:15:14 +02:00
}
timeval& operator-=(timeval& lhs, const timeval& rhs) {
2022-02-02 10:29:30 +01:00
int64_t sum = lhs.tv_sec * 1000000. + lhs.tv_usec;
sum -= rhs.tv_sec * 1000000. + rhs.tv_usec;
lhs.tv_sec = sum / 1000000;
lhs.tv_usec = sum - lhs.tv_sec * 1000000;
return lhs;
2020-09-18 13:15:14 +02:00
}
timeval operator-(timeval lhs, const timeval& rhs) {
2022-02-02 10:29:30 +01:00
lhs -= rhs;
return lhs;
2020-09-18 13:15:14 +02:00
}
double operator/(const timeval& lhs, const timeval& rhs) {
2022-02-02 10:29:30 +01:00
double lhs64 = lhs.tv_sec * 1000000. + lhs.tv_usec;
double rhs64 = rhs.tv_sec * 1000000. + rhs.tv_usec;
return lhs64 / rhs64;
2020-09-18 13:15:14 +02:00
}
timeval& operator/=(timeval& lhs, double scalar) {
2022-02-02 10:29:30 +01:00
int64_t product = lhs.tv_sec * 1000000. + lhs.tv_usec;
product /= scalar;
lhs.tv_sec = product / 1000000;
lhs.tv_usec = product - lhs.tv_sec * 1000000;
return lhs;
2020-09-18 13:15:14 +02:00
}
timeval operator/(timeval lhs, double scalar) {
2022-02-02 10:29:30 +01:00
lhs /= scalar;
return lhs;
2020-09-18 13:15:14 +02:00
}
timeval& operator*=(timeval& lhs, double scalar) {
2022-02-02 10:29:30 +01:00
int64_t product = lhs.tv_sec * 1000000. + lhs.tv_usec;
product *= scalar;
lhs.tv_sec = product / 1000000;
lhs.tv_usec = product - lhs.tv_sec * 1000000;
return lhs;
2020-09-18 13:15:14 +02:00
}
timeval operator*(timeval lhs, double scalar) {
2022-02-02 10:29:30 +01:00
lhs *= scalar;
return lhs;
2020-09-18 13:15:14 +02:00
}
timeval operator*(double scalar, timeval rhs) {
2022-02-02 10:29:30 +01:00
rhs *= scalar;
return rhs;
2020-09-18 13:15:14 +02:00
}
bool operator==(const timeval& lhs, const timeval& rhs) {
2022-02-02 10:29:30 +01:00
int64_t lhs64 = lhs.tv_sec * 1000000. + lhs.tv_usec;
int64_t rhs64 = rhs.tv_sec * 1000000. + rhs.tv_usec;
return lhs64 == rhs64;
2020-09-18 13:15:14 +02:00
}
2022-02-02 10:29:30 +01:00
bool operator!=(const timeval& lhs, const timeval& rhs) { return !operator==(lhs, rhs); }
2020-09-18 13:15:14 +02:00
bool operator<(const timeval& lhs, const timeval& rhs) {
2022-02-02 10:29:30 +01:00
int64_t lhs64 = lhs.tv_sec * 1000000. + lhs.tv_usec;
int64_t rhs64 = rhs.tv_sec * 1000000. + rhs.tv_usec;
return lhs64 < rhs64;
2020-09-18 13:15:14 +02:00
}
2022-02-02 10:29:30 +01:00
bool operator>(const timeval& lhs, const timeval& rhs) { return operator<(rhs, lhs); }
bool operator<=(const timeval& lhs, const timeval& rhs) { return !operator>(lhs, rhs); }
bool operator>=(const timeval& lhs, const timeval& rhs) { return !operator<(lhs, rhs); }
2020-09-18 13:15:14 +02:00
double timevalOperations::toDouble(const timeval timeval) {
2022-02-02 10:29:30 +01:00
double result = timeval.tv_sec * 1000000. + timeval.tv_usec;
return result / 1000000.;
2020-09-18 13:15:14 +02:00
}
timeval timevalOperations::toTimeval(const double seconds) {
2022-02-02 10:29:30 +01:00
timeval tval;
tval.tv_sec = seconds;
tval.tv_usec = seconds * (double)1e6 - (tval.tv_sec * 1e6);
return tval;
2020-09-18 13:15:14 +02:00
}