From 03165c8658528a0ec267bf724d092e677d3c6e34 Mon Sep 17 00:00:00 2001 From: Dirkjan Ochtman Date: Thu, 20 Oct 2022 12:29:22 +0200 Subject: [PATCH] Move Date::years_since() implementation into NaiveDate --- src/date.rs | 10 +--------- src/naive/date.rs | 13 +++++++++++++ 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/date.rs b/src/date.rs index 1e0bf0e7..697ac906 100644 --- a/src/date.rs +++ b/src/date.rs @@ -288,15 +288,7 @@ impl Date { /// Returns the number of whole years from the given `base` until `self`. pub fn years_since(&self, base: Self) -> Option { - 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`. diff --git a/src/naive/date.rs b/src/naive/date.rs index e362cd96..9db2ccf9 100644 --- a/src/naive/date.rs +++ b/src/naive/date.rs @@ -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 { + 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. ///