From a13326ea492c8f137632679cb7c1942dc254efc0 Mon Sep 17 00:00:00 2001 From: xanthospap Date: Wed, 3 Jan 2024 00:15:00 +0200 Subject: [PATCH] var --- src/cdatetime.hpp | 3 +++ src/datetime_tops.hpp | 13 +++++++++++++ src/datetime_write.hpp | 1 + src/dtdatetime.hpp | 25 ++++++++++++++----------- 4 files changed, 31 insertions(+), 11 deletions(-) diff --git a/src/cdatetime.hpp b/src/cdatetime.hpp index a8f0346..6b6c30c 100644 --- a/src/cdatetime.hpp +++ b/src/cdatetime.hpp @@ -37,6 +37,9 @@ constexpr const double MJD0_JD = 2400000.5e0; /** TT minus TAI in [sec] */ constexpr const double TT_MINUS_TAI = 32.184e0; +/** TT minus TAI in [nsec] */ +constexpr const long TT_MINUS_TAI_IN_NANOSEC = 32184 * 1'000'000L; + } /* namespace dso */ #endif diff --git a/src/datetime_tops.hpp b/src/datetime_tops.hpp index 8b3a4e7..dbb3922 100644 --- a/src/datetime_tops.hpp +++ b/src/datetime_tops.hpp @@ -415,6 +415,18 @@ template > #endif constexpr Strg cast_to(Ssrc s) noexcept { + /* underlying int type of the S with the highest resolution */ + using SecIntType = std::conditional_t< + (Ssrc::template sec_factor() > Strg::template sec_factor()), + typename Ssrc::underlying_type, typename Strg::underlying_type>; + + if constexpr (Strg::template sec_factor() >= + Ssrc::template sec_factor()) { + constexpr const SecIntType factor = + Strg::template sec_factor() / + Ssrc::template sec_factor(); + return Strg(s.__member_ref__() * factor); + } else { // this is tricky! We must first compute the numerator and then the fraction. // why? check this out // seconds _s1 = cast_to(milliseconds{2000L}); @@ -422,6 +434,7 @@ constexpr Strg cast_to(Ssrc s) noexcept { // (2000*1)/1000 = 2 which is correct const auto numerator = s.__member_ref__() * Strg::template sec_factor(); return Strg(numerator / Ssrc::template sec_factor()); + } } } /* namespace dso */ diff --git a/src/datetime_write.hpp b/src/datetime_write.hpp index ca0839a..ba921cd 100644 --- a/src/datetime_write.hpp +++ b/src/datetime_write.hpp @@ -149,6 +149,7 @@ const char *to_char(const hms_time &hms, char *buffer) { */ template const char *to_char(const datetime &d, char *buffer) { + printf("\tNote that the date is: %ld %ld\n", d.imjd().as_underlying_type(), d.sec().as_underlying_type()); /* write date to buffer */ ymd_date ymd(d.as_ymd()); if (SpitDate::spit(ymd, buffer) != SpitDate::numChars) { diff --git a/src/dtdatetime.hpp b/src/dtdatetime.hpp index ef6fcc1..712e385 100644 --- a/src/dtdatetime.hpp +++ b/src/dtdatetime.hpp @@ -402,6 +402,11 @@ class datetime { */ constexpr S sec() const noexcept { return m_sec; } + /** Seconds in day as fractional days */ + double fractional_days() const noexcept { + return dso::to_fractional_days(m_sec); + } + /** Operator '+' where the right-hand-side is an interval. * Note that the addition here is algebraic, i.e. the interval is added to * or subtracted from the instance, depending on its sign. @@ -585,8 +590,8 @@ class datetime { * \f$ TT = TAI + ΔT \$ where \f$ ΔT = TT - TAI = 32.184 [sec] \f$ */ constexpr datetime tai2tt() const noexcept { - constexpr const SecIntType dtat = static_cast( - TT_MINUS_TAI * S::template sec_factor()); + constexpr const S dtat = + dso::cast_to(nanoseconds(TT_MINUS_TAI_IN_NANOSEC)); return datetime(m_mjd, m_sec + dtat); } @@ -606,19 +611,17 @@ class datetime { * The two time scales are connected by the formula: * \f$ TAI = GPSTime + 19 [sec] \f$ */ - constexpr datetime tai2gps() const noexcept { - constexpr const SecIntType dt = - static_cast(19 * S::template sec_factor()); - return datetime(m_mjd, m_sec - dt); + [[nodiscard]] constexpr datetime tai2gps() const noexcept { + return datetime(m_mjd, m_sec - dso::cast_to(seconds(19))); } - constexpr datetime gps2tai() const noexcept { - constexpr const SecIntType dt = - static_cast(19 * S::template sec_factor()); - return datetime(m_mjd, m_sec + dt); + [[nodiscard]] constexpr datetime gps2tai() const noexcept { + return datetime(m_mjd, m_sec + dso::cast_to(seconds(19))); } - constexpr datetime gps2tt() const noexcept { return gps2tai().tai2tt(); } + [[nodiscard]] constexpr datetime gps2tt() const noexcept { + return gps2tai().tai2tt(); + } private: /** @brief Add any second type T where S is of higher resolution than T