pub struct Date { /* private fields */ }Expand description
Calendar date
Implementations§
source§impl Date
impl Date
sourcepub const fn new_saturating(year: u16, month: u8, day: u8) -> Self
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));sourcepub const fn new_wrapping(year: u16, month: u8, day: u8) -> Self
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));sourcepub const fn weekday(self) -> Weekday
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);sourcepub const fn weekday_raw(year: u16, month: u8, day: u8) -> Weekday
pub const fn weekday_raw(year: u16, month: u8, day: u8) -> Weekday
Same as Date::weekday but with raw number primitives
sourcepub const fn inner(self) -> (u16, u8, u8)
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));sourcepub const fn inner_typed(self) -> (Year, Month, Day)
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)));sourcepub const fn year(self) -> Year
pub const fn year(self) -> Year
let date = Date::new(2000, 12, 25);
assert_eq!(date.year(), 2000);sourcepub const fn month(self) -> Month
pub const fn month(self) -> Month
let date = Date::new(2000, 12, 25);
assert_eq!(date.month(), 12);sourcepub fn from_str(s: &str) -> Option<Self>
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
0or suffixed byth,rd,nd,st(but not both, e.g,3rdis OK,03is OK,03rdis 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
impl<'__de> BorrowDecode<'__de> for Date
source§fn borrow_decode<__D: BorrowDecoder<'__de>>(
decoder: &mut __D
) -> Result<Self, DecodeError>
fn borrow_decode<__D: BorrowDecoder<'__de>>( decoder: &mut __D ) -> Result<Self, DecodeError>
Attempt to decode this type with the given BorrowDecode.
source§impl<'de> Deserialize<'de> for Date
impl<'de> Deserialize<'de> for Date
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
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 Ord for Date
impl Ord for Date
source§impl PartialEq for Date
impl PartialEq for Date
source§impl PartialOrd for Date
impl PartialOrd for Date
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
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 moreimpl Copy for Date
impl Eq for Date
impl StructuralEq for Date
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more