Skip to content

Commit

Permalink
new ctors
Browse files Browse the repository at this point in the history
  • Loading branch information
xanthospap committed Dec 11, 2023
1 parent 4fb00ad commit 79cc399
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 13 deletions.
10 changes: 10 additions & 0 deletions src/dtfund.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,16 @@ int dat(modified_julian_day mjd) noexcept;
*/
int dat(modified_julian_day mjd, int &extra_sec_in_day) noexcept;

/** A simple struct to signal fractional seconds; just to secure type safety */
struct FractionalSeconds {
double fsec;
}; /* FractionalSeconds */

/** A simple struct to signal fractional days; just to secure type safety */
struct FractionalDays {
double fdays;
}; /* FractionalDays */

/** @brief A wrapper class for years.
*
* A year is represented by just an integer number. There are no limits
Expand Down
38 changes: 30 additions & 8 deletions src/tpdate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class TwoPartDateUTC {
return _mjd;
}

explicit TwoPartDateUTC(int mjd, FDOUBLE secday) noexcept
: _mjd(mjd), _fsec(secday) {
normalize();
}

public:
/** Constructor from datetime<T> */
#if __cplusplus >= 202002L
Expand All @@ -71,9 +76,10 @@ class TwoPartDateUTC {
}

/** Constructor from a pair of doubles, such that MJD = a + b */
explicit TwoPartDateUTC(int b = 0, FDOUBLE s = 0) noexcept
: _mjd(b), _fsec(s) {
if (b != 0 && s != 0e0) /* do not normalize for default constructor! */
explicit TwoPartDateUTC(int b = 0,
FractionalSeconds s = FractionalSeconds{0}) noexcept
: _mjd(b), _fsec(s.fsec) {
if (b != 0 && s.fsec != 0e0) /* do not normalize for default constructor! */
this->normalize();
}

Expand Down Expand Up @@ -250,13 +256,27 @@ class TwoPartDate {
int _mjd; /** Mjd */
FDOUBLE _fsec; /** fractional seconds of day */

/* a constexpr constructor that will not check arguments, and will NOT
/** Construct from MJD and fractional seconds **of day**.
*
* A constexpr constructor that will not check arguments, and will NOT
* normalize the date. Be very carefull with this one!
*/
constexpr explicit TwoPartDate(int mjd, FDOUBLE secday,
[[maybe_unused]] char c) noexcept
: _mjd(mjd), _fsec(secday) {}

/** Construct from MJD and fractional seconds.
*
* This is only private and should be used in rare cases. Normal users,
* should explicitely cast the second argument to FractionalSeconds to
* avoid misconceptions (i.e. is the parameters fractional seconds or
* fractional days?).
*/
explicit TwoPartDate(int mjd, FDOUBLE secday) noexcept
: _mjd(mjd), _fsec(secday) {
normalize();
}

public:
/** Constructor from datetime<T>
* Note that we are not (explicitly) normalizing the instance here, because
Expand Down Expand Up @@ -302,8 +322,10 @@ class TwoPartDate {
return TwoPartDate(datetime<nanoseconds>::max());
}

/** Constructor from a pair of doubles, such that MJD = a + b */
explicit TwoPartDate(int b = 0, FDOUBLE s = 0) noexcept : _mjd(b), _fsec(s) {
/** Constructor from a pair of doubles, such that TODO */
explicit TwoPartDate(int b = 0,
FractionalSeconds s = FractionalSeconds{0}) noexcept
: _mjd(b), _fsec(s.fsec) {
this->normalize();
}

Expand Down Expand Up @@ -471,7 +493,7 @@ class TwoPartDate {
FDOUBLE secinday = SEC_PER_DAY + dat(utcmjd, extrasec);
utcsec = secinday + utcsec;
}
return TwoPartDateUTC(utcmjd, utcsec);
return TwoPartDateUTC(utcmjd, FractionalSeconds{utcsec});
}

/** Transform an instance to UTC assuming it is in TT */
Expand Down Expand Up @@ -599,7 +621,7 @@ class TwoPartDate {
inline TwoPartDate epj2tpd(double epj) noexcept {
double fday;
const double mjd = core::epj2mjd(epj, fday);
return TwoPartDate(mjd, fday * SEC_PER_DAY);
return TwoPartDate(mjd, FractionalSeconds{fday * SEC_PER_DAY});
}
} /* namespace dso */

Expand Down
6 changes: 4 additions & 2 deletions src/tpdate2.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ class TwoPartDate2 {
this->normalize();
}

/** Constructor from a pair of doubles, such that MJD = a + b */
explicit TwoPartDate2(int b = 0, FDOUBLE s = 0) noexcept : _mjd(b), _fday(s) {
/** Constructor from a pair of doubles, such that TODO */
explicit TwoPartDate2(int b = 0,
FractionalSeconds s = FractionalSeconds{0}) noexcept
: _mjd(b), _fday(s.fsec / SEC_PER_DAY) {
this->normalize();
}

Expand Down
2 changes: 1 addition & 1 deletion src/tpdateutc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ dso::TwoPartDateUTC::operator-(const dso::TwoPartDateUTC &d) const noexcept {
int dat2 = dso::dat(modified_julian_day(d.imjd()));
sec += (dat1 - dat2);
}
return TwoPartDate(days, sec);
return TwoPartDate(days, FractionalSeconds{sec});
}
4 changes: 2 additions & 2 deletions src/utc2tai.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
dso::TwoPartDate dso::TwoPartDateUTC::utc2tai() const noexcept {
FDOUBLE taisec;
int taimjd = this->utc2tai(taisec);
return dso::TwoPartDate(taimjd, taisec);
return dso::TwoPartDate(taimjd, dso::FractionalSeconds{taisec});
}

dso::TwoPartDate dso::TwoPartDateUTC::utc2tt() const noexcept {
FDOUBLE ttsec;
int ttmjd = this->utc2tai(ttsec);
return dso::TwoPartDate(ttmjd, ttsec);
return dso::TwoPartDate(ttmjd, dso::FractionalSeconds{ttsec});
}

0 comments on commit 79cc399

Please sign in to comment.