@@ -1195,58 +1195,51 @@ impl NaiveDate {
11951195
11961196 /// Makes a new `NaiveDate` with the year number changed, while keeping the same month and day.
11971197 ///
1198- /// This method assumes you want to work on the date as a year-month-day value. Don't use it if
1199- /// you want the ordinal to stay the same after changing the year, of if you want the week and
1200- /// weekday values to stay the same.
1201- ///
12021198 /// # Errors
12031199 ///
1204- /// Returns `None` if:
1205- /// - The resulting date does not exist (February 29 in a non-leap year).
1206- /// - The year is out of range for a `NaiveDate`.
1200+ /// - Returns [`Error::DoesNotExist`] when the resulting date does not exist.
1201+ /// - Returns [`Error::OutOfRange`] if `year` is out of range for a `NaiveDate`.
12071202 ///
12081203 /// # Examples
12091204 ///
12101205 /// ```
1211- /// use chrono::NaiveDate;
1212- ///
1213- /// assert_eq!(
1214- /// NaiveDate::from_ymd(2015, 9, 8).unwrap().with_year(2016),
1215- /// Some(NaiveDate::from_ymd(2016, 9, 8).unwrap())
1216- /// );
1217- /// assert_eq!(
1218- /// NaiveDate::from_ymd(2015, 9, 8).unwrap().with_year(-308),
1219- /// Some(NaiveDate::from_ymd(-308, 9, 8).unwrap())
1220- /// );
1206+ /// # use chrono::{NaiveDate, Error};
1207+ /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8)?.with_year(2016),
1208+ /// NaiveDate::from_ymd(2016, 9, 8));
1209+ /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8)?.with_year(-308),
1210+ /// NaiveDate::from_ymd(-308, 9, 8));
1211+ /// # Ok::<(), Error>(())
12211212 /// ```
12221213 ///
1223- /// A leap day (February 29) is a case where this method can return `None` .
1214+ /// A leap day (February 29) in a non-leap year will return [`Err(Error::DoesNotExist)`] .
12241215 ///
12251216 /// ```
1226- /// # use chrono::NaiveDate;
1227- /// assert!(NaiveDate::from_ymd(2016, 2, 29).unwrap().with_year(2015).is_none());
1228- /// assert!(NaiveDate::from_ymd(2016, 2, 29).unwrap().with_year(2020).is_some());
1217+ /// # use chrono::{NaiveDate, Error};
1218+ /// assert!(NaiveDate::from_ymd(2016, 2, 29)?.with_year(2015).is_err());
1219+ /// assert!(NaiveDate::from_ymd(2016, 2, 29)?.with_year(2020).is_ok());
1220+ /// # Ok::<(), Error>(())
12291221 /// ```
12301222 ///
1231- /// Don't use `with_year` if you want the ordinal date to stay the same:
1223+ /// Don't use `with_year` if you want the ordinal date to stay the same.
12321224 ///
12331225 /// ```
1234- /// # use chrono::NaiveDate;
1226+ /// # use chrono::{ NaiveDate, Error} ;
12351227 /// assert_ne!(
1236- /// NaiveDate::from_yo(2020, 100).unwrap().with_year(2023).unwrap() ,
1228+ /// NaiveDate::from_yo(2020, 100).unwrap().with_year(2023)? ,
12371229 /// NaiveDate::from_yo(2023, 100).unwrap() // result is 2023-101
12381230 /// );
1231+ /// # Ok::<(), Error>(())
12391232 /// ```
12401233 #[ inline]
1241- pub const fn with_year ( & self , year : i32 ) -> Option < NaiveDate > {
1234+ pub const fn with_year ( & self , year : i32 ) -> Result < NaiveDate , Error > {
12421235 // we need to operate with `mdf` since we should keep the month and day number as is
12431236 let mdf = self . mdf ( ) ;
12441237
12451238 // adjust the flags as needed
12461239 let flags = YearFlags :: from_year ( year) ;
12471240 let mdf = mdf. with_flags ( flags) ;
12481241
1249- ok ! ( NaiveDate :: from_mdf( year, mdf) )
1242+ NaiveDate :: from_mdf ( year, mdf)
12501243 }
12511244
12521245 /// Makes a new `NaiveDate` with the month number (starting from 1) changed.
@@ -1276,7 +1269,7 @@ impl NaiveDate {
12761269 /// use chrono::{Datelike, Error, NaiveDate};
12771270 ///
12781271 /// fn with_year_month(date: NaiveDate, year: i32, month: u32) -> Option<NaiveDate> {
1279- /// date.with_year(year)?.with_month(month)
1272+ /// date.with_year(year).ok() ?.with_month(month)
12801273 /// }
12811274 /// let d = NaiveDate::from_ymd(2020, 2, 29).unwrap();
12821275 /// assert!(with_year_month(d, 2019, 1).is_none()); // fails because of invalid intermediate value
0 commit comments