From 56f1db6dfa30ede8462ba61edb1ded2590d069d4 Mon Sep 17 00:00:00 2001 From: Jacob Pratt Date: Fri, 25 Oct 2024 06:17:44 -0400 Subject: [PATCH] Add `Month::length`, `days_in_month` --- time/src/date.rs | 10 +++++----- time/src/month.rs | 17 ++++++++++++++++- time/src/offset_date_time.rs | 2 +- time/src/util.rs | 22 +++++++++++++++------- 4 files changed, 37 insertions(+), 14 deletions(-) diff --git a/time/src/date.rs b/time/src/date.rs index 92c28114b..56253d9b6 100644 --- a/time/src/date.rs +++ b/time/src/date.rs @@ -24,7 +24,7 @@ use crate::internal_macros::{ }; #[cfg(feature = "parsing")] use crate::parsing::Parsable; -use crate::util::{days_in_year, days_in_year_month, is_leap_year, weeks_in_year}; +use crate::util::{days_in_year, is_leap_year, weeks_in_year}; use crate::{error, Duration, Month, PrimitiveDateTime, Time, Weekday}; type Year = RangedI32; @@ -120,12 +120,12 @@ impl Date { ensure_ranged!(Year: year); match day { 1..=28 => {} - 29..=31 if day <= days_in_year_month(year, month) => {} + 29..=31 if day <= month.length(year) => {} _ => { return Err(error::ComponentRange { name: "day", minimum: 1, - maximum: days_in_year_month(year, month) as _, + maximum: month.length(year) as _, value: day as _, conditional_range: true, }); @@ -1103,12 +1103,12 @@ impl Date { pub const fn replace_day(self, day: u8) -> Result { match day { 1..=28 => {} - 29..=31 if day <= days_in_year_month(self.year(), self.month()) => {} + 29..=31 if day <= self.month().length(self.year()) => {} _ => { return Err(error::ComponentRange { name: "day", minimum: 1, - maximum: days_in_year_month(self.year(), self.month()) as _, + maximum: self.month().length(self.year()) as _, value: day as _, conditional_range: true, }); diff --git a/time/src/month.rs b/time/src/month.rs index 55e53b4b8..6a3d2fa14 100644 --- a/time/src/month.rs +++ b/time/src/month.rs @@ -7,7 +7,7 @@ use core::str::FromStr; use powerfmt::smart_display::{FormatterOptions, Metadata, SmartDisplay}; use self::Month::*; -use crate::error; +use crate::{error, util}; /// Months of the year. #[repr(u8)] @@ -65,6 +65,21 @@ impl Month { } } + /// Get the number of days in the month of a given year. + /// + /// ```rust + /// # use time::Month; + /// assert_eq!(Month::February.length(2020), 29); + /// ``` + pub const fn length(self, year: i32) -> u8 { + match self { + January | March | May | July | August | October | December => 31, + April | June | September | November => 30, + February if util::is_leap_year(year) => 29, + February => 28, + } + } + /// Get the previous month. /// /// ```rust diff --git a/time/src/offset_date_time.rs b/time/src/offset_date_time.rs index 9b478a79c..3ae82cea7 100644 --- a/time/src/offset_date_time.rs +++ b/time/src/offset_date_time.rs @@ -1317,7 +1317,7 @@ impl OffsetDateTime { time.hour() == 23 && time.minute() == 59 && time.second() == 59 - && date.day() == util::days_in_year_month(year, date.month()) + && date.day() == date.month().length(year) } } diff --git a/time/src/util.rs b/time/src/util.rs index 622a23aac..6a0e9e430 100644 --- a/time/src/util.rs +++ b/time/src/util.rs @@ -14,20 +14,28 @@ pub(crate) enum DateAdjustment { None, } +/// Get the number of days in the month of a given year. +/// +/// ```rust +/// # use time::{Month, util}; +/// assert_eq!(util::days_in_month(Month::February, 2020), 29); +/// ``` +pub const fn days_in_month(month: Month, year: i32) -> u8 { + month.length(year) +} + /// Get the number of days in the month of a given year. /// /// ```rust /// # use time::{Month, util}; /// assert_eq!(util::days_in_year_month(2020, Month::February), 29); /// ``` +#[deprecated( + since = "0.3.37", + note = "use `days_in_month` or `Month::length` instead" +)] pub const fn days_in_year_month(year: i32, month: Month) -> u8 { - use Month::*; - match month { - January | March | May | July | August | October | December => 31, - April | June | September | November => 30, - February if is_leap_year(year) => 29, - February => 28, - } + month.length(year) } /// Update time zone information from the system.