Struct Date
pub struct Date { /* private fields */ }
A Gregorian civil date.
A Gregorian civil date can be infallibly converted to and from
UnixEpochDay values.
Note that since this supports a year 0, this technically implements the
ISO 8601 proleptic calendar and not the Gregorian calendar. Notably, the
Gregorian calendar does not have a year 0. Therefore, a Date with a
year 0 is equivalent to the Gregorian 1 BCE year.
Implementations
impl Date
const MIN: Date = _;The minimum allowed Gregorian date.
This is guaranteed to be equivalent to
UnixEpochDay::MINwhen converted to a Unix epoch day.const MAX: Date = _;The maximum allowed Gregorian date.
This is guaranteed to be equivalent to
UnixEpochDay::MAXwhen converted to a Unix epoch day.const fn new(year: i16, month: i8, day: i8) -> Result<Date, RangeError>Creates a new date in the Gregorian calendar.
If the date is invalid or any of the values are out of their supported ranges, then an error is returned.
Note that, technically, the date returned is in the ISO 8601 proleptic calendar. The only difference between it and the Gregorian calendar is that ISO 8601 has a year
0. ISO 8601's year0corresponds to-1 BCEin the Gregorian calendar.const fn new_constrain(year: i16, month: i8, day: i8) -> Result<Date, RangeError>Like
Date::new, but constrains the day value to the last day ofmonth.This still returns an error when
day < 1or whenyearormonthare invalid.const fn from_day_of_year(year: i16, day: i16) -> Result<Date, RangeError>Returns the date corresponding to the day of the given year. The day of the year should be a value in
1..=366, with366only being valid ifyearis a leap year.Returns an error if
yearis not in the range specified byYear, or ifdayis invalid for the given year.const fn from_day_of_year_no_leap(year: i16, day: i16) -> Result<Date, RangeError>Returns the date corresponding to the day of the given year. The day of the year must be a value in
1..=365, with February 29 being completely ignored. That is, it is guaranteed that February 29 will never be returned by this function. It is impossible.Returns an error if
yearis not in the range specified byYear, or ifdayis outside the range1..=365.const fn year(self) -> i16Returns the year component of this date.
The value returned is guaranteed to be in the range specified by
Year.const fn month(self) -> i8Returns the month component of this date.
The value returned is guaranteed to be in the range
1..=12.const fn day(self) -> i8Returns the day component of this date.
The value returned is guaranteed to be in the range
1..=jiff_core::civil::days_in_month(date.year(), date.month()).const fn weekday(self) -> WeekdayReturns the weekday corresponding to this date.
const fn day_of_year(self) -> i16Returns the ordinal day of the year that this date resides in.
For leap years, this always returns a value in the range
1..=366. Otherwise, the value is in the range1..=365.const fn day_of_year_no_leap(self) -> Option<i16>Returns the ordinal day of the year that this date resides in, but ignores leap years.
That is, the range of possible values returned by this routine is
1..=365, even if this date resides in a leap year. If this date is February 29, then this routine returnsNone.The value
365always corresponds to the last day in the year, December 31, even for leap years.const fn first_of_month(self) -> DateReturns the first date of the month for this date.
const fn last_of_month(self) -> DateReturns the last date of the month for this date.
const fn days_in_month(self) -> i8Returns the total number of days in the the month in which this date resides.
This is guaranteed to always return one of the following values, depending on the year and the month: 28, 29, 30 or 31.
const fn first_of_year(self) -> DateReturns the first date of the year that this date resides in.
const fn last_of_year(self) -> DateReturns the last date of the year that this date resides in.
const fn days_in_year(self) -> i16Returns the number of days in the year for this date.
It is guaranteed that the value returned is either
365or366.const fn in_leap_year(self) -> boolReturns true when this date is in a leap year.
const fn yesterday(self) -> Result<Date, RangeError>Returns the day before this date.
Returns an error when this is the minimal date.
const fn tomorrow(self) -> Result<Date, RangeError>Returns the day after this date.
Returns an error when this is the maximal date.
const fn nth_weekday_of_month(&self, nth: i8, weekday: Weekday) -> Result<Date, RangeError>Returns the
nthweekday of the month represented by this date.nthmust be non-zero and otherwise in the range-5..=5. If it isn't, an error is returned.This also returns an error if
abs(nth)==5and there is no "5th" weekday of this month.const fn nth_weekday(self, nth: i32, weekday: Weekday) -> Result<Date, RangeError>Returns the "nth" weekday from this date, not including itself.
The
nthparameter can be positive or negative. A positive value computes the "nth" weekday starting at the day after this date and going forwards in time. A negative value computes the "nth" weekday starting at the day before this date and going backwards in time.For example, if this date's weekday is a Sunday and the first Sunday is asked for (that is,
date.nth_weekday(1, Weekday::Sunday)), then the result is a week from this date corresponding to the following Sunday.const fn checked_add(self, days: i32) -> Result<Date, RangeError>Adds the given number of days to this date.
Returns an error if the result is outside the valid range of a
Date.const fn checked_sub(self, days: i32) -> Result<Date, RangeError>Subtracts the given number of days to this date.
Returns an error if the result is outside the valid range of a
Date.const fn until(self, other: Date) -> i32Returns the number of days from this date until
other.This routine never overflows because of the enforced range on
Datevalues.Example
use date; let date1 = date; let date2 = date; assert_eq!; // The value will be negative when the dates are flipped: assert_eq!;Example: overflow isn't possible
Even when using the minimum or maximum values:
use Date; assert_eq!; assert_eq!;const fn since(self, other: Date) -> i32Returns the number of days from this date since
other.This routine never overflows because of the enforced range on
Datevalues.Example
use date; let date1 = date; let date2 = date; assert_eq!; // The value will be negative when the dates are flipped: assert_eq!;Example: overflow isn't possible
Even when using the minimum or maximum values:
use Date; assert_eq!;const fn to_unix_epoch_day(self) -> UnixEpochDayConverts a Gregorian date to a Unix epoch day.
const fn to_iso_week_date(self) -> ISOWeekDateConverts this Gregorian date to an ISO 8601 week date.
const fn at(self, hour: i8, minute: i8, second: i8, subsec_nanosecond: i32) -> DateTimeA convenience function for constructing a
DateTimefrom this date at the time given by its components.Panics
This panics if the provided values do not correspond to a valid
Time. All of the following conditions must be true:0 <= hour <= 230 <= minute <= 590 <= second <= 590 <= subsec_nanosecond <= 999,999,999
Similarly, when used in a const context, invalid parameters will prevent your Rust program from compiling.
Trait Implementations
impl Clone for Date
fn clone(&self) -> Date
impl Copy for Date
impl Debug for Date
fn fmt(&self, f: &mut Formatter<'_>) -> Result
impl Eq for Date
impl Hash for Date
fn hash<__H: Hasher>(&self, state: &mut __H)
impl Ord for Date
fn cmp(&self, other: &Date) -> Ordering
impl PartialEq for Date
fn eq(&self, other: &Date) -> bool
impl PartialOrd for Date
fn partial_cmp(&self, other: &Date) -> Option<Ordering>
impl StructuralPartialEq for Date
impl Sub for Date
type Output = i32;fn sub(self, rhs: Date) -> i32
Auto Trait Implementations
impl Freeze for Date
impl RefUnwindSafe for Date
impl Send for Date
impl Sync for Date
impl Unpin for Date
impl UnsafeUnpin for Date
impl UnwindSafe for Date
Blanket Implementations
impl<T> Any for Date
where
T: 'static + ?Sized,
fn type_id(&self) -> TypeId
impl<T> Borrow<T> for Date
where
T: ?Sized,
fn borrow(&self) -> &T
impl<T> BorrowMut<T> for Date
where
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
impl<T> CloneToUninit for Date
where
T: Clone,
unsafe fn clone_to_uninit(&self, dest: *mut u8)
impl<T> From<T> for Date
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Date
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn clone_into(&self, target: &mut T)
impl<T, U> Into<U> for Date
where
U: From<T>,
fn into(self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom<U> for Date
where
U: Into<T>,
type Error = Infallible;fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto<U> for Date
where
U: TryFrom<T>,
type Error = <U as TryFrom<T>>::Error;fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>