Move Date::years_since() implementation into NaiveDate

This commit is contained in:
Dirkjan Ochtman 2022-10-20 12:29:22 +02:00
parent 645fca0c55
commit 03165c8658
2 changed files with 14 additions and 9 deletions

View File

@ -288,15 +288,7 @@ impl<Tz: TimeZone> Date<Tz> {
/// Returns the number of whole years from the given `base` until `self`.
pub fn years_since(&self, base: Self) -> Option<u32> {
let mut years = self.year() - base.year();
if (self.month(), self.day()) < (base.month(), base.day()) {
years -= 1;
}
match years >= 0 {
true => Some(years as u32),
false => None,
}
self.date.years_since(base.date)
}
/// The minimum possible `Date`.

View File

@ -1054,6 +1054,19 @@ impl NaiveDate {
)
}
/// Returns the number of whole years from the given `base` until `self`.
pub fn years_since(&self, base: Self) -> Option<u32> {
let mut years = self.year() - base.year();
if (self.month(), self.day()) < (base.month(), base.day()) {
years -= 1;
}
match years >= 0 {
true => Some(years as u32),
false => None,
}
}
/// Formats the date with the specified formatting items.
/// Otherwise it is the same as the ordinary `format` method.
///