Skip to content

Commit

Permalink
Use overflowing_naive_local in methods that don't return DateTime
Browse files Browse the repository at this point in the history
  • Loading branch information
pitdicker committed Jun 8, 2023
1 parent 7f89920 commit 23868d7
Showing 1 changed file with 31 additions and 23 deletions.
54 changes: 31 additions & 23 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// daylight saving time transition.
#[must_use]
pub fn checked_add_months(self, rhs: Months) -> Option<DateTime<Tz>> {
self.naive_local()
self.overflowing_naive_local()
.checked_add_months(rhs)?
.and_local_timezone(Tz::from_offset(&self.offset))
.single()
Expand Down Expand Up @@ -405,7 +405,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// daylight saving time transition.
#[must_use]
pub fn checked_sub_months(self, rhs: Months) -> Option<DateTime<Tz>> {
self.naive_local()
self.overflowing_naive_local()
.checked_sub_months(rhs)?
.and_local_timezone(Tz::from_offset(&self.offset))
.single()
Expand All @@ -421,7 +421,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// daylight saving time transition.
#[must_use]
pub fn checked_add_days(self, days: Days) -> Option<Self> {
self.naive_local()
self.overflowing_naive_local()
.checked_add_days(days)?
.and_local_timezone(TimeZone::from_offset(&self.offset))
.single()
Expand All @@ -437,7 +437,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// daylight saving time transition.
#[must_use]
pub fn checked_sub_days(self, days: Days) -> Option<Self> {
self.naive_local()
self.overflowing_naive_local()
.checked_sub_days(days)?
.and_local_timezone(TimeZone::from_offset(&self.offset))
.single()
Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -828,7 +836,7 @@ where
I: Iterator<Item = B> + Clone,
B: Borrow<Item<'a>>,
{
let local = self.naive_local();
let local = self.overflowing_naive_local();
DelayedFormat::new_with_offset(Some(local.date()), Some(local.time()), &self.offset, items)
}

Expand Down Expand Up @@ -897,39 +905,39 @@ where
impl<Tz: TimeZone> Datelike for DateTime<Tz> {
#[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]
Expand Down Expand Up @@ -1048,19 +1056,19 @@ impl<Tz: TimeZone> Datelike for DateTime<Tz> {
impl<Tz: TimeZone> Timelike for DateTime<Tz> {
#[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.
Expand Down Expand Up @@ -1282,7 +1290,7 @@ impl<Tz: TimeZone> Sub<Days> for DateTime<Tz> {

impl<Tz: TimeZone> fmt::Debug for DateTime<Tz> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.naive_local().fmt(f)?;
self.overflowing_naive_local().fmt(f)?;
self.offset.fmt(f)
}
}
Expand Down

0 comments on commit 23868d7

Please sign in to comment.