Skip to content

Commit

Permalink
Simplify computation for year() of Epoch
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristopherRabotin committed Dec 30, 2023
1 parent 39412e5 commit a16a0a0
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 23 deletions.
2 changes: 1 addition & 1 deletion src/duration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ impl Add for Duration {
}
}

if me.centuries == Self::MIN.centuries && self.nanoseconds < Self::MIN.nanoseconds {
if me.centuries == Self::MIN.centuries {
// Then we do the operation backward
match me
.nanoseconds
Expand Down
20 changes: 4 additions & 16 deletions src/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@ use crate::leap_seconds::{LatestLeapSeconds, LeapSecondProvider};
use crate::parser::Token;
use crate::{
Errors, MonthName, TimeScale, BDT_REF_EPOCH, DAYS_PER_YEAR_NLD, ET_EPOCH_S, GPST_REF_EPOCH,
GST_REF_EPOCH, J1900_OFFSET, J1900_REF_EPOCH, J2000_TO_J1900_DURATION, MJD_OFFSET,
NANOSECONDS_PER_DAY, NANOSECONDS_PER_MICROSECOND, NANOSECONDS_PER_MILLISECOND,
NANOSECONDS_PER_SECOND_U32, UNIX_REF_EPOCH,
GST_REF_EPOCH, J1900_OFFSET, J2000_TO_J1900_DURATION, MJD_OFFSET, NANOSECONDS_PER_DAY,
NANOSECONDS_PER_MICROSECOND, NANOSECONDS_PER_MILLISECOND, NANOSECONDS_PER_SECOND_U32,
UNIX_REF_EPOCH,
};

use crate::efmt::format::Format;
Expand Down Expand Up @@ -2504,19 +2504,7 @@ impl Epoch {
#[must_use]
/// Returns the number of Gregorian years of this epoch in the current time scale.
pub fn year(&self) -> i32 {
let mut year = Self::compute_gregorian(self.to_duration()).0;
if self.time_scale.ref_epoch() != J1900_REF_EPOCH {
// We need to correct for the year epoch error
let ref_offset_days = (self
.time_scale
.ref_epoch()
.round(DAYS_PER_YEAR_NLD * Unit::Day)
- J1900_REF_EPOCH)
.to_unit(Unit::Day);
let ref_year_offset = ref_offset_days / DAYS_PER_YEAR_NLD;
year += ref_year_offset.floor() as i32;
}
year
Self::compute_gregorian(self.duration_since_j1900_tai).0
}

#[must_use]
Expand Down
17 changes: 11 additions & 6 deletions tests/epoch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1883,19 +1883,24 @@ fn regression_test_gh_272() {
assert_eq!(years, 2021);

// Check that even in GPST, we start counting the days at one.
let epoch2 = Epoch::from_str("2021-12-31T00:00:00 GPST").unwrap();
let (years, day_of_year) = epoch2.year_days_of_year();
let epoch = Epoch::from_str("2021-12-31T00:00:00 GPST").unwrap();
let (years, day_of_year) = epoch.year_days_of_year();
assert_eq!(years, 2021);
assert_eq!(day_of_year, 365.0);

let epoch2 = Epoch::from_str("2020-12-31T00:00:00 GPST").unwrap();
let (years, day_of_year) = epoch2.year_days_of_year();
let epoch = Epoch::from_str("2020-12-31T00:00:00 GPST").unwrap();
let (years, day_of_year) = epoch.year_days_of_year();
assert_eq!(years, 2020);
// 366 days in 2020, leap year.
assert_eq!(day_of_year, 366.0);

let epoch2 = Epoch::from_str("2021-01-01T00:00:00 UTC").unwrap();
let (years, day_of_year) = epoch2.year_days_of_year();
let epoch = Epoch::from_str("2021-01-01T00:00:00 UTC").unwrap();
let (years, day_of_year) = epoch.year_days_of_year();
assert_eq!(years, 2021);
assert_eq!(day_of_year, 1.0);

let epoch = Epoch::from_str("2021-01-01T00:00:00 GPST").unwrap();
let (years, day_of_year) = epoch.year_days_of_year();
assert_eq!(years, 2021);
assert_eq!(day_of_year, 1.0);
}

0 comments on commit a16a0a0

Please sign in to comment.