Skip to content

Commit

Permalink
var
Browse files Browse the repository at this point in the history
  • Loading branch information
xanthospap committed Jan 2, 2024
1 parent 40f94d9 commit a13326e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
3 changes: 3 additions & 0 deletions src/cdatetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
13 changes: 13 additions & 0 deletions src/datetime_tops.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,26 @@ template <typename Ssrc, typename Strg,
typename = std::enable_if_t<Strg::is_of_sec_type>>
#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<long>() > Strg::template sec_factor<long>()),
typename Ssrc::underlying_type, typename Strg::underlying_type>;

if constexpr (Strg::template sec_factor<long>() >=
Ssrc::template sec_factor<long>()) {
constexpr const SecIntType factor =
Strg::template sec_factor<SecIntType>() /
Ssrc::template sec_factor<SecIntType>();
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, seconds>(milliseconds{2000L});
// this is: (1/1000)*2000 which is 0 because 1/1000 is 0, but
// (2000*1)/1000 = 2 which is correct
const auto numerator = s.__member_ref__() * Strg::template sec_factor<long>();
return Strg(numerator / Ssrc::template sec_factor<long>());
}
}

} /* namespace dso */
Expand Down
1 change: 1 addition & 0 deletions src/datetime_write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ const char *to_char(const hms_time<S> &hms, char *buffer) {
*/
template <YMDFormat FD, HMSFormat FT, typename S>
const char *to_char(const datetime<S> &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<FD>::spit(ymd, buffer) != SpitDate<FD>::numChars) {
Expand Down
25 changes: 14 additions & 11 deletions src/dtdatetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<S>(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.
Expand Down Expand Up @@ -585,8 +590,8 @@ class datetime {
* \f$ TT = TAI + ΔT \$ where \f$ ΔT = TT - TAI = 32.184 [sec] \f$
*/
constexpr datetime<S> tai2tt() const noexcept {
constexpr const SecIntType dtat = static_cast<SecIntType>(
TT_MINUS_TAI * S::template sec_factor<double>());
constexpr const S dtat =
dso::cast_to<nanoseconds, S>(nanoseconds(TT_MINUS_TAI_IN_NANOSEC));
return datetime(m_mjd, m_sec + dtat);
}

Expand All @@ -606,19 +611,17 @@ class datetime {
* The two time scales are connected by the formula:
* \f$ TAI = GPSTime + 19 [sec] \f$
*/
constexpr datetime<S> tai2gps() const noexcept {
constexpr const SecIntType dt =
static_cast<SecIntType>(19 * S::template sec_factor<SecIntType>());
return datetime(m_mjd, m_sec - dt);
[[nodiscard]] constexpr datetime<S> tai2gps() const noexcept {
return datetime(m_mjd, m_sec - dso::cast_to<seconds, S>(seconds(19)));
}

constexpr datetime<S> gps2tai() const noexcept {
constexpr const SecIntType dt =
static_cast<SecIntType>(19 * S::template sec_factor<SecIntType>());
return datetime(m_mjd, m_sec + dt);
[[nodiscard]] constexpr datetime<S> gps2tai() const noexcept {
return datetime(m_mjd, m_sec + dso::cast_to<seconds, S>(seconds(19)));
}

constexpr datetime<S> gps2tt() const noexcept { return gps2tai().tai2tt(); }
[[nodiscard]] constexpr datetime<S> gps2tt() const noexcept {
return gps2tai().tai2tt();
}

private:
/** @brief Add any second type T where S is of higher resolution than T
Expand Down

0 comments on commit a13326e

Please sign in to comment.