Skip to content

Commit

Permalink
Rename local Duration type to TimeDelta
Browse files Browse the repository at this point in the history
Co-authored-by: Dirkjan Ochtman <[email protected]>
  • Loading branch information
esheppa and djc committed Aug 28, 2022
1 parent 549c645 commit 3aa69af
Show file tree
Hide file tree
Showing 16 changed files with 771 additions and 784 deletions.
60 changes: 30 additions & 30 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use crate::format::Locale;
use crate::format::{DelayedFormat, Item, StrftimeItems};
use crate::naive::{IsoWeek, NaiveDate, NaiveTime};
use crate::offset::{TimeZone, Utc};
use crate::oldtime::Duration as OldDuration;
use crate::time_delta::TimeDelta;
use crate::DateTime;
use crate::{Datelike, Weekday};

Expand Down Expand Up @@ -240,7 +240,7 @@ impl<Tz: TimeZone> Date<Tz> {
///
/// Returns `None` when it will result in overflow.
#[inline]
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)?;
Some(Date { date, offset: self.offset })
}
Expand All @@ -249,7 +249,7 @@ impl<Tz: TimeZone> Date<Tz> {
///
/// Returns `None` when it will result in overflow.
#[inline]
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)?;
Some(Date { date, offset: self.offset })
}
Expand All @@ -260,7 +260,7 @@ impl<Tz: TimeZone> Date<Tz> {
/// This does not overflow or underflow at all,
/// as all possible output fits in the range of `Duration`.
#[inline]
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)
}

Expand Down Expand Up @@ -483,43 +483,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>;

#[inline]
fn add(self, rhs: OldDuration) -> Date<Tz> {
fn add(self, rhs: TimeDelta) -> Date<Tz> {
self.checked_add_signed(rhs).expect("`Date + Duration` overflowed")
}
}

impl<Tz: TimeZone> AddAssign<OldDuration> for Date<Tz> {
impl<Tz: TimeZone> AddAssign<TimeDelta> for Date<Tz> {
#[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");
}
}

impl<Tz: TimeZone> Sub<OldDuration> for Date<Tz> {
impl<Tz: TimeZone> Sub<TimeDelta> for Date<Tz> {
type Output = Date<Tz>;

#[inline]
fn sub(self, rhs: OldDuration) -> Date<Tz> {
fn sub(self, rhs: TimeDelta) -> Date<Tz> {
self.checked_sub_signed(rhs).expect("`Date - Duration` overflowed")
}
}

impl<Tz: TimeZone> SubAssign<OldDuration> for Date<Tz> {
impl<Tz: TimeZone> SubAssign<TimeDelta> for Date<Tz> {
#[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");
}
}

impl<Tz: TimeZone> Sub<Date<Tz>> for Date<Tz> {
type Output = OldDuration;
type Output = TimeDelta;

#[inline]
fn sub(self, rhs: Date<Tz>) -> OldDuration {
fn sub(self, rhs: Date<Tz>) -> TimeDelta {
self.signed_duration_since(rhs)
}
}
Expand All @@ -543,7 +543,7 @@ where
mod tests {
use super::Date;

use crate::oldtime::Duration;
use crate::time_delta::TimeDelta;
use crate::{FixedOffset, NaiveDate, Utc};

#[cfg(feature = "clock")]
Expand All @@ -555,15 +555,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);
}

Expand All @@ -573,20 +573,20 @@ mod tests {
let date = Date::<Utc>::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(60 * 60);
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(2 * 60 * 60);
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]
Expand All @@ -597,8 +597,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]
Expand All @@ -607,20 +607,20 @@ mod tests {
let date = Date::<Utc>::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(60 * 60);
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(2 * 60 * 60);
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]
Expand All @@ -631,7 +631,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));
}
}
38 changes: 17 additions & 21 deletions src/datetime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ use std::string::ToString;
#[cfg(any(feature = "std", test))]
use std::time::{SystemTime, UNIX_EPOCH};

#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};

#[cfg(any(feature = "alloc", feature = "std", test))]
use crate::format::DelayedFormat;
#[cfg(feature = "unstable-locales")]
Expand All @@ -28,14 +31,7 @@ use crate::naive::{IsoWeek, NaiveDate, NaiveDateTime, NaiveTime};
#[cfg(feature = "clock")]
use crate::offset::Local;
use crate::offset::{FixedOffset, Offset, TimeZone, Utc};
use crate::oldtime::Duration as OldDuration;
use crate::Date;
use crate::Months;
use crate::{Datelike, Timelike, Weekday};

#[cfg(feature = "rkyv")]
use rkyv::{Archive, Deserialize, Serialize};

use crate::{Date, Datelike, Months, TimeDelta, Timelike, Weekday};

/// documented at re-export site
#[cfg(feature = "serde")]
Expand Down Expand Up @@ -322,7 +318,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
///
/// Returns `None` when it will result in overflow.
#[inline]
pub fn checked_add_signed(self, rhs: OldDuration) -> Option<DateTime<Tz>> {
pub fn checked_add_signed(self, rhs: TimeDelta) -> Option<DateTime<Tz>> {
let datetime = self.datetime.checked_add_signed(rhs)?;
let tz = self.timezone();
Some(tz.from_utc_datetime(&datetime))
Expand All @@ -345,7 +341,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
///
/// Returns `None` when it will result in overflow.
#[inline]
pub fn checked_sub_signed(self, rhs: OldDuration) -> Option<DateTime<Tz>> {
pub fn checked_sub_signed(self, rhs: TimeDelta) -> Option<DateTime<Tz>> {
let datetime = self.datetime.checked_sub_signed(rhs)?;
let tz = self.timezone();
Some(tz.from_utc_datetime(&datetime))
Expand All @@ -367,7 +363,7 @@ impl<Tz: TimeZone> DateTime<Tz> {
/// Subtracts another `DateTime` from the current date and time.
/// This does not overflow or underflow at all.
#[inline]
pub fn signed_duration_since<Tz2: TimeZone>(self, rhs: DateTime<Tz2>) -> OldDuration {
pub fn signed_duration_since<Tz2: TimeZone>(self, rhs: DateTime<Tz2>) -> TimeDelta {
self.datetime.signed_duration_since(rhs.datetime)
}

Expand Down Expand Up @@ -887,18 +883,18 @@ impl<Tz: TimeZone> hash::Hash for DateTime<Tz> {
}
}

impl<Tz: TimeZone> Add<OldDuration> for DateTime<Tz> {
impl<Tz: TimeZone> Add<TimeDelta> for DateTime<Tz> {
type Output = DateTime<Tz>;

#[inline]
fn add(self, rhs: OldDuration) -> DateTime<Tz> {
fn add(self, rhs: TimeDelta) -> DateTime<Tz> {
self.checked_add_signed(rhs).expect("`DateTime + Duration` overflowed")
}
}

impl<Tz: TimeZone> AddAssign<OldDuration> for DateTime<Tz> {
impl<Tz: TimeZone> AddAssign<TimeDelta> for DateTime<Tz> {
#[inline]
fn add_assign(&mut self, rhs: OldDuration) {
fn add_assign(&mut self, rhs: TimeDelta) {
let datetime =
self.datetime.checked_add_signed(rhs).expect("`DateTime + Duration` overflowed");
let tz = self.timezone();
Expand All @@ -914,18 +910,18 @@ impl<Tz: TimeZone> Add<Months> for DateTime<Tz> {
}
}

impl<Tz: TimeZone> Sub<OldDuration> for DateTime<Tz> {
impl<Tz: TimeZone> Sub<TimeDelta> for DateTime<Tz> {
type Output = DateTime<Tz>;

#[inline]
fn sub(self, rhs: OldDuration) -> DateTime<Tz> {
fn sub(self, rhs: TimeDelta) -> DateTime<Tz> {
self.checked_sub_signed(rhs).expect("`DateTime - Duration` overflowed")
}
}

impl<Tz: TimeZone> SubAssign<OldDuration> for DateTime<Tz> {
impl<Tz: TimeZone> SubAssign<TimeDelta> for DateTime<Tz> {
#[inline]
fn sub_assign(&mut self, rhs: OldDuration) {
fn sub_assign(&mut self, rhs: TimeDelta) {
let datetime =
self.datetime.checked_sub_signed(rhs).expect("`DateTime - Duration` overflowed");
let tz = self.timezone();
Expand All @@ -942,10 +938,10 @@ impl<Tz: TimeZone> Sub<Months> for DateTime<Tz> {
}

impl<Tz: TimeZone> Sub<DateTime<Tz>> for DateTime<Tz> {
type Output = OldDuration;
type Output = TimeDelta;

#[inline]
fn sub(self, rhs: DateTime<Tz>) -> OldDuration {
fn sub(self, rhs: DateTime<Tz>) -> TimeDelta {
self.signed_duration_since(rhs)
}
}
Expand Down
Loading

0 comments on commit 3aa69af

Please sign in to comment.