Skip to content

Commit

Permalink
minor
Browse files Browse the repository at this point in the history
  • Loading branch information
xanthospap committed Nov 27, 2024
1 parent 15ffebb commit 586cb61
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 25 deletions.
17 changes: 15 additions & 2 deletions include/dtdatetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,13 +349,26 @@ class datetime {
}

/** @brief Get the difference between two datetime instances as some kind of
* floating type time.
* difference.
*
* The difference can be obtained as a fractional days or fractional seconds,
* depending on the template parameter \p DT.
* depending on the template parameter \p DT. This choice is passed in via
* the \p DT template parameter.
*
* If called as diff(d1,d2), the computation is d1-d2; the difference can be
* negative if d2>d1.
*
* examples:
* const datetime<nanoseconds> d1 = (...);
* const datetime<nanoseconds> d2 = (...);
* // get the d1-d2 time period in seconds (fractional) as a
* // FractionalSeconds instnce:
* FractionalSeconds sec =
* d1.diff<DateTimeDifferenceType::FractionalSeconds>(d2);
* // get the above as a floating point number:
* double fsec = sec.seconds();
*
* @see fractional_types.hpp
* @warning Does not take into account leap seconds.
*/
template <DateTimeDifferenceType DT>
Expand Down
40 changes: 19 additions & 21 deletions include/tpdate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -240,14 +240,10 @@ class TwoPartDateUTC {
* Normalize here is meant in the sense that the fractional seconds of day
* (\p _fsec) are always in the range [0,86400+leap_insertion_secs), while
* the integral part (\p _mjd) is always a valid integer.
* The only case where negative seconds are allowed, is when the MJD part is
* zero. In this case, the seconds of day are allowed to be negative, so
* that they can hold the sign.
*/
void normalize() noexcept {
if (_fsec >= 0e0 && _fsec < 86400e0)
return;
assert(_fsec >= 0e0);
int extra_sec_in_day;
dat(modified_julian_day(_mjd), extra_sec_in_day);
/* for each MJD, remove integral days. Each MJD may have a different
Expand All @@ -259,6 +255,12 @@ class TwoPartDateUTC {
++_mjd;
dat(modified_julian_day(_mjd), extra_sec_in_day);
}
while (_fsec < 0e0) {
--_mjd;
_fsec += 86400e0;
dat(modified_julian_day(_mjd), extra_sec_in_day);
_fsec += 1e0 * extra_sec_in_day;
}
#ifdef DEBUG
if (_mjd)
assert(_fsec >= 0e0 && _fsec < 86400e0 + extra_sec_in_day);
Expand Down Expand Up @@ -567,11 +569,13 @@ class TwoPartDate {
return FractionalDays{(_mjd - d._mjd) + (_fsec - d._fsec) / SEC_PER_DAY};
} else if constexpr (DT == DateTimeDifferenceType::FractionalSeconds) {
/* difference as fractional seconds */
return FractionalSeconds{(_fsec - d._fsec) + (_mjd - d._mjd) * SEC_PER_DAY};
return FractionalSeconds{(_fsec - d._fsec) +
(_mjd - d._mjd) * SEC_PER_DAY};
} else {
/* difference as fractional (julian) years */
return FractionalYears{this->diff<DateTimeDifferenceType::FractionalDays>(d).days() /
DAYS_IN_JULIAN_YEAR};
return FractionalYears{
this->diff<DateTimeDifferenceType::FractionalDays>(d).days() /
DAYS_IN_JULIAN_YEAR};
}
}

Expand Down Expand Up @@ -606,34 +610,28 @@ class TwoPartDate {
* \f$ TAI = GPSTime + 19 [sec] \f$
*/
TwoPartDate tai2gps() const noexcept {
return TwoPartDate(_mjd, _fsec - 19e0);
return TwoPartDate(_mjd, _fsec - TAI_MINUS_GPS);
}

/** @brief Transform an instance to TAI Time assuming it is in GPS Time. */
TwoPartDate gps2tai() const noexcept {
return TwoPartDate(_mjd, _fsec + 19e0);
return TwoPartDate(_mjd, _fsec + TAI_MINUS_GPS);
}

/** @brief Transform an instance to UTC assuming it is in TAI. */
TwoPartDateUTC tai2utc() const noexcept {
FDOUBLE utcsec = _fsec - dat(modified_julian_day(_mjd));
FDOUBLE utcsec = _fsec - (double)dat(modified_julian_day(_mjd));
int utcmjd = _mjd;
/* in case we are actually on the previous day, check for leap insertion
* day and 'normalize' integral and fractional parts
*/
if (utcsec < 0e0) {
--utcmjd;
int extrasec;
FDOUBLE secinday =
SEC_PER_DAY + dat(modified_julian_day(utcmjd), extrasec);
utcsec = secinday + utcsec;
}
/* let the TwoPartDateUTC constructor normalize the instance */
return TwoPartDateUTC(utcmjd, FractionalSeconds{utcsec});
}

/** @brief Transform an instance to UTC assuming it is in GPS Time. */
TwoPartDateUTC gps2utc() const noexcept { return gps2tai().tai2utc(); }

/** @brief Transform an instance to UTC assuming it is in TT */
TwoPartDateUTC tt2utc() const noexcept {
const auto tai = this->tai2tt();
const auto tai = this->tt2tai();
return tai.tai2utc();
}

Expand Down
3 changes: 3 additions & 0 deletions src/core/cdatetime.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ 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;

/** TAI minus GPS in [sec] */
constexpr const double TAI_MINUS_GPS = 19e0;

} /* namespace dso */

#endif
4 changes: 2 additions & 2 deletions src/core/fundamental_calendar_utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
* safety.
*/

#ifndef __DSO_NONTYPE_CALENDAR_UTILS_HPP__
#define __DSO_NONTYPE_CALENDAR_UTILS_HPP__
#ifndef __DSO_NONTYPE_CALENDAR_UTILS_CORE_HPP__
#define __DSO_NONTYPE_CALENDAR_UTILS_CORE_HPP__

#include "cdatetime.hpp"
#include <cmath>
Expand Down

0 comments on commit 586cb61

Please sign in to comment.