@@ -1201,52 +1201,47 @@ impl NaiveDate {
12011201 ///
12021202 /// # Errors
12031203 ///
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`.
1204+ /// Returns:
1205+ /// - [`Error::DoesNotExist`] if the resulting date does not exist.
1206+ /// - [`Error::OutOfRange`] if ` year` is out of range for a `NaiveDate`.
12071207 ///
12081208 /// # Examples
12091209 ///
12101210 /// ```
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- /// );
1211+ /// # use chrono::{NaiveDate, Error};
1212+ /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8)?.with_year(2016), NaiveDate::from_ymd(2016, 9, 8));
1213+ /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8)?.with_year(-308), NaiveDate::from_ymd(-308, 9, 8));
1214+ /// # Ok::<(), Error>(())
12211215 /// ```
12221216 ///
1223- /// A leap day (February 29) is a case where this method can return `None` .
1217+ /// A leap day (February 29) in a non-leap year will return [`Err(Error::DoesNotExist)`] .
12241218 ///
12251219 /// ```
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());
1220+ /// # use chrono::{NaiveDate, Error};
1221+ /// assert_eq!(NaiveDate::from_ymd(2016, 2, 29)?.with_year(2015), Err(Error::DoesNotExist));
1222+ /// assert!(NaiveDate::from_ymd(2016, 2, 29)?.with_year(2020).is_ok());
1223+ /// # Ok::<(), Error>(())
12291224 /// ```
12301225 ///
1231- /// Don't use `with_year` if you want the ordinal date to stay the same:
1226+ /// Don't use `with_year` if you want the ordinal date to stay the same.
12321227 ///
12331228 /// ```
1234- /// # use chrono::NaiveDate;
1235- /// assert_ne!(
1236- /// NaiveDate::from_yo(2020 , 100).unwrap().with_year(2023).unwrap(),
1237- /// NaiveDate::from_yo(2023, 100).unwrap() // result is 2023-101
1238- /// );
1229+ /// # use chrono::{ NaiveDate, Error} ;
1230+ /// let date = NaiveDate::from_yo(2020, 100)?.with_year(2023);
1231+ /// assert_ne!(date, NaiveDate::from_yo(2023 , 100));
1232+ /// assert_eq!(date, NaiveDate::from_yo(2023, 99));
1233+ /// # Ok::<(), Error>(())
12391234 /// ```
12401235 #[ inline]
1241- pub const fn with_year ( & self , year : i32 ) -> Option < NaiveDate > {
1236+ pub const fn with_year ( & self , year : i32 ) -> Result < NaiveDate , Error > {
12421237 // we need to operate with `mdf` since we should keep the month and day number as is
12431238 let mdf = self . mdf ( ) ;
12441239
12451240 // adjust the flags as needed
12461241 let flags = YearFlags :: from_year ( year) ;
12471242 let mdf = mdf. with_flags ( flags) ;
12481243
1249- ok ! ( NaiveDate :: from_mdf( year, mdf) )
1244+ NaiveDate :: from_mdf ( year, mdf)
12501245 }
12511246
12521247 /// Makes a new `NaiveDate` with the month number (starting from 1) changed.
@@ -1276,7 +1271,7 @@ impl NaiveDate {
12761271 /// use chrono::{Datelike, Error, NaiveDate};
12771272 ///
12781273 /// fn with_year_month(date: NaiveDate, year: i32, month: u32) -> Option<NaiveDate> {
1279- /// date.with_year(year)?.with_month(month)
1274+ /// date.with_year(year).ok() ?.with_month(month)
12801275 /// }
12811276 /// let d = NaiveDate::from_ymd(2020, 2, 29).unwrap();
12821277 /// assert!(with_year_month(d, 2019, 1).is_none()); // fails because of invalid intermediate value
0 commit comments