Skip to content

Commit f765936

Browse files
bragov4ikdjc
authored andcommitted
Add checked_days
1 parent ccb04bc commit f765936

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

src/naive/mod.rs

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,43 @@ impl NaiveWeek {
166166
#[inline]
167167
#[must_use]
168168
pub const fn days(&self) -> RangeInclusive<NaiveDate> {
169-
self.first_day()..=self.last_day()
169+
// `expect` doesn't work because `RangeInclusive` is not `Copy`
170+
match self.checked_days() {
171+
Some(val) => val,
172+
None => panic!("{}", "first or last weekday is out of range for `NaiveDate`"),
173+
}
174+
}
175+
176+
/// Returns an [`Option<RangeInclusive<T>>`] representing the whole week bounded by
177+
/// [checked_first_day](NaiveWeek::checked_first_day) and
178+
/// [checked_last_day](NaiveWeek::checked_last_day) functions.
179+
///
180+
/// Returns `None` if either of the boundaries are out of `NaiveDate`'s range
181+
/// (more than ca. 262,000 years away from common era).
182+
///
183+
///
184+
/// # Examples
185+
///
186+
/// ```
187+
/// use chrono::{NaiveDate, Weekday};
188+
///
189+
/// let date = NaiveDate::MAX;
190+
/// let week = date.week(Weekday::Mon);
191+
/// let _days = match week.checked_days() {
192+
/// Some(d) => d,
193+
/// None => {
194+
/// // error handling code
195+
/// return;
196+
/// }
197+
/// };
198+
/// ```
199+
#[inline]
200+
#[must_use]
201+
pub const fn checked_days(&self) -> Option<RangeInclusive<NaiveDate>> {
202+
match (self.checked_first_day(), self.checked_last_day()) {
203+
(Some(first), Some(last)) => Some(first..=last),
204+
(_, _) => None,
205+
}
170206
}
171207
}
172208

0 commit comments

Comments
 (0)