Skip to content

Commit

Permalink
Add Month::length, days_in_month
Browse files Browse the repository at this point in the history
  • Loading branch information
jhpratt committed Dec 3, 2024
1 parent 03bcfe9 commit 56f1db6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 14 deletions.
10 changes: 5 additions & 5 deletions time/src/date.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<MIN_YEAR, MAX_YEAR>;
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -1103,12 +1103,12 @@ impl Date {
pub const fn replace_day(self, day: u8) -> Result<Self, error::ComponentRange> {
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,
});
Expand Down
17 changes: 16 additions & 1 deletion time/src/month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion time/src/offset_date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
22 changes: 15 additions & 7 deletions time/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 56f1db6

Please sign in to comment.