From d20c7805053e39add24f9d96553ecb18c6e98db3 Mon Sep 17 00:00:00 2001 From: Paul Dicker Date: Fri, 19 May 2023 14:52:26 +0200 Subject: [PATCH] Use `checked_(add|sub)_offset` in `Add` and `Sub` impls of `DateTime` --- src/datetime/mod.rs | 20 ++++++-------------- 1 file changed, 6 insertions(+), 14 deletions(-) diff --git a/src/datetime/mod.rs b/src/datetime/mod.rs index f9499ede43..975b28cd61 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -1007,22 +1007,13 @@ impl AddAssign for DateTime { } } -fn add_with_leapsecond(lhs: &T, rhs: i32) -> T -where - T: Timelike + Add, -{ - // extract and temporarily remove the fractional part and later recover it - let nanos = lhs.nanosecond(); - let lhs = lhs.with_nanosecond(0).unwrap(); - (lhs + OldDuration::seconds(i64::from(rhs))).with_nanosecond(nanos).unwrap() -} - impl Add for DateTime { type Output = DateTime; #[inline] - fn add(self, rhs: FixedOffset) -> DateTime { - add_with_leapsecond(&self, rhs.local_minus_utc()) + fn add(mut self, rhs: FixedOffset) -> DateTime { + self.datetime = self.naive_utc().checked_add_offset(rhs).unwrap(); + self } } @@ -1057,8 +1048,9 @@ impl Sub for DateTime { type Output = DateTime; #[inline] - fn sub(self, rhs: FixedOffset) -> DateTime { - add_with_leapsecond(&self, -rhs.local_minus_utc()) + fn sub(mut self, rhs: FixedOffset) -> DateTime { + self.datetime = self.naive_utc().checked_sub_offset(rhs).unwrap(); + self } }