Skip to content

Commit 334565c

Browse files
Kinranydjc
authored andcommitted
replace Datelike::days_between with NaiveDate::abs_diff
1 parent c34e4c5 commit 334565c

File tree

2 files changed

+20
-8
lines changed

2 files changed

+20
-8
lines changed

src/naive/date/mod.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1148,7 +1148,7 @@ impl NaiveDate {
11481148
/// );
11491149
/// ```
11501150
#[must_use]
1151-
pub const fn signed_duration_since(self, rhs: NaiveDate) -> TimeDelta {
1151+
pub const fn signed_duration_since(self, rhs: Self) -> TimeDelta {
11521152
let year1 = self.year();
11531153
let year2 = rhs.year();
11541154
let (year1_div_400, year1_mod_400) = div_mod_floor(year1, 400);
@@ -1161,6 +1161,24 @@ impl NaiveDate {
11611161
expect(TimeDelta::try_days(days), "always in range")
11621162
}
11631163

1164+
/// Returns the absolute difference between two `NaiveDate`s measured as the number of days.
1165+
///
1166+
/// This is always an integer, non-negative number, similar to `abs_diff` in `std`.
1167+
///
1168+
/// # Example
1169+
///
1170+
/// ```
1171+
/// # use chrono::{Days, NaiveDate};
1172+
/// #
1173+
/// let date1: NaiveDate = "2020-01-01".parse().unwrap();
1174+
/// let date2: NaiveDate = "2020-01-31".parse().unwrap();
1175+
/// assert_eq!(date2.abs_diff(date1), Days::new(30));
1176+
/// assert_eq!(date1.abs_diff(date2), Days::new(30));
1177+
/// ```
1178+
pub const fn abs_diff(self, rhs: Self) -> Days {
1179+
Days::new(i32::abs_diff(self.num_days_from_ce(), rhs.num_days_from_ce()) as u64)
1180+
}
1181+
11641182
/// Returns the number of whole years from the given `base` until `self`.
11651183
///
11661184
/// # Errors

src/traits.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::{Days, IsoWeek, Month, Weekday};
1+
use crate::{IsoWeek, Month, Weekday};
22

33
/// The common set of methods for date component.
44
///
@@ -277,12 +277,6 @@ pub trait Datelike: Sized {
277277
// know it is in range, and the result will never be `None`.
278278
month.num_days(self.year()).unwrap()
279279
}
280-
281-
/// Get the difference in days between the two days,
282-
/// ignoring the time component
283-
fn days_between(&self, other: impl Datelike) -> Days {
284-
Days::new(i32::abs_diff(self.num_days_from_ce(), other.num_days_from_ce()).into())
285-
}
286280
}
287281

288282
/// The common set of methods for time component.

0 commit comments

Comments
 (0)