Struct date::Date

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

A representation of a single date.

Implementations§

source§

impl Date

source

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

Construct a new Date from the provided year, month, and day.

§Examples
use date::Date;
let date = Date::new(2012, 4, 21);
§Panic

This function panics if it receives “out-of-bounds” values (e.g. “March 32” or “February 30”). However, it can be convenient to be able to send such values to avoid having to handle overflow yourself; use Date::overflowing_new for this purpose.

source

pub const fn overflowing_new(year: i16, month: u8, day: u8) -> Self

Construct a new Date from the provided year, month, and day.

This function accepts “overflow” values that would lead to invalid dates, and canonicalizes them to correct dates, allowing for some math to be done on the inputs without needing to perform overflow checks yourself.

For example, it’s legal to send “March 32” to this function, and it will yield April 1 of the same year. It’s also legal to send a month or day value of zero, and it will conform to the month or day (respectively) prior to the first.

source§

impl Date

source

pub const fn year(&self) -> i16

Returns the year number in the calendar date.

source

pub const fn month(&self) -> u8

Returns the month number, starting from 1.

The return value ranges from 1 to 12.

source

pub const fn day(&self) -> u8

Returns the day of the month, starting from 1.

The return value ranges from 1 to 31. (The last day of the month differs by months.)

source

pub const fn weekday(&self) -> Weekday

Return the weekday corresponding to the given date.

source§

impl Date

source

pub fn format<'a>(&'a self, format_str: &'a str) -> FormattedDate<'_>

Format the date according to the provided strftime specifier. The following date specifiers are supported:

§Year
TokenExampleDescription
%C20Gregorian year divided by 100, zero-padded to 2 digits
%Y2012Gregorian year, zero-padded to 4 digits
%y12Gregorian year modulo 100, zero-padded to 2 digits
§Month
TokenExampleDescription
%BDecemberFull English name of the month
%b or %hDecThree-letter abbreviation for the month’s English name
%m07Month number (0112), zero-padded to 2 digits
§Day
TokenExampleDescription
%d21Day number (0131), zero-padded to 2 digits
%aSatThree-letter abbreviation for the weekday’s English name
%ASaturdayFull English name of the weekday
%w6Integer representing the weekday: Sunday (0) to Saturday (6)
%u6Integer representing the weekday: Monday (1) to Sunday (7)
%j112Day of the year (001366), zero-padded to 3 digits
§Full Date Shortcuts
TokenExampleDescription
%D07/08/01Month-day-year format. Same as %m/%d/%y.
%F2001-07-08Year-month-day format (ISO 8601). Same as %Y-%m-%d.
%v8-Jul-2001Day-month-year format. Same as %e-%b-%Y.

For tokens with default padding, the padding can be modified or suppressed with a modifier:

Token ModifierEffect
- (e.g. %-d)Suppress padding
_ (e.g. %_d)Use spaces for padding
0 (e.g. %0d)Use zeroes for padding (currently the default for padded tokens)

Additionally, the following tokens convert to plain characters:

TokenLiteral
%n\n (newline)
%t\t (tab)
%%%

Trait Implementations§

source§

impl Add<DateInterval> for Date

source§

fn add(self, interval: DateInterval) -> Self::Output

Return a new Date that is the given number of days later.

§

type Output = Date

The resulting type after applying the + operator.
source§

impl AddAssign<DateInterval> for Date

source§

fn add_assign(&mut self, interval: DateInterval)

Performs the += operation. Read more
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<'de> Deserialize<'de> for Date

source§

fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error>

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

impl Display for Date

source§

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

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

impl FromStr for Date

§

type Err = ParseError

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. 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: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error>

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

impl Sub<DateInterval> for Date

source§

fn sub(self, interval: DateInterval) -> Self::Output

Return a new Date that is the given number of days earlier.

§

type Output = Date

The resulting type after applying the - operator.
source§

impl Sub for Date

§

type Output = DateInterval

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Date) -> Self::Output

Performs the - operation. Read more
source§

impl SubAssign<DateInterval> for Date

source§

fn sub_assign(&mut self, interval: DateInterval)

Performs the -= operation. Read more
source§

impl Copy for Date

source§

impl Eq for Date

source§

impl StructuralPartialEq for Date

Auto Trait Implementations§

§

impl Freeze for Date

§

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> ToString for T
where T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. 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>,