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 27, 2022
1 parent 77f5eed commit 785fbb4
Show file tree
Hide file tree
Showing 17 changed files with 780 additions and 805 deletions.
30 changes: 8 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,30 +76,16 @@ See the [cargo docs][] for examples of specifying features.

### Duration

Chrono currently uses its own [`Duration`] type to represent the magnitude
of a time span. Since this has the same name as the newer, standard type for
duration, the reference will refer this type as `OldDuration`.

Note that this is an "accurate" duration represented as seconds and
Chrono currently uses its own [`TimeDelta`] type to represent the magnitude
of a time span. Note that this is an "accurate" duration represented as seconds and
nanoseconds and does not represent "nominal" components such as days or
months.

When the `oldtime` feature is enabled, [`Duration`] is an alias for the
[`time::Duration`](https://docs.rs/time/0.1.40/time/struct.Duration.html)
type from v0.1 of the time crate. time v0.1 is deprecated, so new code
should disable the `oldtime` feature and use the `chrono::Duration` type
instead. The `oldtime` feature is enabled by default for backwards
compatibility, but future versions of Chrono are likely to remove the
feature entirely.

Chrono does not yet natively support
the standard [`Duration`](https://doc.rust-lang.org/std/time/struct.Duration.html) type,
but it will be supported in the future.
Meanwhile you can convert between two types with
[`Duration::from_std`](https://docs.rs/time/0.1.40/time/struct.Duration.html#method.from_std)
and
[`Duration::to_std`](https://docs.rs/time/0.1.40/time/struct.Duration.html#method.to_std)
methods.
[`TimeDelta::from_std`] and [`TimeDelta::to_std`] methods.

### Date and Time

Expand Down Expand Up @@ -184,7 +170,7 @@ The following illustrates most supported operations to the date and time:

```rust
use chrono::prelude::*;
use chrono::Duration;
use chrono::TimeDelta;

// assume this returned `2014-11-28T21:45:59.324310806+09:00`:
let dt = FixedOffset::east(9*3600).ymd(2014, 11, 28).and_hms_nano(21, 45, 59, 324310806);
Expand All @@ -211,11 +197,11 @@ assert_eq!(dt.with_year(-300).unwrap().num_days_from_ce(), -109606); // November
// arithmetic operations
let dt1 = Utc.ymd(2014, 11, 14).and_hms(8, 9, 10);
let dt2 = Utc.ymd(2014, 11, 14).and_hms(10, 9, 8);
assert_eq!(dt1.signed_duration_since(dt2), Duration::seconds(-2 * 3600 + 2));
assert_eq!(dt2.signed_duration_since(dt1), Duration::seconds(2 * 3600 - 2));
assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0) + Duration::seconds(1_000_000_000),
assert_eq!(dt1.signed_duration_since(dt2), TimeDelta::seconds(-2 * 3600 + 2));
assert_eq!(dt2.signed_duration_since(dt1), TimeDelta::seconds(2 * 3600 - 2));
assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0) + TimeDelta::seconds(1_000_000_000),
Utc.ymd(2001, 9, 9).and_hms(1, 46, 40));
assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0) - Duration::seconds(1_000_000_000),
assert_eq!(Utc.ymd(1970, 1, 1).and_hms(0, 0, 0) - TimeDelta::seconds(1_000_000_000),
Utc.ymd(1938, 4, 24).and_hms(22, 13, 20));
```

Expand Down
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 785fbb4

Please sign in to comment.