From 23868d74fb69cb23f336374f8a85627f6ce4fdb4 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Fri, 19 May 2023 17:34:29 +0200 Subject: [PATCH] Use `overflowing_naive_local` in methods that don't return `DateTime` --- src/datetime/mod.rs | 54 ++++++++++++++++++++++++++------------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index e96228642d..5a7dcd776d 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -372,7 +372,7 @@ impl DateTime { /// daylight saving time transition. #[must_use] pub fn checked_add_months(self, rhs: Months) -> Option> { - self.naive_local() + self.overflowing_naive_local() .checked_add_months(rhs)? .and_local_timezone(Tz::from_offset(&self.offset)) .single() @@ -405,7 +405,7 @@ impl DateTime { /// daylight saving time transition. #[must_use] pub fn checked_sub_months(self, rhs: Months) -> Option> { - self.naive_local() + self.overflowing_naive_local() .checked_sub_months(rhs)? .and_local_timezone(Tz::from_offset(&self.offset)) .single() @@ -421,7 +421,7 @@ impl DateTime { /// daylight saving time transition. #[must_use] pub fn checked_add_days(self, days: Days) -> Option { - self.naive_local() + self.overflowing_naive_local() .checked_add_days(days)? .and_local_timezone(TimeZone::from_offset(&self.offset)) .single() @@ -437,7 +437,7 @@ impl DateTime { /// daylight saving time transition. #[must_use] pub fn checked_sub_days(self, days: Days) -> Option { - self.naive_local() + self.overflowing_naive_local() .checked_sub_days(days)? .and_local_timezone(TimeZone::from_offset(&self.offset)) .single() @@ -733,8 +733,12 @@ where #[must_use] pub fn to_rfc2822(&self) -> String { let mut result = String::with_capacity(32); - crate::format::write_rfc2822(&mut result, self.naive_local(), self.offset.fix()) - .expect("writing rfc2822 datetime to string should never fail"); + crate::format::write_rfc2822( + &mut result, + self.overflowing_naive_local(), + self.offset.fix(), + ) + .expect("writing rfc2822 datetime to string should never fail"); result } @@ -744,8 +748,12 @@ where #[must_use] pub fn to_rfc3339(&self) -> String { let mut result = String::with_capacity(32); - crate::format::write_rfc3339(&mut result, self.naive_local(), self.offset.fix()) - .expect("writing rfc3339 datetime to string should never fail"); + crate::format::write_rfc3339( + &mut result, + self.overflowing_naive_local(), + self.offset.fix(), + ) + .expect("writing rfc3339 datetime to string should never fail"); result } @@ -828,7 +836,7 @@ where I: Iterator + Clone, B: Borrow>, { - let local = self.naive_local(); + let local = self.overflowing_naive_local(); DelayedFormat::new_with_offset(Some(local.date()), Some(local.time()), &self.offset, items) } @@ -897,39 +905,39 @@ where impl Datelike for DateTime { #[inline] fn year(&self) -> i32 { - self.naive_local().year() + self.overflowing_naive_local().year() } #[inline] fn month(&self) -> u32 { - self.naive_local().month() + self.overflowing_naive_local().month() } #[inline] fn month0(&self) -> u32 { - self.naive_local().month0() + self.overflowing_naive_local().month0() } #[inline] fn day(&self) -> u32 { - self.naive_local().day() + self.overflowing_naive_local().day() } #[inline] fn day0(&self) -> u32 { - self.naive_local().day0() + self.overflowing_naive_local().day0() } #[inline] fn ordinal(&self) -> u32 { - self.naive_local().ordinal() + self.overflowing_naive_local().ordinal() } #[inline] fn ordinal0(&self) -> u32 { - self.naive_local().ordinal0() + self.overflowing_naive_local().ordinal0() } #[inline] fn weekday(&self) -> Weekday { - self.naive_local().weekday() + self.overflowing_naive_local().weekday() } #[inline] fn iso_week(&self) -> IsoWeek { - self.naive_local().iso_week() + self.overflowing_naive_local().iso_week() } #[inline] @@ -1048,19 +1056,19 @@ impl Datelike for DateTime { impl Timelike for DateTime { #[inline] fn hour(&self) -> u32 { - self.naive_local().hour() + self.overflowing_naive_local().hour() } #[inline] fn minute(&self) -> u32 { - self.naive_local().minute() + self.overflowing_naive_local().minute() } #[inline] fn second(&self) -> u32 { - self.naive_local().second() + self.overflowing_naive_local().second() } #[inline] fn nanosecond(&self) -> u32 { - self.naive_local().nanosecond() + self.overflowing_naive_local().nanosecond() } /// Makes a new `DateTime` with the hour number changed. @@ -1282,7 +1290,7 @@ impl Sub for DateTime { impl fmt::Debug for DateTime { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.naive_local().fmt(f)?; + self.overflowing_naive_local().fmt(f)?; self.offset.fmt(f) } }