File tree 1 file changed +37
-1
lines changed 1 file changed +37
-1
lines changed Original file line number Diff line number Diff line change @@ -166,7 +166,43 @@ impl NaiveWeek {
166
166
#[ inline]
167
167
#[ must_use]
168
168
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
+ }
170
206
}
171
207
}
172
208
You can’t perform that action at this time.
0 commit comments