@@ -1201,52 +1201,46 @@ 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 [`Error::DoesNotExist`] when the resulting date does not exist.
1205+ /// - Returns [`Error::OutOfRange`] if `year` is out of range for a `NaiveDate`.
12071206 ///
12081207 /// # Examples
12091208 ///
12101209 /// ```
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- /// );
1210+ /// # use chrono::{NaiveDate, Error};
1211+ /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8)?.with_year(2016), NaiveDate::from_ymd(2016, 9, 8));
1212+ /// assert_eq!(NaiveDate::from_ymd(2015, 9, 8)?.with_year(-308), NaiveDate::from_ymd(-308, 9, 8));
1213+ /// # Ok::<(), Error>(())
12211214 /// ```
12221215 ///
1223- /// A leap day (February 29) is a case where this method can return `None` .
1216+ /// A leap day (February 29) in a non-leap year will return [`Err(Error::DoesNotExist)`] .
12241217 ///
12251218 /// ```
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());
1219+ /// # use chrono::{NaiveDate, Error};
1220+ /// assert_eq!(NaiveDate::from_ymd(2016, 2, 29)?.with_year(2015), Err(Error::DoesNotExist));
1221+ /// assert!(NaiveDate::from_ymd(2016, 2, 29)?.with_year(2020).is_ok());
1222+ /// # Ok::<(), Error>(())
12291223 /// ```
12301224 ///
1231- /// Don't use `with_year` if you want the ordinal date to stay the same:
1225+ /// Don't use `with_year` if you want the ordinal date to stay the same.
12321226 ///
12331227 /// ```
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- /// );
1228+ /// # use chrono::{ NaiveDate, Error} ;
1229+ /// let date = NaiveDate::from_yo(2020, 100)?.with_year(2023);
1230+ /// assert_ne!(date, NaiveDate::from_yo(2023 , 100));
1231+ /// assert_eq!(date, NaiveDate::from_yo(2023, 99));
1232+ /// # Ok::<(), Error>(())
12391233 /// ```
12401234 #[ inline]
1241- pub const fn with_year ( & self , year : i32 ) -> Option < NaiveDate > {
1235+ pub const fn with_year ( & self , year : i32 ) -> Result < NaiveDate , Error > {
12421236 // we need to operate with `mdf` since we should keep the month and day number as is
12431237 let mdf = self . mdf ( ) ;
12441238
12451239 // adjust the flags as needed
12461240 let flags = YearFlags :: from_year ( year) ;
12471241 let mdf = mdf. with_flags ( flags) ;
12481242
1249- ok ! ( NaiveDate :: from_mdf( year, mdf) )
1243+ NaiveDate :: from_mdf ( year, mdf)
12501244 }
12511245
12521246 /// Makes a new `NaiveDate` with the month number (starting from 1) changed.
@@ -1276,7 +1270,7 @@ impl NaiveDate {
12761270 /// use chrono::{Datelike, Error, NaiveDate};
12771271 ///
12781272 /// fn with_year_month(date: NaiveDate, year: i32, month: u32) -> Option<NaiveDate> {
1279- /// date.with_year(year)?.with_month(month)
1273+ /// date.with_year(year).ok() ?.with_month(month)
12801274 /// }
12811275 /// let d = NaiveDate::from_ymd(2020, 2, 29).unwrap();
12821276 /// assert!(with_year_month(d, 2019, 1).is_none()); // fails because of invalid intermediate value
0 commit comments