Enum Month

pub enum Month

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

# use chrono::prelude::*;
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 = 0

January

February = 1

February

March = 2

March

April = 3

April

May = 4

May

June = 5

June

July = 6

July

August = 7

August

September = 8

September

October = 9

October

November = 10

November

December = 11

December

Implementations

impl Month

const fn succ(&self) -> Month

The next month.

m: January February ... December
m.succ(): February March ... January
const fn pred(&self) -> Month

The previous month.

m: January February ... December
m.pred(): December January ... November
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
const fn name(&self) -> &'static str

Get the name of the month

use chrono::Month;

assert_eq!(Month::January.name(), "January")
fn num_days(&self, year: i32) -> Option<u8>

Get the length in days of the month

Yields None if year is out of range for NaiveDate.

Trait Implementations

impl Clone for Month

fn clone(&self) -> Month

impl Copy for Month

impl Debug for Month

fn fmt(&self, f: &mut Formatter<'_>) -> Result

impl Eq for Month

impl FromPrimitive for 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)

fn from_i64(n: i64) -> Option<Month>
fn from_u32(n: u32) -> Option<Month>

impl FromStr for Month

type Err = ParseMonthError;
fn from_str(s: &str) -> Result<Self, Self::Err>

impl Hash for Month

fn hash<__H: Hasher>(&self, state: &mut __H)

impl Ord for Month

fn cmp(&self, other: &Month) -> Ordering

impl PartialEq for Month

fn eq(&self, other: &Month) -> bool

impl PartialOrd for Month

fn partial_cmp(&self, other: &Month) -> Option<Ordering>

impl StructuralPartialEq for Month

impl TryFrom<u8> for Month

type Error = OutOfRange;
fn try_from(value: u8) -> Result<Self, Self::Error>

Auto Trait Implementations

impl Freeze for Month

impl RefUnwindSafe for Month

impl Send for Month

impl Sync for Month

impl Unpin for Month

impl UnsafeUnpin for Month

impl UnwindSafe for Month

Blanket Implementations

impl<T> Any for Month where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Month where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Month where T: ?Sized,

fn borrow_mut(&mut self) -> &mut T

impl<T> CloneToUninit for Month where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> From<T> for Month

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Month where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Month where U: From<T>,

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom<U> for Month where U: Into<T>,

type Error = Infallible;
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto<U> for Month where U: TryFrom<T>,

type Error = <U as TryFrom<T>>::Error;
fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>