Skip to content

Commit

Permalink
fix IsoWeek so that its flags are always correct
Browse files Browse the repository at this point in the history
This fixes #295. PartialEq and PartialOrd are implemented by directly
comparing the internal integer field, which includes the flags value.
  • Loading branch information
moshevds authored and djc committed Mar 16, 2023
1 parent 9d37649 commit dc9ea3a
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion src/naive/isoweek.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ pub(super) fn iso_week_from_yof(year: i32, of: Of) -> IsoWeek {
(year, rawweek)
}
};
IsoWeek { ywf: (year << 10) | (week << 4) as DateImpl | DateImpl::from(of.flags().0) }
let flags = YearFlags::from_year(year);
IsoWeek { ywf: (year << 10) | (week << 4) as DateImpl | DateImpl::from(flags.0) }
}

impl IsoWeek {
Expand Down Expand Up @@ -164,4 +165,38 @@ mod tests {
assert_eq!(maxweek.week0(), 0);
assert_eq!(format!("{:?}", maxweek), NaiveDate::MAX.format("%G-W%V").to_string());
}

#[test]
fn test_iso_week_equivalence_for_first_week() {
let monday = NaiveDate::from_ymd_opt(2024, 12, 30).unwrap();
let friday = NaiveDate::from_ymd_opt(2025, 1, 3).unwrap();

assert_eq!(monday.iso_week(), friday.iso_week());
}

#[test]
fn test_iso_week_equivalence_for_last_week() {
let monday = NaiveDate::from_ymd_opt(2026, 12, 28).unwrap();
let friday = NaiveDate::from_ymd_opt(2027, 1, 1).unwrap();

assert_eq!(monday.iso_week(), friday.iso_week());
}

#[test]
fn test_iso_week_ordering_for_first_week() {
let monday = NaiveDate::from_ymd_opt(2024, 12, 30).unwrap();
let friday = NaiveDate::from_ymd_opt(2025, 1, 3).unwrap();

assert!(monday.iso_week() >= friday.iso_week());
assert!(monday.iso_week() <= friday.iso_week());
}

#[test]
fn test_iso_week_ordering_for_last_week() {
let monday = NaiveDate::from_ymd_opt(2026, 12, 28).unwrap();
let friday = NaiveDate::from_ymd_opt(2027, 1, 1).unwrap();

assert!(monday.iso_week() >= friday.iso_week());
assert!(monday.iso_week() <= friday.iso_week());
}
}

0 comments on commit dc9ea3a

Please sign in to comment.