diff --git a/src/epoch/initializers.rs b/src/epoch/initializers.rs index dbb2d53..d1eb806 100644 --- a/src/epoch/initializers.rs +++ b/src/epoch/initializers.rs @@ -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] @@ -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] diff --git a/tests/epoch.rs b/tests/epoch.rs index 63af9d8..ae0e58a 100644 --- a/tests/epoch.rs +++ b/tests/epoch.rs @@ -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" + ); +}