Skip to content

Commit

Permalink
Fix double counting of leap seconds in MJD/JDE initializers in UTC
Browse files Browse the repository at this point in the history
Closes #302
  • Loading branch information
ChristopherRabotin committed Oct 6, 2024
1 parent 64b4767 commit 9436c67
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 20 deletions.
34 changes: 14 additions & 20 deletions src/epoch/initializers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -102,21 +102,18 @@ impl Epoch {

#[must_use]
pub fn from_mjd_tai(days: f64) -> Self {
Self::from_mjd_in_time_scale(days, TimeScale::TAI)
}

pub fn from_mjd_in_time_scale(days: f64, time_scale: TimeScale) -> Self {
assert!(
days.is_finite(),
"Attempted to initialize Epoch with non finite number"
);
Self::from_tai_duration((days - MJD_J1900) * Unit::Day)
}

pub fn from_mjd_in_time_scale(days: f64, time_scale: TimeScale) -> Self {
// always refer to TAI/mjd
let mut e = Self::from_mjd_tai(days);
if time_scale.uses_leap_seconds() {
e.duration += e.leap_seconds(true).unwrap_or(0.0) * Unit::Second;
Self {
duration: (days - MJD_J1900) * Unit::Day,
time_scale,
}
e.time_scale = time_scale;
e
}

#[must_use]
Expand All @@ -142,21 +139,18 @@ impl Epoch {

#[must_use]
pub fn from_jde_tai(days: f64) -> Self {
Self::from_jde_in_time_scale(days, TimeScale::TAI)
}

fn from_jde_in_time_scale(days: f64, time_scale: TimeScale) -> Self {
assert!(
days.is_finite(),
"Attempted to initialize Epoch with non finite number"
);
Self::from_tai_duration((days - MJD_J1900 - MJD_OFFSET) * Unit::Day)
}

fn from_jde_in_time_scale(days: f64, time_scale: TimeScale) -> Self {
// always refer to TAI/jde
let mut e = Self::from_jde_tai(days);
if time_scale.uses_leap_seconds() {
e.duration += e.leap_seconds(true).unwrap_or(0.0) * Unit::Second;
Self {
duration: (days - MJD_J1900 - MJD_OFFSET) * Unit::Day,
time_scale,
}
e.time_scale = time_scale;
e
}

#[must_use]
Expand Down
29 changes: 29 additions & 0 deletions tests/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2143,3 +2143,32 @@ fn regression_test_gh_317() {
nanos
);
}

#[test]
fn regression_test_gh_302() {
let days = 60660.0;
let mjd = Epoch::from_mjd_utc(days);
let extra_duration = 2 * Unit::Hour + 5 * Unit::Minute + 8 * Unit::Second;
let mjd_plus_duration = mjd + extra_duration;
assert_eq!(
mjd_plus_duration.to_mjd_utc_days(),
days + extra_duration.to_unit(Unit::Day)
);
assert_eq!(
mjd_plus_duration.to_gregorian_str(TimeScale::UTC),
"2024-12-16T02:05:08 UTC"
);

// Repeat with JDE
let jde = Epoch::from_jde_utc(days + MJD_OFFSET);
let extra_duration = 2 * Unit::Hour + 5 * Unit::Minute + 8 * Unit::Second;
let jde_plus_duration = jde + extra_duration;
assert_eq!(
mjd_plus_duration.to_mjd_utc_days(),
days + extra_duration.to_unit(Unit::Day)
);
assert_eq!(
jde_plus_duration.to_gregorian_str(TimeScale::UTC),
"2024-12-16T02:05:08 UTC"
);
}

0 comments on commit 9436c67

Please sign in to comment.