diff --git a/src/date.rs b/src/date.rs index be2f05c5..a66882ce 100644 --- a/src/date.rs +++ b/src/date.rs @@ -13,15 +13,13 @@ use core::{fmt, hash}; #[cfg(feature = "rkyv")] use rkyv::{Archive, Deserialize, Serialize}; -use crate::duration::Duration as OldDuration; #[cfg(all(feature = "unstable-locales", feature = "alloc"))] use crate::format::Locale; #[cfg(feature = "alloc")] use crate::format::{DelayedFormat, Item, StrftimeItems}; use crate::naive::{IsoWeek, NaiveDate, NaiveTime}; use crate::offset::{TimeZone, Utc}; -use crate::DateTime; -use crate::{Datelike, Weekday}; +use crate::{DateTime, Datelike, TimeDelta, Weekday}; /// ISO 8601 calendar date with time zone. /// @@ -53,7 +51,7 @@ use crate::{Datelike, Weekday}; /// /// - The date is timezone-agnostic up to one day (i.e. practically always), /// so the local date and UTC date should be equal for most cases -/// even though the raw calculation between `NaiveDate` and `Duration` may not. +/// even though the raw calculation between `NaiveDate` and `TimeDelta` may not. #[deprecated(since = "0.4.23", note = "Use `NaiveDate` or `DateTime` instead")] #[derive(Clone)] #[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] @@ -259,34 +257,34 @@ impl Date { tz.from_utc_date(&self.date) } - /// Adds given `Duration` to the current date. + /// Adds given `TimeDelta` to the current date. /// /// Returns `None` when it will result in overflow. #[inline] #[must_use] - pub fn checked_add_signed(self, rhs: OldDuration) -> Option> { + pub fn checked_add_signed(self, rhs: TimeDelta) -> Option> { let date = self.date.checked_add_signed(rhs)?; Some(Date { date, offset: self.offset }) } - /// Subtracts given `Duration` from the current date. + /// Subtracts given `TimeDelta` from the current date. /// /// Returns `None` when it will result in overflow. #[inline] #[must_use] - pub fn checked_sub_signed(self, rhs: OldDuration) -> Option> { + pub fn checked_sub_signed(self, rhs: TimeDelta) -> Option> { let date = self.date.checked_sub_signed(rhs)?; Some(Date { date, offset: self.offset }) } /// Subtracts another `Date` from the current date. - /// Returns a `Duration` of integral numbers. + /// Returns a `TimeDelta` of integral numbers. /// /// This does not overflow or underflow at all, - /// as all possible output fits in the range of `Duration`. + /// as all possible output fits in the range of `TimeDelta`. #[inline] #[must_use] - pub fn signed_duration_since(self, rhs: Date) -> OldDuration { + pub fn signed_duration_since(self, rhs: Date) -> TimeDelta { self.date.signed_duration_since(rhs.date) } @@ -495,43 +493,43 @@ impl hash::Hash for Date { } } -impl Add for Date { +impl Add for Date { type Output = Date; #[inline] - fn add(self, rhs: OldDuration) -> Date { - self.checked_add_signed(rhs).expect("`Date + Duration` overflowed") + fn add(self, rhs: TimeDelta) -> Date { + self.checked_add_signed(rhs).expect("`Date + TimeDelta` overflowed") } } -impl AddAssign for Date { +impl AddAssign for Date { #[inline] - fn add_assign(&mut self, rhs: OldDuration) { - self.date = self.date.checked_add_signed(rhs).expect("`Date + Duration` overflowed"); + fn add_assign(&mut self, rhs: TimeDelta) { + self.date = self.date.checked_add_signed(rhs).expect("`Date + TimeDelta` overflowed"); } } -impl Sub for Date { +impl Sub for Date { type Output = Date; #[inline] - fn sub(self, rhs: OldDuration) -> Date { - self.checked_sub_signed(rhs).expect("`Date - Duration` overflowed") + fn sub(self, rhs: TimeDelta) -> Date { + self.checked_sub_signed(rhs).expect("`Date - TimeDelta` overflowed") } } -impl SubAssign for Date { +impl SubAssign for Date { #[inline] - fn sub_assign(&mut self, rhs: OldDuration) { - self.date = self.date.checked_sub_signed(rhs).expect("`Date - Duration` overflowed"); + fn sub_assign(&mut self, rhs: TimeDelta) { + self.date = self.date.checked_sub_signed(rhs).expect("`Date - TimeDelta` overflowed"); } } impl Sub> for Date { - type Output = OldDuration; + type Output = TimeDelta; #[inline] - fn sub(self, rhs: Date) -> OldDuration { + fn sub(self, rhs: Date) -> TimeDelta { self.signed_duration_since(rhs) } } @@ -572,8 +570,7 @@ where mod tests { use super::Date; - use crate::duration::Duration; - use crate::{FixedOffset, NaiveDate, Utc}; + use crate::{FixedOffset, NaiveDate, TimeDelta, Utc}; #[cfg(feature = "clock")] use crate::offset::{Local, TimeZone}; @@ -584,15 +581,15 @@ mod tests { const WEEKS_PER_YEAR: f32 = 52.1775; // This is always at least one year because 1 year = 52.1775 weeks. - let one_year_ago = Utc::today() - Duration::weeks((WEEKS_PER_YEAR * 1.5).ceil() as i64); + let one_year_ago = Utc::today() - TimeDelta::weeks((WEEKS_PER_YEAR * 1.5).ceil() as i64); // A bit more than 2 years. - let two_year_ago = Utc::today() - Duration::weeks((WEEKS_PER_YEAR * 2.5).ceil() as i64); + let two_year_ago = Utc::today() - TimeDelta::weeks((WEEKS_PER_YEAR * 2.5).ceil() as i64); assert_eq!(Utc::today().years_since(one_year_ago), Some(1)); assert_eq!(Utc::today().years_since(two_year_ago), Some(2)); // If the given DateTime is later than now, the function will always return 0. - let future = Utc::today() + Duration::weeks(12); + let future = Utc::today() + TimeDelta::weeks(12); assert_eq!(Utc::today().years_since(future), None); } @@ -602,20 +599,20 @@ mod tests { let date = Date::::from_utc(naivedate, Utc); let mut date_add = date; - date_add += Duration::days(5); - assert_eq!(date_add, date + Duration::days(5)); + date_add += TimeDelta::days(5); + assert_eq!(date_add, date + TimeDelta::days(5)); let timezone = FixedOffset::east_opt(60 * 60).unwrap(); let date = date.with_timezone(&timezone); let date_add = date_add.with_timezone(&timezone); - assert_eq!(date_add, date + Duration::days(5)); + assert_eq!(date_add, date + TimeDelta::days(5)); let timezone = FixedOffset::west_opt(2 * 60 * 60).unwrap(); let date = date.with_timezone(&timezone); let date_add = date_add.with_timezone(&timezone); - assert_eq!(date_add, date + Duration::days(5)); + assert_eq!(date_add, date + TimeDelta::days(5)); } #[test] @@ -626,8 +623,8 @@ mod tests { let date = Local.from_utc_date(&naivedate); let mut date_add = date; - date_add += Duration::days(5); - assert_eq!(date_add, date + Duration::days(5)); + date_add += TimeDelta::days(5); + assert_eq!(date_add, date + TimeDelta::days(5)); } #[test] @@ -636,20 +633,20 @@ mod tests { let date = Date::::from_utc(naivedate, Utc); let mut date_sub = date; - date_sub -= Duration::days(5); - assert_eq!(date_sub, date - Duration::days(5)); + date_sub -= TimeDelta::days(5); + assert_eq!(date_sub, date - TimeDelta::days(5)); let timezone = FixedOffset::east_opt(60 * 60).unwrap(); let date = date.with_timezone(&timezone); let date_sub = date_sub.with_timezone(&timezone); - assert_eq!(date_sub, date - Duration::days(5)); + assert_eq!(date_sub, date - TimeDelta::days(5)); let timezone = FixedOffset::west_opt(2 * 60 * 60).unwrap(); let date = date.with_timezone(&timezone); let date_sub = date_sub.with_timezone(&timezone); - assert_eq!(date_sub, date - Duration::days(5)); + assert_eq!(date_sub, date - TimeDelta::days(5)); } #[test] @@ -660,7 +657,7 @@ mod tests { let date = Local.from_utc_date(&naivedate); let mut date_sub = date; - date_sub -= Duration::days(5); - assert_eq!(date_sub, date - Duration::days(5)); + date_sub -= TimeDelta::days(5); + assert_eq!(date_sub, date - TimeDelta::days(5)); } }