Struct nichi::Date

source ·
pub struct Date { /* private fields */ }
Expand description

Calendar date

Implementations§

source§

impl Date

source

pub const fn new(year: u16, month: u8, day: u8) -> Self

Create a new Date from numbers

Panics

This function panics if:

  • month == 0
  • month > 12
  • day == 0
  • day < 32
Date::new(2000, 0, 0);
Date::new(2000, 1, 32);
Date::new(2000, 13, 31);
source

pub const fn new_saturating(year: u16, month: u8, day: u8) -> Self

let date = Date::new_saturating(2000, 0, 0);
assert_eq!(date.inner(), (2000, 1, 1));

let date = Date::new_saturating(2000, 13, 32);
assert_eq!(date.inner(), (2000, 12, 31));
source

pub const fn new_wrapping(year: u16, month: u8, day: u8) -> Self

// Year does not wrap.

let date = Date::new_wrapping(2000, 0, 0);
assert_eq!(date.inner(), (2000, 12, 31));

let date = Date::new_wrapping(2000, 13, 32);
assert_eq!(date.inner(), (2000, 1, 1));
source

pub const fn new_typed(year: Year, month: Month, day: Day) -> Self

Create a new Date from typed Year, Month, and Day

let date = Date::new_typed(
	Year(2000_u16),
	Month::December,
	Day::TwentyFifth
);

// Christmas in the year 2000 was on a Monday.
assert_eq!(date.weekday(), Weekday::Monday);
source

pub const fn weekday(self) -> Weekday

Receive the corresponding Weekday of this Date.

This uses Tomohiko Sakamoto’s algorithm.

It is accurate for any Date.

// US Independence day was on a Thursday.
assert_eq!(Date::new(1776, 7, 4).weekday(), Weekday::Thursday);

// Nintendo Switch was released on a Friday.
assert_eq!(Date::new(2017, 3, 3).weekday(), Weekday::Friday);

// Christmas in 1999 was on a Saturday.
assert_eq!(Date::new(1999, 12, 25).weekday(), Weekday::Saturday);

// A good album was released on a Wednesday.
assert_eq!(Date::new(2018, 4, 25).weekday(), Weekday::Wednesday);
source

pub const fn weekday_raw(year: u16, month: u8, day: u8) -> Weekday

Same as Date::weekday but with raw number primitives

source

pub const fn inner(self) -> (u16, u8, u8)

let date = Date::new(2000, 12, 25);
let ((year, month, day)) = date.inner();

assert_eq!((year, month, day), (2000, 12, 25));
source

pub const fn inner_typed(self) -> (Year, Month, Day)

let date = Date::new(2000, 12, 25);
assert_eq!(date.inner_typed(), (Year(2000), Month::new(12), Day::new(25)));
source

pub const fn year(self) -> Year

let date = Date::new(2000, 12, 25);
assert_eq!(date.year(), 2000);
source

pub const fn month(self) -> Month

let date = Date::new(2000, 12, 25);
assert_eq!(date.month(), 12);
source

pub const fn day(self) -> Day

let date = Date::new(2000, 12, 25);
assert_eq!(date.day(), 25);
source

pub fn from_str(s: &str) -> Option<Self>

Create Date from a string

Invariants
  • The year must be 1000..=9999
  • The month must be at least the first 3 letters of the month in english (oct, Dec, SEP, etc)
  • The day must be a number, either optionally with a leading 0 or suffixed by th, rd, nd, st (but not both, e.g, 3rd is OK, 03 is OK, 03rd is INVALID)

The order of the year, month, and day do not matter:

let december_25th_2010 = Date::new(2010, 12, 25);
assert_eq!(Date::from_str("dec 25 2010").unwrap(), december_25th_2010);
assert_eq!(Date::from_str("2010 dec 25").unwrap(), december_25th_2010);
assert_eq!(Date::from_str("2010 25th Dec").unwrap(), december_25th_2010);
assert_eq!(Date::from_str("25TH 2010 DEC").unwrap(), december_25th_2010);

Infinite amount of separator characters are allowed:

let december_25th_2010 = Date::new(2010, 12, 25);
assert_eq!(Date::from_str("dec-25 ...       2010").unwrap(), december_25th_2010);

This function is extremely leniant, as long as some resemblance of a calendar date is in the input string, it will parse it out:

//                                            Year 2010
//                                  25th day      |
//                         December     |         |
//                            |         |         |
assert_eq!( //                v         v         v
	Date::from_str("----fasdf decBR wef 25 a - >.a2010a...aa").unwrap(),
	Date::new(2010, 12, 25),
);
ISO 8601 (like)

This function also parses ISO 8601-like dates.

The year, month, and day must be available in that order.

A single separator character must exist, although it does not need to be -.

assert_eq!(Date::from_str("2010-12-25").unwrap(), Date::new(2010, 12, 25));
assert_eq!(Date::from_str("2010.02.02").unwrap(), Date::new(2010, 2, 2));
assert_eq!(Date::from_str("2010/2/2").unwrap(),   Date::new(2010, 2, 2));
assert_eq!(Date::from_str("2010_02_2").unwrap(),  Date::new(2010, 2, 2));
assert_eq!(Date::from_str("2010 2 02").unwrap(),  Date::new(2010, 2, 2));
Examples
let december_25th_2010 = Date::new(2010, 12, 25);

assert_eq!(Date::from_str("dec, 25, 2010").unwrap(),        december_25th_2010);
assert_eq!(Date::from_str("dec 25 2010").unwrap(),          december_25th_2010);
assert_eq!(Date::from_str("Dec 25th 2010").unwrap(),        december_25th_2010);
assert_eq!(Date::from_str("DEC 25TH 2010").unwrap(),        december_25th_2010);
assert_eq!(Date::from_str("DEC-25th-2010").unwrap(),        december_25th_2010);
assert_eq!(Date::from_str("2010.dec.25").unwrap(),          december_25th_2010);
assert_eq!(Date::from_str("2010, 25th, Dec").unwrap(),      december_25th_2010);
assert_eq!(Date::from_str("2010 december 25th").unwrap(),   december_25th_2010);
assert_eq!(Date::from_str("2010, DECEMBER, 25th").unwrap(), december_25th_2010);
assert_eq!(Date::from_str("DECEMBER 25th 2010").unwrap(),   december_25th_2010);
assert_eq!(Date::from_str("December 25th, 2010").unwrap(),  december_25th_2010);

let april_3rd_1000 = Date::new(1000, 4, 3);
assert_eq!(Date::from_str("apr, 3, 1000").unwrap(),     april_3rd_1000);
assert_eq!(Date::from_str("apr 03 1000").unwrap(),      april_3rd_1000);
assert_eq!(Date::from_str("Apr 3rd 1000").unwrap(),    april_3rd_1000);
assert_eq!(Date::from_str("APR 3RD 1000").unwrap(),     april_3rd_1000);
assert_eq!(Date::from_str("APR-3RD-1000").unwrap(),    april_3rd_1000);
assert_eq!(Date::from_str("1000.apr.03").unwrap(),      april_3rd_1000);
assert_eq!(Date::from_str("1000, 3rd, Apr").unwrap(),   april_3rd_1000);
assert_eq!(Date::from_str("1000 april 3rd").unwrap(),  april_3rd_1000);
assert_eq!(Date::from_str("1000, APRIL, 3RD").unwrap(), april_3rd_1000);
assert_eq!(Date::from_str("APRIL 3rd 1000").unwrap(),   april_3rd_1000);
assert_eq!(Date::from_str("April 3rd, 1000").unwrap(), april_3rd_1000);

Trait Implementations§

source§

impl<'__de> BorrowDecode<'__de> for Date

source§

fn borrow_decode<__D: BorrowDecoder<'__de>>( decoder: &mut __D ) -> Result<Self, DecodeError>

Attempt to decode this type with the given BorrowDecode.
source§

impl Clone for Date

source§

fn clone(&self) -> Date

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Date

source§

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

Formats the value using the given formatter. Read more
source§

impl Decode for Date

source§

fn decode<__D: Decoder>(decoder: &mut __D) -> Result<Self, DecodeError>

Attempt to decode this type with the given Decode.
source§

impl Default for Date

source§

fn default() -> Date

Returns the “default value” for a type. Read more
source§

impl<'de> Deserialize<'de> for Date

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Encode for Date

source§

fn encode<__E: Encoder>(&self, encoder: &mut __E) -> Result<(), EncodeError>

Encode a given type.
source§

impl Hash for Date

source§

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

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Ord for Date

source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl PartialEq for Date

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd for Date

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

fn lt(&self, other: &Rhs) -> bool

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

fn le(&self, other: &Rhs) -> bool

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

fn gt(&self, other: &Rhs) -> bool

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

fn ge(&self, other: &Rhs) -> bool

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Serialize for Date

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Copy for Date

source§

impl Eq for Date

source§

impl StructuralEq for Date

source§

impl StructuralPartialEq for Date

Auto Trait Implementations§

§

impl RefUnwindSafe for Date

§

impl Send for Date

§

impl Sync for Date

§

impl Unpin for Date

§

impl UnwindSafe for Date

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

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

source§

fn into(self) -> U

Calls U::from(self).

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

source§

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

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

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

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

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

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

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

Performs the conversion.
source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,