From 85a1ebce850b2310ac6c6c71fd38634cf257aa59 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 32dd025740..fcd8876f4c 100644 --- a/src/datetime/mod.rs +++ b/src/datetime/mod.rs @@ -1166,22 +1166,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 } } @@ -1236,8 +1227,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 } }