Skip to content

Commit

Permalink
Adds a public Month enum.
Browse files Browse the repository at this point in the history
Implementation for a Month enum, allowing -ser|de-rialization.
The implementation mostly mirrors the Weekday enum.
It only adds the enum with minimal integration so as to
not cause any backwards compatibility issue.
A deeper integration should be up to discussion.
Suggested in Issue #202
  • Loading branch information
hhamana authored and botahamec committed May 26, 2022
1 parent 7656101 commit 7b1e9f6
Show file tree
Hide file tree
Showing 3 changed files with 393 additions and 28 deletions.
4 changes: 2 additions & 2 deletions CHANGELOG.md
Expand Up @@ -12,13 +12,13 @@ Versions with only mechanical changes will be omitted from the following list.

### Features

* Added day and week iterators for `NaiveDate` (@gnzlbg & @robyoung)
* Add day and week iterators for `NaiveDate` (@gnzlbg & @robyoung)
* Add a `Month` enum (@hhamana)

### Improvements

* Added MIN and MAX values for `NaiveTime`, `NaiveDateTime` and `DateTime<Utc>`.


## 0.4.13

### Features
Expand Down
56 changes: 53 additions & 3 deletions src/format/mod.rs
Expand Up @@ -28,15 +28,14 @@ use core::str::FromStr;
#[cfg(any(feature = "std", test))]
use std::error::Error;

#[cfg(any(feature = "alloc", feature = "std", test))]
use div::{div_floor, mod_floor};
#[cfg(any(feature = "alloc", feature = "std", test))]
use naive::{NaiveDate, NaiveTime};
#[cfg(any(feature = "alloc", feature = "std", test))]
use offset::{FixedOffset, Offset};
#[cfg(any(feature = "alloc", feature = "std", test))]
use {Datelike, Timelike};
use {ParseWeekdayError, Weekday};
use {Datelike, Month, Timelike, Weekday};
use {ParseMonthError, ParseWeekdayError};

pub use self::parse::parse;
pub use self::parsed::Parsed;
Expand Down Expand Up @@ -759,3 +758,54 @@ impl FromStr for Weekday {
}
}
}

/// Parsing a `str` into a `Month` uses the format [`%W`](./format/strftime/index.html).
///
/// # Example
///
/// ~~~~
/// use chrono::Month;
///
/// assert_eq!("January".parse::<Month>(), Ok(Month::January));
/// assert!("any day".parse::<Month>().is_err());
/// ~~~~
///
/// The parsing is case-insensitive.
///
/// ~~~~
/// # use chrono::Month;
/// assert_eq!("fEbruARy".parse::<Month>(), Ok(Month::February));
/// ~~~~
///
/// Only the shortest form (e.g. `jan`) and the longest form (e.g. `january`) is accepted.
///
/// ~~~~
/// # use chrono::Month;
/// assert!("septem".parse::<Month>().is_err());
/// assert!("Augustin".parse::<Month>().is_err());
/// ~~~~
impl FromStr for Month {
type Err = ParseMonthError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if let Ok(("", w)) = scan::short_or_long_month0(s) {
match w {
0 => Ok(Month::January),
1 => Ok(Month::February),
2 => Ok(Month::March),
3 => Ok(Month::April),
4 => Ok(Month::May),
5 => Ok(Month::June),
6 => Ok(Month::July),
7 => Ok(Month::August),
8 => Ok(Month::September),
9 => Ok(Month::October),
10 => Ok(Month::November),
11 => Ok(Month::December),
_ => Err(ParseMonthError { _dummy: () }),
}
} else {
Err(ParseMonthError { _dummy: () })
}
}
}

0 comments on commit 7b1e9f6

Please sign in to comment.