Skip to content

Commit

Permalink
Merge num_days_from with days_since
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Mar 1, 2024
1 parent 2120775 commit f8a1e5b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 43 deletions.
2 changes: 1 addition & 1 deletion src/naive/date/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ impl arbitrary::Arbitrary<'_> for NaiveDate {

impl NaiveDate {
pub(crate) fn weeks_from(&self, day: Weekday) -> i32 {
(self.ordinal() as i32 - self.weekday().num_days_from(day) as i32 + 6) / 7
(self.ordinal() as i32 - self.weekday().days_since(day) as i32 + 6) / 7
}

/// Makes a new `NaiveDate` from year, ordinal and flags.
Expand Down
68 changes: 26 additions & 42 deletions src/weekday.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,9 @@ impl Weekday {
/// assert_eq!(Sun.days_since(Tue), 5);
/// assert_eq!(Wed.days_since(Sun), 3);
/// ```
pub fn days_since(&self, other: Weekday) -> u32 {
let lhs = self.num_days_from_monday();
let rhs = other.num_days_from_monday();
pub const fn days_since(&self, other: Weekday) -> u32 {
let lhs = *self as u32;
let rhs = other as u32;
if lhs < rhs {
7 + lhs - rhs
} else {
Expand All @@ -122,7 +122,7 @@ impl Weekday {
/// `w.number_from_monday()`: | 1 | 2 | 3 | 4 | 5 | 6 | 7
#[inline]
pub const fn number_from_monday(&self) -> u32 {
self.num_days_from(Weekday::Mon) + 1
self.days_since(Weekday::Mon) + 1
}

/// Returns a day-of-week number starting from Sunday = 1.
Expand All @@ -132,7 +132,7 @@ impl Weekday {
/// `w.number_from_sunday()`: | 2 | 3 | 4 | 5 | 6 | 7 | 1
#[inline]
pub const fn number_from_sunday(&self) -> u32 {
self.num_days_from(Weekday::Sun) + 1
self.days_since(Weekday::Sun) + 1
}

/// Returns a day-of-week number starting from Monday = 0.
Expand All @@ -155,7 +155,7 @@ impl Weekday {
/// ```
#[inline]
pub const fn num_days_from_monday(&self) -> u32 {
self.num_days_from(Weekday::Mon)
self.days_since(Weekday::Mon)
}

/// Returns a day-of-week number starting from Sunday = 0.
Expand All @@ -165,17 +165,7 @@ impl Weekday {
/// `w.num_days_from_sunday()`: | 1 | 2 | 3 | 4 | 5 | 6 | 0
#[inline]
pub const fn num_days_from_sunday(&self) -> u32 {
self.num_days_from(Weekday::Sun)
}

/// Returns a day-of-week number starting from the parameter `day` (D) = 0.
///
/// `w`: | `D` | `D+1` | `D+2` | `D+3` | `D+4` | `D+5` | `D+6`
/// --------------------------- | ----- | ----- | ----- | ----- | ----- | ----- | -----
/// `w.num_days_from(wd)`: | 0 | 1 | 2 | 3 | 4 | 5 | 6
#[inline]
pub(crate) const fn num_days_from(&self, day: Weekday) -> u32 {
(*self as u32 + 7 - day as u32) % 7
self.days_since(Weekday::Sun)
}
}

Expand Down Expand Up @@ -316,34 +306,28 @@ mod tests {
use super::Weekday;

#[test]
fn test_num_days_from() {
fn test_days_since() {
for i in 0..7 {
let base_day = Weekday::try_from(i).unwrap();

assert_eq!(base_day.num_days_from_monday(), base_day.num_days_from(Weekday::Mon));
assert_eq!(base_day.num_days_from_sunday(), base_day.num_days_from(Weekday::Sun));

assert_eq!(base_day.num_days_from(base_day), 0);

assert_eq!(base_day.num_days_from(base_day.pred()), 1);
assert_eq!(base_day.num_days_from(base_day.pred().pred()), 2);
assert_eq!(base_day.num_days_from(base_day.pred().pred().pred()), 3);
assert_eq!(base_day.num_days_from(base_day.pred().pred().pred().pred()), 4);
assert_eq!(base_day.num_days_from(base_day.pred().pred().pred().pred().pred()), 5);
assert_eq!(
base_day.num_days_from(base_day.pred().pred().pred().pred().pred().pred()),
6
);

assert_eq!(base_day.num_days_from(base_day.succ()), 6);
assert_eq!(base_day.num_days_from(base_day.succ().succ()), 5);
assert_eq!(base_day.num_days_from(base_day.succ().succ().succ()), 4);
assert_eq!(base_day.num_days_from(base_day.succ().succ().succ().succ()), 3);
assert_eq!(base_day.num_days_from(base_day.succ().succ().succ().succ().succ()), 2);
assert_eq!(
base_day.num_days_from(base_day.succ().succ().succ().succ().succ().succ()),
1
);
assert_eq!(base_day.num_days_from_monday(), base_day.days_since(Weekday::Mon));
assert_eq!(base_day.num_days_from_sunday(), base_day.days_since(Weekday::Sun));

assert_eq!(base_day.days_since(base_day), 0);

assert_eq!(base_day.days_since(base_day.pred()), 1);
assert_eq!(base_day.days_since(base_day.pred().pred()), 2);
assert_eq!(base_day.days_since(base_day.pred().pred().pred()), 3);
assert_eq!(base_day.days_since(base_day.pred().pred().pred().pred()), 4);
assert_eq!(base_day.days_since(base_day.pred().pred().pred().pred().pred()), 5);
assert_eq!(base_day.days_since(base_day.pred().pred().pred().pred().pred().pred()), 6);

assert_eq!(base_day.days_since(base_day.succ()), 6);
assert_eq!(base_day.days_since(base_day.succ().succ()), 5);
assert_eq!(base_day.days_since(base_day.succ().succ().succ()), 4);
assert_eq!(base_day.days_since(base_day.succ().succ().succ().succ()), 3);
assert_eq!(base_day.days_since(base_day.succ().succ().succ().succ().succ()), 2);
assert_eq!(base_day.days_since(base_day.succ().succ().succ().succ().succ().succ()), 1);
}
}

Expand Down

0 comments on commit f8a1e5b

Please sign in to comment.