pub enum Month {
January,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December,
}
Expand description
The month of the year.
This enum is just a convenience implementation. The month in dates created by DateLike objects does not return this enum.
It is possible to convert from a date to a month independently
use chrono::prelude::*;
let date = Utc.with_ymd_and_hms(2019, 10, 28, 9, 10, 11).unwrap();
// `2019-10-28T09:10:11Z`
let month = Month::try_from(u8::try_from(date.month()).unwrap()).ok();
assert_eq!(month, Some(Month::October))
Or from a Month to an integer usable by dates
let month = Month::January;
let dt = Utc.with_ymd_and_hms(2019, month.number_from_month(), 28, 9, 10, 11).unwrap();
assert_eq!((dt.year(), dt.month(), dt.day()), (2019, 1, 28));
Allows mapping from and to month, from 1-January to 12-December. Can be Serialized/Deserialized with serde
Variants
January
January
February
February
March
March
April
April
May
May
June
June
July
July
August
August
September
September
October
October
November
November
December
December
Implementations
sourceimpl Month
impl Month
sourcepub const fn succ(&self) -> Month
pub const fn succ(&self) -> Month
The next month.
m : | January | February | ... | December |
---|---|---|---|---|
m.succ() : | February | March | ... | January |
sourcepub const fn pred(&self) -> Month
pub const fn pred(&self) -> Month
The previous month.
m : | January | February | ... | December |
---|---|---|---|---|
m.pred() : | December | January | ... | November |
sourcepub const fn number_from_month(&self) -> u32
pub const fn number_from_month(&self) -> u32
Returns a month-of-year number starting from January = 1.
m : | January | February | ... | December |
---|---|---|---|---|
m.number_from_month() : | 1 | 2 | ... | 12 |
Trait Implementations
sourceimpl FromPrimitive for Month
impl FromPrimitive for Month
sourcefn from_u64(n: u64) -> Option<Month>
fn from_u64(n: u64) -> Option<Month>
Returns an Option<Month>
from a i64, assuming a 1-index, January = 1.
Month::from_i64(n: i64)
: | 1
| 2
| … | 12
—————————| –––––––––– | ——————— | … | —–
``: | Some(Month::January) | Some(Month::February) | … | Some(Month::December)
sourcefn from_i64(n: i64) -> Option<Month>
fn from_i64(n: i64) -> Option<Month>
i64
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_u32(n: u32) -> Option<Month>
fn from_u32(n: u32) -> Option<Month>
u32
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_isize(n: isize) -> Option<Self>
fn from_isize(n: isize) -> Option<Self>
isize
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_i8(n: i8) -> Option<Self>
fn from_i8(n: i8) -> Option<Self>
i8
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_i16(n: i16) -> Option<Self>
fn from_i16(n: i16) -> Option<Self>
i16
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_i32(n: i32) -> Option<Self>
fn from_i32(n: i32) -> Option<Self>
i32
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_usize(n: usize) -> Option<Self>
fn from_usize(n: usize) -> Option<Self>
usize
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_u8(n: u8) -> Option<Self>
fn from_u8(n: u8) -> Option<Self>
u8
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourcefn from_u16(n: u16) -> Option<Self>
fn from_u16(n: u16) -> Option<Self>
u16
to return an optional value of this type. If the
value cannot be represented by this type, then None
is returned. Read moresourceimpl FromStr for Month
impl FromStr for Month
Parsing a str
into a Month
uses the format %W
.
Example
use chrono::Month;
assert_eq!("January".parse::<Month>(), Ok(Month::January));
assert!("any day".parse::<Month>().is_err());
The parsing is case-insensitive.
assert_eq!("fEbruARy".parse::<Month>(), Ok(Month::February));
Only the shortest form (e.g. jan
) and the longest form (e.g. january
) is accepted.
assert!("septem".parse::<Month>().is_err());
assert!("Augustin".parse::<Month>().is_err());
type Err = ParseMonthError
type Err = ParseMonthError
sourceimpl Hash for Month
impl Hash for Month
sourcefn hash<__H: Hasher>(&self, state: &mut __H)
fn hash<__H: Hasher>(&self, state: &mut __H)
Hasher
]. Read more1.3.0fn hash_slice<H>(data: &[Self], state: &mut H)where
H: Hasher,
fn hash_slice<H>(data: &[Self], state: &mut H)where
H: Hasher,
Hasher
]. Read more