Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bench/benches/chrono.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use chrono::format::StrftimeItems;
use chrono::prelude::*;
#[cfg(feature = "unstable-locales")]
use chrono::Locale;
use chrono::{DateTime, Duration, FixedOffset, Local, Utc, __BenchYearFlags};
use chrono::{DateTime, FixedOffset, Local, TimeDelta, Utc, __BenchYearFlags};

fn bench_datetime_parse_from_rfc2822(c: &mut Criterion) {
c.bench_function("bench_datetime_parse_from_rfc2822", |b| {
Expand Down Expand Up @@ -198,7 +198,7 @@ fn bench_format_manual(c: &mut Criterion) {

fn bench_naivedate_add_signed(c: &mut Criterion) {
let date = NaiveDate::from_ymd_opt(2023, 7, 29).unwrap();
let extra = Duration::days(25);
let extra = TimeDelta::days(25);
c.bench_function("bench_naivedate_add_signed", |b| {
b.iter(|| black_box(date).checked_add_signed(extra).unwrap())
});
Expand Down
81 changes: 39 additions & 42 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down Expand Up @@ -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<Tz>` instead")]
#[derive(Clone)]
#[cfg_attr(feature = "rkyv", derive(Archive, Deserialize, Serialize))]
Expand Down Expand Up @@ -259,34 +257,34 @@ impl<Tz: TimeZone> Date<Tz> {
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<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 })
}

/// 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<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 })
}

/// 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<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 @@ -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>;

#[inline]
fn add(self, rhs: OldDuration) -> Date<Tz> {
self.checked_add_signed(rhs).expect("`Date + Duration` overflowed")
fn add(self, rhs: TimeDelta) -> Date<Tz> {
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]
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<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> {
self.checked_sub_signed(rhs).expect("`Date - Duration` overflowed")
fn sub(self, rhs: TimeDelta) -> Date<Tz> {
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]
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<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 Down Expand Up @@ -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};
Expand All @@ -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);
}

Expand All @@ -602,20 +599,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_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]
Expand All @@ -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]
Expand All @@ -636,20 +633,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_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]
Expand All @@ -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));
}
}
Loading