Date

Struct Date 

Source
pub struct Date { /* private fields */ }
Expand description

Date in the proleptic Gregorian calendar.

See the DateUtilities implementation for get, set and manipulation methods.

Range: 30. June -5879611..=12. July 5879611. Please note that year 0 does not exist. After year -1 follows year 1.

Implementations§

Source§

impl Date

Source

pub fn now() -> Self

Creates a new Date instance with SystemTime::now().

let date = Date::now();
assert!(2021 < date.year());
Source

pub fn from_ymd(year: i32, month: u32, day: u32) -> Result<Self, AstrolabeError>

Creates a new Date instance from year, month and day (day of month).

Returns an OutOfRange error if the provided values are invalid.

let date = Date::from_ymd(2022, 05, 02).unwrap();
assert_eq!("2022/05/02", date.format("yyyy/MM/dd"));
Source

pub fn as_ymd(&self) -> (i32, u32, u32)

Returns the date.

let date = Date::from_ymd(2022, 05, 02).unwrap();
let (year, month, day) = date.as_ymd();
assert_eq!(2022, year);
assert_eq!(5, month);
assert_eq!(2, day);
Source

pub fn parse(string: &str, format: &str) -> Result<Self, AstrolabeError>

Parses a string with a given format and creates a new Date instance from it. See Date::format for a list of available symbols.

Returns an InvalidFormat error if the given string could not be parsed with the given format.

let date = Date::parse("2022-05-02", "yyyy-MM-dd").unwrap();
assert_eq!("2022/05/02", date.format("yyyy/MM/dd"));
Source

pub fn format(&self, format: &str) -> String

Formatting with format strings based on Unicode Date Field Symbols.

Please note that not all symbols are implemented. If you need something that is not implemented, please open an issue on GitHub describing your need.

§Available Symbols:
Field TypePatternExamplesHint
eraG..GGGAD
GGGGAnno Domini*
GGGGGA
yeary2, 20, 201, 2017, 20173
yy02, 20, 01, 17, 73
yyy002, 020, 201, 2017, 20173
yyyy0002, 0020, 0201, 2017, 20173
yyyyy+Unlimited length,
padded with zeros.
quarterq2*
qq02
qqqQ2
qqqq2nd quarter
qqqqq2
monthM9, 12
MM09, 12
MMMSep
MMMMSeptember*
MMMMMS
weekw8, 27Week of year
ww08, 27*
daysd1Day of month
dd01*
D1, 24, 135Day of year, *
DD01, 24, 135
DDD001, 024, 135
week daye31-7, 1 is Sunday, *
ee031-7, 1 is Sunday
eeeTue
eeeeTuesday
eeeeeT
eeeeeeTu
eeeeeee21-7, 1 is Monday
eeeeeeee021-7, 1 is Monday

* = Default

If the sequence is longer than listed in the table, the output will be the same as the default pattern for this unit (marked with *).

Surround any character with apostrophes (') to escape them. If you want escape ', write ''.

let date = Date::from_ymd(2022, 5, 2).unwrap();
assert_eq!("2022/05/02", date.format("yyyy/MM/dd"));
// Escape characters
assert_eq!("2022/MM/dd", date.format("yyyy/'MM/dd'"));
assert_eq!("2022/'05/02'", date.format("yyyy/''MM/dd''"));
Source

pub fn duration_between(&self, compare: &Self) -> Duration

Returns the duration between the provided date.

Trait Implementations§

Source§

impl Add<Duration> for Date

Source§

fn add(self, rhs: Duration) -> Self::Output

Performs the + operation.

Only adds full days (86 400 seconds) to Date. Any additional duration will be ignored.

Source§

type Output = Date

The resulting type after applying the + operator.
Source§

impl AddAssign<Duration> for Date

Source§

fn add_assign(&mut self, rhs: Duration)

Performs the += operation.

Only adds full days (86 400 seconds) to Date. Any additional duration will be ignored.

Source§

impl Clone for Date

Source§

fn clone(&self) -> Date

Returns a duplicate of the value. Read more
1.0.0§

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

Performs copy-assignment from source. Read more
Source§

impl DateUtilities for Date

Source§

fn year(&self) -> i32

Returns the year.
Source§

fn month(&self) -> u32

Returns the month of the year (1-12).
Source§

fn day(&self) -> u32

Returns the day of the month (1-31).
Source§

fn day_of_year(&self) -> u32

Returns the day of the year (1-365 or 1-366).
Source§

fn weekday(&self) -> u8

Returns the day of the week (0-6, 0 is Sunday).
Source§

fn from_timestamp(timestamp: i64) -> Self

Creates a date from a unix timestamp (non-leap seconds since January 1, 1970 00:00:00 UTC). Read more
Source§

fn timestamp(&self) -> i64

Returns the number of non-leap seconds since January 1, 1970 00:00:00 UTC. (Negative if date is before)
Source§

fn set_year(&self, year: i32) -> Result<Self, AstrolabeError>

Sets the year to the provided value. Has to be in range -5879611..=5879611.
Source§

fn set_month(&self, month: u32) -> Result<Self, AstrolabeError>

Sets the month of the year to the provided value. Has to be in range 1..=12. Read more
Source§

fn set_day(&self, day: u32) -> Result<Self, AstrolabeError>

Sets the day of the month to the provided value. Has to be in range 1..=31 and cannot be greater than the number of days in the current month. Read more
Source§

fn set_day_of_year(&self, day_of_year: u32) -> Result<Self, AstrolabeError>

Sets the day of the year to the provided value. Has to be in range 1..=365 or 1..=366 in case of a leap year. Read more
Source§

fn add_years(&self, years: u32) -> Self

Adds the provided years to the current date. Read more
Source§

fn add_months(&self, months: u32) -> Self

Adds the provided months to the current date. Read more
Source§

fn add_days(&self, days: u32) -> Self

Adds the provided days to the current date. Read more
Source§

fn sub_years(&self, years: u32) -> Self

Subtracts the provided years from the current date. Read more
Source§

fn sub_months(&self, months: u32) -> Self

Subtracts the provided months from the current date. Read more
Source§

fn sub_days(&self, days: u32) -> Self

Subtracts the provided days from the current date. Read more
Source§

fn clear_until_year(&self) -> Self

Clears date/time units until the year (inclusive).
Source§

fn clear_until_month(&self) -> Self

Clears date/time units until the month (inclusive).
Source§

fn clear_until_day(&self) -> Self

Clears date/time units until the day (inclusive).
Source§

fn years_since(&self, compare: &Self) -> i32

Returns full years since the provided date.
Source§

fn months_since(&self, compare: &Self) -> i32

Returns full months since the provided date.
Source§

fn days_since(&self, compare: &Self) -> i64

Returns full days since the provided date.
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<'_, Postgres> for Date

Available on crate features sqlx-postgres and sqlx only.
Source§

fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>

Decode a new value of this type using a raw value from the database.
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

Available on crate feature serde only.

Deserialize a yyyy-MM-dd formatted string into a Date instance.

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

Source§

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

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

impl Encode<'_, Postgres> for Date

Available on crate features sqlx-postgres and sqlx only.
Source§

fn encode_by_ref( &self, buf: &mut PgArgumentBuffer, ) -> Result<IsNull, BoxDynError>

Writes the value of self into buf without moving self. Read more
Source§

fn size_hint(&self) -> usize

Source§

fn encode( self, buf: &mut <DB as Database>::ArgumentBuffer<'q>, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>
where Self: Sized,

Writes the value of self into buf in the expected format for the database.
Source§

fn produces(&self) -> Option<<DB as Database>::TypeInfo>

Source§

impl From<&Date> for Date

Source§

fn from(date: &Date) -> Self

Converts to this type from the input type.
Source§

impl From<&Date> for DateTime

Source§

fn from(value: &Date) -> Self

Converts to this type from the input type.
Source§

impl From<&DateTime> for Date

Source§

fn from(value: &DateTime) -> Self

Converts to this type from the input type.
Source§

impl From<Date> for DateTime

Source§

fn from(value: Date) -> Self

Converts to this type from the input type.
Source§

impl From<DateTime> for Date

Source§

fn from(value: DateTime) -> Self

Converts to this type from the input type.
Source§

impl FromStr for Date

Source§

type Err = AstrolabeError

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§

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

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

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

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

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

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

impl PartialEq for Date

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0§

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

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§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl PgHasArrayType for Date

Available on crate features sqlx-postgres and sqlx only.
Source§

impl Serialize for Date

Available on crate feature serde only.

Serialize a Date instance as yyyy-MM-dd.

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 Sub<Duration> for Date

Source§

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

Performs the - operation.

Only removes full days (86 400 seconds) to Date. Any additional duration will be ignored.

Source§

type Output = Date

The resulting type after applying the - operator.
Source§

impl SubAssign<Duration> for Date

Source§

fn sub_assign(&mut self, rhs: Duration)

Performs the -= operation.

Only removes full days (86 400 seconds) to Date. Any additional duration will be ignored.

Source§

impl Type<Postgres> for Date

Available on crate features sqlx-postgres and sqlx only.
Source§

fn type_info() -> PgTypeInfo

Returns the canonical SQL type for this Rust type. Read more
Source§

fn compatible(ty: &<DB as Database>::TypeInfo) -> bool

Determines if this Rust type is compatible with the given SQL type. 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§

§

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

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

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

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

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

§

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

Mutably borrows from an owned value. Read more
§

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

§

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

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
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.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
§

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

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

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

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

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

impl<T> ToString for T
where T: Display + ?Sized,

§

fn to_string(&self) -> String

Converts the given value to a String. Read more
§

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

§

type Error = Infallible

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

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

Performs the conversion.
§

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.
§

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

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

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