Skip to content

Commit

Permalink
Add checked_days
Browse files Browse the repository at this point in the history
  • Loading branch information
bragov4ik committed Aug 8, 2024
1 parent 09f3f5c commit e0d7ff8
Showing 1 changed file with 37 additions and 1 deletion.
38 changes: 37 additions & 1 deletion src/naive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,43 @@ impl NaiveWeek {
#[inline]
#[must_use]
pub const fn days(&self) -> RangeInclusive<NaiveDate> {
self.first_day()..=self.last_day()
// `expect` doesn't work because `RangeInclusive` is not `Copy`
match self.checked_days() {
Some(val) => val,
None => panic!("{}", "first or last weekday is out of range for `NaiveDate`"),
}
}

/// Returns an [`Option<RangeInclusive<T>>`] representing the whole week bounded by
/// [checked_first_day](NaiveWeek::checked_first_day) and
/// [checked_last_day](NaiveWeek::checked_last_day) functions.
///
/// Returns `None` if either of the boundaries are out of `NaiveDate`'s range
/// (more than ca. 262,000 years away from common era).
///
///
/// # Examples
///
/// ```
/// use chrono::{NaiveDate, Weekday};
///
/// let date = NaiveDate::MAX;
/// let week = date.week(Weekday::Mon);
/// let _days = match week.checked_days() {
/// Some(d) => d,
/// None => {
/// // error handling code
/// return;
/// }
/// };
/// ```
#[inline]
#[must_use]
pub const fn checked_days(&self) -> Option<RangeInclusive<NaiveDate>> {
match (self.checked_first_day(), self.checked_last_day()) {
(Some(first), Some(last)) => Some(first..=last),
(_, _) => None,
}
}
}

Expand Down

0 comments on commit e0d7ff8

Please sign in to comment.