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
impl Date
Sourcepub fn now() -> Self
pub fn now() -> Self
Creates a new Date instance with SystemTime::now().
let date = Date::now();
assert!(2021 < date.year());Sourcepub fn from_ymd(year: i32, month: u32, day: u32) -> Result<Self, AstrolabeError>
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"));Sourcepub fn as_ymd(&self) -> (i32, u32, u32)
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);Sourcepub fn parse(string: &str, format: &str) -> Result<Self, AstrolabeError>
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"));Sourcepub fn format(&self, format: &str) -> String
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 Type | Pattern | Examples | Hint |
|---|---|---|---|
| era | G..GGG | AD | |
| GGGG | Anno Domini | * | |
| GGGGG | A | ||
| year | y | 2, 20, 201, 2017, 20173 | |
| yy | 02, 20, 01, 17, 73 | ||
| yyy | 002, 020, 201, 2017, 20173 | ||
| yyyy | 0002, 0020, 0201, 2017, 20173 | ||
| yyyyy+ | … | Unlimited length, padded with zeros. | |
| quarter | q | 2 | * |
| 02 | |||
| qqq | Q2 | ||
| qqqq | 2nd quarter | ||
| qqqqq | 2 | ||
| month | M | 9, 12 | |
| MM | 09, 12 | ||
| MMM | Sep | ||
| MMMM | September | * | |
| MMMMM | S | ||
| week | w | 8, 27 | Week of year |
| ww | 08, 27 | * | |
| days | d | 1 | Day of month |
| dd | 01 | * | |
| D | 1, 24, 135 | Day of year, * | |
| DD | 01, 24, 135 | ||
| DDD | 001, 024, 135 | ||
| week day | e | 3 | 1-7, 1 is Sunday, * |
| ee | 03 | 1-7, 1 is Sunday | |
| eee | Tue | ||
| eeee | Tuesday | ||
| eeeee | T | ||
| eeeeee | Tu | ||
| eeeeeee | 2 | 1-7, 1 is Monday | |
| eeeeeeee | 02 | 1-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''"));Sourcepub fn duration_between(&self, compare: &Self) -> Duration
pub fn duration_between(&self, compare: &Self) -> Duration
Returns the duration between the provided date.
Trait Implementations§
Source§impl AddAssign<Duration> for Date
impl AddAssign<Duration> for Date
Source§fn add_assign(&mut self, rhs: Duration)
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 DateUtilities for Date
impl DateUtilities for Date
Source§fn day_of_year(&self) -> u32
fn day_of_year(&self) -> u32
1-365 or 1-366).Source§fn from_timestamp(timestamp: i64) -> Self
fn from_timestamp(timestamp: i64) -> Self
Source§fn timestamp(&self) -> i64
fn timestamp(&self) -> i64
Source§fn set_year(&self, year: i32) -> Result<Self, AstrolabeError>
fn set_year(&self, year: i32) -> Result<Self, AstrolabeError>
-5879611..=5879611.Source§fn set_month(&self, month: u32) -> Result<Self, AstrolabeError>
fn set_month(&self, month: u32) -> Result<Self, AstrolabeError>
1..=12. Read moreSource§fn set_day(&self, day: u32) -> Result<Self, AstrolabeError>
fn set_day(&self, day: u32) -> Result<Self, AstrolabeError>
1..=31 and cannot be greater than the number of days in the current month. Read moreSource§fn set_day_of_year(&self, day_of_year: u32) -> Result<Self, AstrolabeError>
fn set_day_of_year(&self, day_of_year: u32) -> Result<Self, AstrolabeError>
1..=365 or 1..=366 in case of a leap year. Read moreSource§fn add_years(&self, years: u32) -> Self
fn add_years(&self, years: u32) -> Self
Source§fn add_months(&self, months: u32) -> Self
fn add_months(&self, months: u32) -> Self
Source§fn sub_years(&self, years: u32) -> Self
fn sub_years(&self, years: u32) -> Self
Source§fn sub_months(&self, months: u32) -> Self
fn sub_months(&self, months: u32) -> Self
Source§fn sub_days(&self, days: u32) -> Self
fn sub_days(&self, days: u32) -> Self
Source§fn clear_until_year(&self) -> Self
fn clear_until_year(&self) -> Self
Source§fn clear_until_month(&self) -> Self
fn clear_until_month(&self) -> Self
Source§fn clear_until_day(&self) -> Self
fn clear_until_day(&self) -> Self
Source§fn years_since(&self, compare: &Self) -> i32
fn years_since(&self, compare: &Self) -> i32
Source§fn months_since(&self, compare: &Self) -> i32
fn months_since(&self, compare: &Self) -> i32
Source§fn days_since(&self, compare: &Self) -> i64
fn days_since(&self, compare: &Self) -> i64
Source§impl Decode<'_, Postgres> for Date
Available on crate features sqlx-postgres and sqlx only.
impl Decode<'_, Postgres> for Date
sqlx-postgres and sqlx only.Source§fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>
fn decode(value: PgValueRef<'_>) -> Result<Self, BoxDynError>
Source§impl<'de> Deserialize<'de> for Date
Available on crate feature serde only.Deserialize a yyyy-MM-dd formatted string into a Date instance.
impl<'de> Deserialize<'de> for Date
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>,
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>where
D: Deserializer<'de>,
Source§impl Encode<'_, Postgres> for Date
Available on crate features sqlx-postgres and sqlx only.
impl Encode<'_, Postgres> for Date
sqlx-postgres and sqlx only.Source§fn encode_by_ref(
&self,
buf: &mut PgArgumentBuffer,
) -> Result<IsNull, BoxDynError>
fn encode_by_ref( &self, buf: &mut PgArgumentBuffer, ) -> Result<IsNull, BoxDynError>
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,
fn encode(
self,
buf: &mut <DB as Database>::ArgumentBuffer<'q>,
) -> Result<IsNull, Box<dyn Error + Sync + Send>>where
Self: Sized,
self into buf in the expected format for the database.fn produces(&self) -> Option<<DB as Database>::TypeInfo>
Source§impl Ord for Date
impl Ord for Date
Source§impl PartialOrd for Date
impl PartialOrd for Date
Source§impl PgHasArrayType for Date
Available on crate features sqlx-postgres and sqlx only.
impl PgHasArrayType for Date
sqlx-postgres and sqlx only.fn array_type_info() -> PgTypeInfo
fn array_compatible(ty: &PgTypeInfo) -> bool
Source§impl Serialize for Date
Available on crate feature serde only.Serialize a Date instance as yyyy-MM-dd.
impl Serialize for Date
serde only.Serialize a Date instance as yyyy-MM-dd.
Source§impl SubAssign<Duration> for Date
impl SubAssign<Duration> for Date
Source§fn sub_assign(&mut self, rhs: Duration)
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.
impl Copy for Date
impl Eq for Date
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
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