Rename Duration to TimeDelta in date module

This commit is contained in:
Paul Dicker 2024-02-02 19:49:10 +01:00 committed by Paul Dicker
parent 3bd9a90e91
commit 3a9d8a3a3c

View File

@ -13,15 +13,13 @@ use core::{fmt, hash};
#[cfg(feature = "rkyv")] #[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize}; use rkyv::{Archive, Deserialize, Serialize};
use crate::duration::Duration as OldDuration;
#[cfg(all(feature = "unstable-locales", feature = "alloc"))] #[cfg(all(feature = "unstable-locales", feature = "alloc"))]
use crate::format::Locale; use crate::format::Locale;
#[cfg(feature = "alloc")] #[cfg(feature = "alloc")]
use crate::format::{DelayedFormat, Item, StrftimeItems}; use crate::format::{DelayedFormat, Item, StrftimeItems};
use crate::naive::{IsoWeek, NaiveDate, NaiveTime}; use crate::naive::{IsoWeek, NaiveDate, NaiveTime};
use crate::offset::{TimeZone, Utc}; use crate::offset::{TimeZone, Utc};
use crate::DateTime; use crate::{DateTime, Datelike, TimeDelta, Weekday};
use crate::{Datelike, Weekday};
/// ISO 8601 calendar date with time zone. /// 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), /// - 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 /// 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<Tz>` instead")] #[deprecated(since = "0.4.23", note = "Use `NaiveDate` or `DateTime<Tz>` instead")]
#[derive(Clone)] #[derive(Clone)]
#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))] #[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
@ -259,34 +257,34 @@ impl<Tz: TimeZone> Date<Tz> {
tz.from_utc_date(&self.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. /// Returns `None` when it will result in overflow.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn checked_add_signed(self, rhs: OldDuration) -> Option<Date<Tz>> { pub fn checked_add_signed(self, rhs: TimeDelta) -> Option<Date<Tz>> {
let date = self.date.checked_add_signed(rhs)?; let date = self.date.checked_add_signed(rhs)?;
Some(Date { date, offset: self.offset }) 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. /// Returns `None` when it will result in overflow.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<Date<Tz>> { pub fn checked_sub_signed(self, rhs: TimeDelta) -> Option<Date<Tz>> {
let date = self.date.checked_sub_signed(rhs)?; let date = self.date.checked_sub_signed(rhs)?;
Some(Date { date, offset: self.offset }) Some(Date { date, offset: self.offset })
} }
/// Subtracts another `Date` from the current date. /// 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, /// 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] #[inline]
#[must_use] #[must_use]
pub fn signed_duration_since<Tz2: TimeZone>(self, rhs: Date<Tz2>) -> OldDuration { pub fn signed_duration_since<Tz2: TimeZone>(self, rhs: Date<Tz2>) -> TimeDelta {
self.date.signed_duration_since(rhs.date) self.date.signed_duration_since(rhs.date)
} }
@ -495,43 +493,43 @@ impl<Tz: TimeZone> hash::Hash for Date<Tz> {
} }
} }
impl<Tz: TimeZone> Add<OldDuration> for Date<Tz> { impl<Tz: TimeZone> Add<TimeDelta> for Date<Tz> {
type Output = Date<Tz>; type Output = Date<Tz>;
#[inline] #[inline]
fn add(self, rhs: OldDuration) -> Date<Tz> { fn add(self, rhs: TimeDelta) -> Date<Tz> {
self.checked_add_signed(rhs).expect("`Date + Duration` overflowed") self.checked_add_signed(rhs).expect("`Date + TimeDelta` overflowed")
} }
} }
impl<Tz: TimeZone> AddAssign<OldDuration> for Date<Tz> { impl<Tz: TimeZone> AddAssign<TimeDelta> for Date<Tz> {
#[inline] #[inline]
fn add_assign(&mut self, rhs: OldDuration) { fn add_assign(&mut self, rhs: TimeDelta) {
self.date = self.date.checked_add_signed(rhs).expect("`Date + Duration` overflowed"); self.date = self.date.checked_add_signed(rhs).expect("`Date + TimeDelta` overflowed");
} }
} }
impl<Tz: TimeZone> Sub<OldDuration> for Date<Tz> { impl<Tz: TimeZone> Sub<TimeDelta> for Date<Tz> {
type Output = Date<Tz>; type Output = Date<Tz>;
#[inline] #[inline]
fn sub(self, rhs: OldDuration) -> Date<Tz> { fn sub(self, rhs: TimeDelta) -> Date<Tz> {
self.checked_sub_signed(rhs).expect("`Date - Duration` overflowed") self.checked_sub_signed(rhs).expect("`Date - TimeDelta` overflowed")
} }
} }
impl<Tz: TimeZone> SubAssign<OldDuration> for Date<Tz> { impl<Tz: TimeZone> SubAssign<TimeDelta> for Date<Tz> {
#[inline] #[inline]
fn sub_assign(&mut self, rhs: OldDuration) { fn sub_assign(&mut self, rhs: TimeDelta) {
self.date = self.date.checked_sub_signed(rhs).expect("`Date - Duration` overflowed"); self.date = self.date.checked_sub_signed(rhs).expect("`Date - TimeDelta` overflowed");
} }
} }
impl<Tz: TimeZone> Sub<Date<Tz>> for Date<Tz> { impl<Tz: TimeZone> Sub<Date<Tz>> for Date<Tz> {
type Output = OldDuration; type Output = TimeDelta;
#[inline] #[inline]
fn sub(self, rhs: Date<Tz>) -> OldDuration { fn sub(self, rhs: Date<Tz>) -> TimeDelta {
self.signed_duration_since(rhs) self.signed_duration_since(rhs)
} }
} }
@ -572,8 +570,7 @@ where
mod tests { mod tests {
use super::Date; use super::Date;
use crate::duration::Duration; use crate::{FixedOffset, NaiveDate, TimeDelta, Utc};
use crate::{FixedOffset, NaiveDate, Utc};
#[cfg(feature = "clock")] #[cfg(feature = "clock")]
use crate::offset::{Local, TimeZone}; use crate::offset::{Local, TimeZone};
@ -584,15 +581,15 @@ mod tests {
const WEEKS_PER_YEAR: f32 = 52.1775; const WEEKS_PER_YEAR: f32 = 52.1775;
// This is always at least one year because 1 year = 52.1775 weeks. // 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. // 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(one_year_ago), Some(1));
assert_eq!(Utc::today().years_since(two_year_ago), Some(2)); 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. // 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); assert_eq!(Utc::today().years_since(future), None);
} }
@ -602,20 +599,20 @@ mod tests {
let date = Date::<Utc>::from_utc(naivedate, Utc); let date = Date::<Utc>::from_utc(naivedate, Utc);
let mut date_add = date; let mut date_add = date;
date_add += Duration::days(5); date_add += TimeDelta::days(5);
assert_eq!(date_add, date + Duration::days(5)); assert_eq!(date_add, date + TimeDelta::days(5));
let timezone = FixedOffset::east_opt(60 * 60).unwrap(); let timezone = FixedOffset::east_opt(60 * 60).unwrap();
let date = date.with_timezone(&timezone); let date = date.with_timezone(&timezone);
let date_add = date_add.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 timezone = FixedOffset::west_opt(2 * 60 * 60).unwrap();
let date = date.with_timezone(&timezone); let date = date.with_timezone(&timezone);
let date_add = date_add.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] #[test]
@ -626,8 +623,8 @@ mod tests {
let date = Local.from_utc_date(&naivedate); let date = Local.from_utc_date(&naivedate);
let mut date_add = date; let mut date_add = date;
date_add += Duration::days(5); date_add += TimeDelta::days(5);
assert_eq!(date_add, date + Duration::days(5)); assert_eq!(date_add, date + TimeDelta::days(5));
} }
#[test] #[test]
@ -636,20 +633,20 @@ mod tests {
let date = Date::<Utc>::from_utc(naivedate, Utc); let date = Date::<Utc>::from_utc(naivedate, Utc);
let mut date_sub = date; let mut date_sub = date;
date_sub -= Duration::days(5); date_sub -= TimeDelta::days(5);
assert_eq!(date_sub, date - Duration::days(5)); assert_eq!(date_sub, date - TimeDelta::days(5));
let timezone = FixedOffset::east_opt(60 * 60).unwrap(); let timezone = FixedOffset::east_opt(60 * 60).unwrap();
let date = date.with_timezone(&timezone); let date = date.with_timezone(&timezone);
let date_sub = date_sub.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 timezone = FixedOffset::west_opt(2 * 60 * 60).unwrap();
let date = date.with_timezone(&timezone); let date = date.with_timezone(&timezone);
let date_sub = date_sub.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] #[test]
@ -660,7 +657,7 @@ mod tests {
let date = Local.from_utc_date(&naivedate); let date = Local.from_utc_date(&naivedate);
let mut date_sub = date; let mut date_sub = date;
date_sub -= Duration::days(5); date_sub -= TimeDelta::days(5);
assert_eq!(date_sub, date - Duration::days(5)); assert_eq!(date_sub, date - TimeDelta::days(5));
} }
} }