From 9d446064859d9e8edcdf9b3af78c8f5bfae0d029 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Mon, 8 Jan 2024 07:09:37 +0000 Subject: [PATCH 1/2] Add Months::num_months() and num_years() --- src/month.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/month.rs b/src/month.rs index 146a98ebe5..c2acc53835 100644 --- a/src/month.rs +++ b/src/month.rs @@ -227,6 +227,18 @@ impl Months { pub const fn new(num: u32) -> Self { Self(num) } + + /// Returns the total number of months in the `Months` instance. + #[inline] + pub const fn num_months(&self) -> u32 { + self.0 + } + + /// Returns the total number of whole years in the `Months` instance. + #[inline] + pub const fn num_years(&self) -> u32 { + self.0 / 12 + } } /// An error resulting from reading `` value with `FromStr`. @@ -298,7 +310,7 @@ mod month_serde { #[cfg(test)] mod tests { use super::Month; - use crate::{Datelike, OutOfRange, TimeZone, Utc}; + use crate::{Datelike, Months, OutOfRange, TimeZone, Utc}; #[test] fn test_month_enum_try_from() { @@ -353,6 +365,24 @@ mod tests { assert!(Month::September > Month::March); } + #[test] + fn test_months_num_months() { + assert_eq!(Months::new(0).num_months(), 0); + assert_eq!(Months::new(1).num_months(), 1); + assert_eq!(Months::new(u32::MAX).num_months(), u32::MAX); + } + + #[test] + fn test_months_num_years() { + assert_eq!(Months::new(0).num_years(), 0); + assert_eq!(Months::new(1).num_years(), 0); + assert_eq!(Months::new(11).num_years(), 0); + assert_eq!(Months::new(12).num_years(), 1); + assert_eq!(Months::new(23).num_years(), 1); + assert_eq!(Months::new(24).num_years(), 2); + assert_eq!(Months::new(u32::MAX).num_years(), u32::MAX / 12); + } + #[test] #[cfg(feature = "serde")] fn test_serde_serialize() { From b56e3a4de7eb982f1ec99fe83765bee26ca409c1 Mon Sep 17 00:00:00 2001 From: Dan Williams Date: Tue, 9 Jan 2024 11:15:52 +0000 Subject: [PATCH 2/2] Renamed Months::num_months() to as_u32(), and removed num_years() --- src/month.rs | 27 +++++---------------------- 1 file changed, 5 insertions(+), 22 deletions(-) diff --git a/src/month.rs b/src/month.rs index c2acc53835..9e14dad6d8 100644 --- a/src/month.rs +++ b/src/month.rs @@ -230,15 +230,9 @@ impl Months { /// Returns the total number of months in the `Months` instance. #[inline] - pub const fn num_months(&self) -> u32 { + pub const fn as_u32(&self) -> u32 { self.0 } - - /// Returns the total number of whole years in the `Months` instance. - #[inline] - pub const fn num_years(&self) -> u32 { - self.0 / 12 - } } /// An error resulting from reading `` value with `FromStr`. @@ -366,21 +360,10 @@ mod tests { } #[test] - fn test_months_num_months() { - assert_eq!(Months::new(0).num_months(), 0); - assert_eq!(Months::new(1).num_months(), 1); - assert_eq!(Months::new(u32::MAX).num_months(), u32::MAX); - } - - #[test] - fn test_months_num_years() { - assert_eq!(Months::new(0).num_years(), 0); - assert_eq!(Months::new(1).num_years(), 0); - assert_eq!(Months::new(11).num_years(), 0); - assert_eq!(Months::new(12).num_years(), 1); - assert_eq!(Months::new(23).num_years(), 1); - assert_eq!(Months::new(24).num_years(), 2); - assert_eq!(Months::new(u32::MAX).num_years(), u32::MAX / 12); + fn test_months_as_u32() { + assert_eq!(Months::new(0).as_u32(), 0); + assert_eq!(Months::new(1).as_u32(), 1); + assert_eq!(Months::new(u32::MAX).as_u32(), u32::MAX); } #[test]