Skip to content

Commit

Permalink
Only 36524 days in the 20th century
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherRabotin committed Apr 7, 2024
1 parent d29c3cd commit fe5a79f
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 10 deletions.
9 changes: 4 additions & 5 deletions src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,8 +391,7 @@ impl Epoch {
TimeScale::ET => {
// Run a Newton Raphston to convert find the correct value of the ... ?!

let mut seconds =
(j1900_tai_offset - TimeScale::ET.prime_epoch_offset()).to_seconds();
let mut seconds = (j1900_tai_offset - ts.prime_epoch_offset()).to_seconds();
for _ in 0..5 {
seconds -= -NAIF_K
* (NAIF_M0
Expand All @@ -408,11 +407,11 @@ impl Epoch {
);

// Match SPICE by changing the UTC definition.
j1900_tai_offset + delta_et_tai.seconds() - TimeScale::ET.prime_epoch_offset()
j1900_tai_offset + delta_et_tai.seconds() - ts.prime_epoch_offset()
}
TimeScale::TDB => {
// Iterate to convert find the correct value of the
let mut seconds = (j1900_tai_offset - J2000_TO_J1900_DURATION).to_seconds();
let mut seconds = (j1900_tai_offset - ts.prime_epoch_offset()).to_seconds();
let mut delta = 1e8; // Arbitrary large number, greater than first step of Newton Raphson.
for _ in 0..5 {
let next = seconds - Self::inner_g(seconds);
Expand All @@ -430,7 +429,7 @@ impl Epoch {
Self::inner_g(seconds + (TT_OFFSET_MS * Unit::Millisecond).to_seconds());
let delta_tdb_tai = gamma.seconds() + TT_OFFSET_MS.milliseconds();

j1900_tai_offset + delta_tdb_tai - J2000_TO_J1900_DURATION
j1900_tai_offset + delta_tdb_tai - ts.prime_epoch_offset()
}
TimeScale::UTC => {
// Assume it's TAI
Expand Down
19 changes: 15 additions & 4 deletions src/timescale/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,14 +148,15 @@ impl TimeScale {
}
}

/// Returns the duration between this time scale's reference epoch and the hifitime "prime epoch" of 1900-01-01 00:00:00 (same as NTP).
/// Returns the duration between this time scale's reference epoch and the hifitime "prime epoch" of 1900-01-01 00:00:00 (the NTP prime epoch).
pub(crate) const fn prime_epoch_offset(&self) -> Duration {
match self {
TimeScale::ET | TimeScale::TDB => {
// Both ET and TDB are defined at J2000, which is 2000-01-01 12:00:00
// Both ET and TDB are defined at J2000, which is 2000-01-01 12:00:00 and there were only 36524 days in the 20th century.
// Hence, this math is the output of (Unit.Century*1 + Unit.Hour*12 - Unit.Day*1).to_parts() via Hifitime in Python.
Duration {
centuries: 1,
nanoseconds: 43_200_000_000_000,
centuries: 0,
nanoseconds: 3155716800000000000,
}
}
_ => Duration::ZERO,
Expand Down Expand Up @@ -210,6 +211,7 @@ impl From<u8> for TimeScale {
#[cfg(test)]
mod unit_test_timescale {
use super::TimeScale;
use crate::{Duration, Epoch, Unit};

#[test]
#[cfg(feature = "serde")]
Expand Down Expand Up @@ -237,6 +239,15 @@ mod unit_test_timescale {

#[test]
fn test_ref_epoch() {
let prime_e = Epoch::from_duration(Duration::ZERO, TimeScale::TAI);
assert_eq!(prime_e.duration, Duration::ZERO);
assert_eq!(format!("{prime_e:?}"), "1900-01-01T00:00:00 TAI");
// NOTE: There are only 36524 days in the 20th century, but one century is 36425, so we "overflow" the next century by one day!
assert_eq!(
format!("{:?}", prime_e + Unit::Century * 1),
"2000-01-02T00:00:00 TAI"
);

assert_eq!(
format!("{:?}", TimeScale::ET.reference_epoch()),
"2000-01-01T12:00:00 ET"
Expand Down
2 changes: 1 addition & 1 deletion tests/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -655,7 +655,7 @@ fn naif_spice_et_tdb_verification() {
0.0
};
assert!(
dbg!(epoch.to_et_seconds() - et_s + extra_seconds).abs() < EPSILON,
(epoch.to_et_seconds() - et_s + extra_seconds).abs() < EPSILON,
"{} failed ET test",
epoch
);
Expand Down

0 comments on commit fe5a79f

Please sign in to comment.