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);
assert_eq!(date.year(), 2012);
assert_eq!(date.month(), 4);
assert_eq!(date.day(), 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 from_timestamp(unix_timestamp: i64) -> Self

Construct a new Date based on the Unix timestamp.

§Examples
use date::date;
use date::Date;

let day_one = Date::from_timestamp(0);
assert_eq!(day_one, date! { 1970-01-01 });
let later = Date::from_timestamp(15_451 * 86_400);
assert_eq!(later, date! { 2012-04-21 });

Negative timestamps are also supported:

let before_unix_era = Date::from_timestamp(-1);
assert_eq!(before_unix_era, date! { 1969-12-31 });
let hobbit_publication = Date::from_timestamp(-11_790 * 86_400);
assert_eq!(hobbit_publication, date! { 1937-09-21 });
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

pub fn parse( date_str: impl AsRef<str>, date_fmt: &'static str ) -> ParseResult<Date>

Parse a date from a string, according to the provided format string.

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 day_of_year(&self) -> u16

The day of the current year. Range: [1, 366]

source

pub const fn week(&self) -> u16

The week number of the year (between 0 and 53, inclusive), with a new week starting each Sunday.

Week 1 begins on the first Sunday of the year; leading days before that are part of week 0.

source

pub const fn weekday(&self) -> Weekday

Return the weekday corresponding to the given date.

source§

impl Date

source

pub const fn timestamp(&self) -> i64

The Unix timestamp for this date at midnight UTC.

§Examples
assert_eq!(date! { 1969-12-31 }.timestamp(), -86_400);
assert_eq!(date! { 1970-01-01 }.timestamp(), 0);
assert_eq!(date! { 1970-01-05 }.timestamp(), 4 * 86_400);
assert_eq!(date! { 2012-04-21 }.timestamp(), 1334966400);
source§

impl Date

source

pub fn today_utc() -> Self

The date representing today, in UTC.

§Panic

This function will panic if the system clock is set to a time prior to January 1, 1970.

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)
%%%
source§

impl Date

source

pub fn iter_through(&self, end: Date) -> DateIterator

An iterator of dates beginning with this date, and ending with the provided end date (inclusive).

source§

impl Date

source

pub const MAX: Self = _

The maximum date that can be represented.

source

pub const MIN: Self = _

The minimum date that can be represented.

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 Add<MonthInterval> for Date

§

type Output = Date

The resulting type after applying the + operator.
source§

fn add(self, interval: MonthInterval) -> Self

Performs the + operation. Read more
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 From<RawDate> for Date

source§

fn from(value: RawDate) -> Self

Converts to this type from the input type.
source§

impl FromStr for Date

§

type Err = ParseError

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

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

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<MonthInterval> for Date

§

type Output = Date

The resulting type after applying the - operator.
source§

fn sub(self, interval: MonthInterval) -> Self

Performs the - operation. Read more
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>,