Skip to content

Commit

Permalink
refactor(datetime,calendar): Move week_of_month implementation detail…
Browse files Browse the repository at this point in the history
…s into calendar.
  • Loading branch information
mildgravitas committed Mar 16, 2022
1 parent 09294f8 commit 874e818
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 14 deletions.
50 changes: 50 additions & 0 deletions components/calendar/src/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,31 @@ pub mod week_of {
}
}

/// Computes & returns the week of given month or year accoding to a calendar with min_week_days = 1.
///
/// # Arguments
/// - first_weekday: The first day of a week.
/// - day: 1-based day of the month or year.
/// - week_day: The weekday of `day`.
pub fn simple_week_of(first_weekday: IsoWeekday, day: u16, week_day: IsoWeekday) -> u16 {
let calendar = CalendarInfo {
first_weekday,
min_week_days: 1,
};

week_of(
&calendar,
// The duration of the current and previous unit does not influence the result if min_week_days = 1
// so we only need to use a valid value.
MIN_UNIT_DAYS,
MIN_UNIT_DAYS,
day,
week_day,
)
.expect("week_of should can't fail with MIN_UNIT_DAYS")
.week
}

#[cfg(test)]
mod tests {
use super::{week_of, CalendarInfo, RelativeUnit, RelativeWeek, UnitInfo, WeekOf};
Expand Down Expand Up @@ -425,4 +450,29 @@ pub mod week_of {
Ok(())
}
}

#[test]
fn test_simple_week_of() {
// The 1st is a Monday and the week starts on Mondays.
assert_eq!(
simple_week_of(IsoWeekday::Monday, 2, IsoWeekday::Tuesday),
1
);
assert_eq!(simple_week_of(IsoWeekday::Monday, 7, IsoWeekday::Sunday), 1);
assert_eq!(simple_week_of(IsoWeekday::Monday, 8, IsoWeekday::Monday), 2);

// The 1st is a Wednesday and the week starts on Tuesdays.
assert_eq!(
simple_week_of(IsoWeekday::Tuesday, 1, IsoWeekday::Wednesday),
1
);
assert_eq!(
simple_week_of(IsoWeekday::Tuesday, 6, IsoWeekday::Monday),
1
);
assert_eq!(
simple_week_of(IsoWeekday::Tuesday, 7, IsoWeekday::Tuesday),
2
);
}
}
18 changes: 4 additions & 14 deletions components/datetime/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,28 +175,18 @@ fn week_of_month<T: DateInput>(
datetime: &T,
first_weekday: IsoWeekday,
) -> Result<WeekOfMonth, DateTimeError> {
// This value isn't used by week_of() given that we use a calendar with min_week_days = 1.
const UNUSED_DAYS_IN_MONTH: u16 = 60;

let calendar = week_of::CalendarInfo {
first_weekday,
min_week_days: 1,
};

let day_of_month = datetime
.day_of_month()
.ok_or(DateTimeError::MissingInput("DateTimeInput::day_of_month"))?;

let week = week_of::week_of(
&calendar,
UNUSED_DAYS_IN_MONTH,
UNUSED_DAYS_IN_MONTH,
let week = week_of::simple_week_of(
first_weekday,
day_of_month.0 as u16,
datetime
.iso_weekday()
.ok_or(DateTimeError::MissingInput("DateTimeInput::iso_weekday"))?,
)?;
Ok(WeekOfMonth(u32::from(week.week)))
);
Ok(WeekOfMonth(u32::from(week)))
}

impl<'data, T: DateTimeInput> DateTimeInputWithLocale<'data, T> {
Expand Down

0 comments on commit 874e818

Please sign in to comment.