Files
trung09122002 48bd5377dc upload
2026-09-18 18:14:42 +07:00

198 lines
7.1 KiB
C++

#ifndef VSLAM_UNIX_TIME_H
#define VSLAM_UNIX_TIME_H
// Thay ros::Time / ros::Duration.
//
// Dua tren xloc_cpp/include/unix_time.h, giu nguyen layout {uint32 sec, nsec}
// de tuong thich API voi xloc. Ba sua loi so voi ban goc:
//
// 1. fromSec() voi input am: ban goc lam static_cast<uint32_t>(std::floor(s))
// -> tran im lang. Ban nay kep ve 0 va co fromSecChecked() de goi biet.
// 2. So hoc noi bo lam bang int64_t nanoseconds. Ban goc ep hieu hai UnixTime
// sang int32_t; nam 2038 se tran, va ngay bay gio thi sec ~1.7e9 da sat
// gioi han 2.1e9 cua int32_t.
// 3. TIME_MAX: ban goc dat trong anonymous namespace TRONG HEADER, nen moi
// translation unit co mot ban sao rieng (va sinh canh bao unused). Ban nay
// dung `inline constexpr` cua C++17.
//
// QUY TAC HAI DONG HO -- doc ky truoc khi dung:
// * UnixTime dung de DONG DAU du lieu (stamp). Nguon la system_clock, co the
// nhay khi NTP dieu chinh.
// * Timeout, chu ky, do thoi luong PHAI dung std::chrono::steady_clock.
// Lan lon hai thu nay la lop bug dang co trong safety_monitor_node.cpp: no
// dung ros::Time::now() cho ca hai, nen mot buoc NTP sau khi Jetson bat WiFi
// lam (now - last_cmd_time_) nhay vot -> NO_CMD dung robot dot ngot, hoac
// nhay lui -> watchdog khong bao gio kich.
// Xem clock.h: moi stage nhan Clock& thay vi goi UnixTime::now() truc tiep.
#include <cstdint>
#include <cstdlib>
#include <cmath>
#include <chrono>
#include <limits>
#include <string>
#include <thread>
namespace vslam {
class Duration;
constexpr int64_t kNsPerSec = 1000000000LL;
class UnixTime {
public:
uint32_t sec;
uint32_t nsec;
constexpr UnixTime() : sec(0), nsec(0) {}
constexpr UnixTime(uint32_t s, uint32_t ns) : sec(s), nsec(ns) {}
static UnixTime now() {
using namespace std::chrono;
const auto ns =
duration_cast<nanoseconds>(system_clock::now().time_since_epoch()).count();
return fromNSec(ns);
}
static constexpr UnixTime fromNSec(int64_t ns) {
return ns <= 0 ? UnixTime()
: UnixTime(static_cast<uint32_t>(ns / kNsPerSec),
static_cast<uint32_t>(ns % kNsPerSec));
}
// Input am bi kep ve 0. Ban goc tran im lang o day.
static UnixTime fromSec(double s) {
if (!(s > 0.0)) return UnixTime(); // bat ca NaN
return fromNSec(static_cast<int64_t>(std::llround(s * 1e9)));
}
// Nhu fromSec() nhung bao cho goi biet input co hop le khong.
static bool fromSecChecked(double s, UnixTime* out) {
if (!(s >= 0.0) || s > static_cast<double>(std::numeric_limits<uint32_t>::max())) {
return false;
}
*out = fromNSec(static_cast<int64_t>(std::llround(s * 1e9)));
return true;
}
constexpr int64_t toNSec() const {
return static_cast<int64_t>(sec) * kNsPerSec + static_cast<int64_t>(nsec);
}
constexpr double toSec() const {
return static_cast<double>(sec) + static_cast<double>(nsec) * 1e-9;
}
constexpr bool isZero() const { return sec == 0 && nsec == 0; }
constexpr bool operator==(const UnixTime& o) const {
return sec == o.sec && nsec == o.nsec;
}
constexpr bool operator!=(const UnixTime& o) const { return !(*this == o); }
constexpr bool operator<(const UnixTime& o) const {
return sec < o.sec || (sec == o.sec && nsec < o.nsec);
}
constexpr bool operator>(const UnixTime& o) const { return o < *this; }
constexpr bool operator<=(const UnixTime& o) const { return !(*this > o); }
constexpr bool operator>=(const UnixTime& o) const { return !(*this < o); }
friend Duration operator-(const UnixTime& a, const UnixTime& b);
friend UnixTime operator+(const UnixTime& t, const Duration& d);
friend UnixTime operator-(const UnixTime& t, const Duration& d);
// Giu lai dang ham de tuong thich voi ma goi theo kieu xloc.
static constexpr UnixTime TIME_MAX() {
return UnixTime(std::numeric_limits<uint32_t>::max(),
static_cast<uint32_t>(kNsPerSec - 1));
}
// "1737000000.123456789" -- dung cho log va JSON, khong mat do chinh xac
// nhu khi in double.
std::string toString() const;
};
inline constexpr UnixTime kTimeMax = UnixTime::TIME_MAX();
inline constexpr UnixTime kTimeZero = UnixTime();
class Duration {
public:
int32_t sec;
int32_t nsec;
constexpr Duration() : sec(0), nsec(0) {}
Duration(int32_t s, int32_t ns) : sec(s), nsec(ns) { normalize(); }
explicit Duration(double seconds) { *this = fromNSec(static_cast<int64_t>(std::llround(seconds * 1e9))); }
static Duration fromSec(double s) { return Duration(s); }
static Duration fromNSec(int64_t ns_total) {
Duration d;
d.sec = static_cast<int32_t>(ns_total / kNsPerSec);
d.nsec = static_cast<int32_t>(ns_total % kNsPerSec);
d.normalize();
return d;
}
constexpr int64_t toNSec() const {
return static_cast<int64_t>(sec) * kNsPerSec + static_cast<int64_t>(nsec);
}
constexpr double toSec() const {
return static_cast<double>(sec) + static_cast<double>(nsec) * 1e-9;
}
constexpr bool isZero() const { return sec == 0 && nsec == 0; }
void normalize() {
if (nsec >= kNsPerSec) {
sec += static_cast<int32_t>(nsec / kNsPerSec);
nsec = static_cast<int32_t>(nsec % kNsPerSec);
} else if (nsec < 0) {
const int32_t borrow = (std::abs(nsec) / static_cast<int32_t>(kNsPerSec)) + 1;
sec -= borrow;
nsec += borrow * static_cast<int32_t>(kNsPerSec);
}
}
Duration operator+(const Duration& o) const { return fromNSec(toNSec() + o.toNSec()); }
Duration operator-(const Duration& o) const { return fromNSec(toNSec() - o.toNSec()); }
Duration operator-() const { return fromNSec(-toNSec()); }
Duration& operator+=(const Duration& o) { *this = *this + o; return *this; }
Duration& operator-=(const Duration& o) { *this = *this - o; return *this; }
constexpr bool operator==(const Duration& o) const { return sec == o.sec && nsec == o.nsec; }
constexpr bool operator!=(const Duration& o) const { return !(*this == o); }
bool operator<(const Duration& o) const { return toNSec() < o.toNSec(); }
bool operator>(const Duration& o) const { return o < *this; }
bool operator<=(const Duration& o) const { return !(*this > o); }
bool operator>=(const Duration& o) const { return !(*this < o); }
// Chi dung o main/tool. Trong stage phai dung Clock::SleepUntil() de replay
// tat dinh chay duoc.
void sleep() const {
const int64_t ns = toNSec();
if (ns <= 0) return;
std::this_thread::sleep_for(std::chrono::nanoseconds(ns));
}
};
inline Duration operator-(const UnixTime& a, const UnixTime& b) {
return Duration::fromNSec(a.toNSec() - b.toNSec());
}
inline UnixTime operator+(const UnixTime& t, const Duration& d) {
return UnixTime::fromNSec(t.toNSec() + d.toNSec());
}
inline UnixTime operator-(const UnixTime& t, const Duration& d) {
return UnixTime::fromNSec(t.toNSec() - d.toNSec());
}
// Giay troi qua giua hai stamp. Dung cho moi phep so sanh timeout theo stamp
// du lieu (khong phai timeout tuong doi -- cai do dung steady_clock).
inline double SecondsBetween(const UnixTime& later, const UnixTime& earlier) {
return static_cast<double>(later.toNSec() - earlier.toNSec()) * 1e-9;
}
} // namespace vslam
#endif // VSLAM_UNIX_TIME_H