Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Months::num_months() and num_years() #1373

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 14 additions & 1 deletion src/month.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,12 @@ 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 as_u32(&self) -> u32 {
self.0
}
}

/// An error resulting from reading `<Month>` value with `FromStr`.
Expand Down Expand Up @@ -298,7 +304,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() {
Expand Down Expand Up @@ -353,6 +359,13 @@ mod tests {
assert!(Month::September > Month::March);
}

#[test]
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]
#[cfg(feature = "serde")]
fn test_serde_serialize() {
Expand Down