#include #include "JAcoustics/JToA.hh" // FIXME: to be replaced by std::chrono::round once we move to C++17 namespace { template To round(const Duration& d) { To t0 = std::chrono::duration_cast(d); To t1 = t0 + To{1}; auto diff0 = d - t0; auto diff1 = t1 - d; if (diff0 == diff1) { if (t0.count() & 1) { return t1; } return t0; } else if (diff0 < diff1) { return t0; } return t1; } } namespace JACOUSTICS { constexpr double ONE_TICK_IN_NS = 16E-9; // one tick of the 62.5 MHz clock = 16 nanoseconds constexpr double SECOND_IN_NANOSECOND = 1E-9; constexpr double SECOND_IN_MICROSECOND = 1E-6; constexpr int64_t NANOSECONDS_IN_ONE_SECOND = 1000000000LL; constexpr int64_t NANOSECONDS_IN_ONE_TICK = 16LL; JToA::JToA() = default; double JToA::daqFrameTimeStart_s() const { return (static_cast(SECONDS) + static_cast(TICKS) * ONE_TICK_IN_NS); } double JToA::TOA_S() const { return relativeToA_s() + daqFrameTimeStart_s(); } int64_t JToA::relativeToA_ns() const { return TOA_NS - daqFrameTimeStart_ns(); } double JToA::relativeToA_s() const { // to mimic what's done in the streamds retrieval, we truncate // to microseconds first, then go to seconds. std::chrono::microseconds mus = round(std::chrono::nanoseconds{relativeToA_ns()}); return static_cast(mus.count()) * SECOND_IN_MICROSECOND; } int64_t JToA::daqFrameTimeStart_ns() const { return SECONDS * NANOSECONDS_IN_ONE_SECOND + TICKS * NANOSECONDS_IN_ONE_TICK; } } // namespace JACOUSTICS