@@ -227,6 +227,18 @@ impl Months {
227227 pub const fn new ( num : u32 ) -> Self {
228228 Self ( num)
229229 }
230+
231+ /// Returns the total number of months in the `Months` instance.
232+ #[ inline]
233+ pub const fn num_months ( & self ) -> u32 {
234+ self . 0
235+ }
236+
237+ /// Returns the total number of whole years in the `Months` instance.
238+ #[ inline]
239+ pub const fn num_years ( & self ) -> u32 {
240+ self . 0 / 12
241+ }
230242}
231243
232244/// An error resulting from reading `<Month>` value with `FromStr`.
@@ -298,7 +310,7 @@ mod month_serde {
298310#[ cfg( test) ]
299311mod tests {
300312 use super :: Month ;
301- use crate :: { Datelike , OutOfRange , TimeZone , Utc } ;
313+ use crate :: { Datelike , Months , OutOfRange , TimeZone , Utc } ;
302314
303315 #[ test]
304316 fn test_month_enum_try_from ( ) {
@@ -352,6 +364,24 @@ mod tests {
352364 assert ! ( Month :: July >= Month :: May ) ;
353365 assert ! ( Month :: September > Month :: March ) ;
354366 }
367+
368+ #[ test]
369+ fn test_months_num_months ( ) {
370+ assert_eq ! ( Months :: new( 0 ) . num_months( ) , 0 ) ;
371+ assert_eq ! ( Months :: new( 1 ) . num_months( ) , 1 ) ;
372+ assert_eq ! ( Months :: new( u32 :: MAX ) . num_months( ) , u32 :: MAX ) ;
373+ }
374+
375+ #[ test]
376+ fn test_months_num_years ( ) {
377+ assert_eq ! ( Months :: new( 0 ) . num_years( ) , 0 ) ;
378+ assert_eq ! ( Months :: new( 1 ) . num_years( ) , 0 ) ;
379+ assert_eq ! ( Months :: new( 11 ) . num_years( ) , 0 ) ;
380+ assert_eq ! ( Months :: new( 12 ) . num_years( ) , 1 ) ;
381+ assert_eq ! ( Months :: new( 23 ) . num_years( ) , 1 ) ;
382+ assert_eq ! ( Months :: new( 24 ) . num_years( ) , 2 ) ;
383+ assert_eq ! ( Months :: new( u32 :: MAX ) . num_years( ) , u32 :: MAX / 12 ) ;
384+ }
355385
356386 #[ test]
357387 #[ cfg( feature = "serde" ) ]
0 commit comments