Skip to content

Commit

Permalink
Move Date::years_since() implementation into NaiveDate
Browse files Browse the repository at this point in the history
  • Loading branch information
djc committed Nov 9, 2022
1 parent 645fca0 commit 03165c8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
10 changes: 1 addition & 9 deletions src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Expand Down
13 changes: 13 additions & 0 deletions src/naive/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
///
Expand Down

0 comments on commit 03165c8

Please sign in to comment.