Struct DateWith
struct DateWith { ... }
A builder for setting the fields on a Date.
This builder is constructed via Date::with.
Example
The builder ensures one can chain together the individual components
of a date without it failing at an intermediate step. For example,
if you had a date of 2024-10-31 and wanted to change both the day
and the month, and each setting was validated independent of the other,
you would need to be careful to set the day first and then the month.
In some cases, you would need to set the month first and then the day!
But with the builder, you can set values in any order:
use date;
let d1 = date;
let d2 = d1.with.month.day.build?;
assert_eq!;
let d1 = date;
let d2 = d1.with.day.month.build?;
assert_eq!;
# Ok::
Implementations
impl DateWith
fn build(self: Self) -> Result<Date, Error>Create a new
Datefrom the fields set on this configuration.An error occurs when the fields combine to an invalid date.
For any fields not set on this configuration, the values are taken from the
Datethat originally created this configuration. When no values are set, this routine is guaranteed to succeed and will always return the original date without modification.Example
This creates a date corresponding to the last day in the year:
use date; assert_eq!; // It also works with leap years for the same input: assert_eq!; # Ok::Example: error for invalid date
If the fields combine to form an invalid date, then an error is returned:
use date; let d = date; assert!; let d = date; assert!;fn year(self: Self, year: i16) -> DateWithSet the year field on a
Date.One can access this value via
Date::year.This overrides any previous year settings.
Errors
This returns an error when
DateWith::buildis called if the given year is outside the range-9999..=9999. This can also return an error if the resulting date is otherwise invalid.Example
This shows how to create a new date with a different year:
use date; let d1 = date; assert_eq!; let d2 = d1.with.year.build?; assert_eq!; # Ok::Example: only changing the year can fail
For example, while
2024-02-29is valid,2023-02-29is not:use date; let d1 = date; assert!;fn era_year(self: Self, year: i16, era: Era) -> DateWithSet year of a date via its era and its non-negative numeric component.
One can access this value via
Date::era_year.Errors
This returns an error when
DateWith::buildis called if the year is outside the range for the era specified. ForEra::BCE, the range is1..=10000. ForEra::CE, the range is1..=9999.Example
This shows that
CEyears are equivalent to the years used by this crate:use ; let d1 = date; assert_eq!; let d2 = d1.with.era_year.build?; assert_eq!; // CE years are always positive and can be at most 9999: assert!; assert!; # Ok::But
BCEyears always correspond to years less than or equal to0in this crate:use ; let d1 = date; assert_eq!; assert_eq!; let d2 = d1.with.era_year.build?; assert_eq!; assert_eq!; let d2 = d1.with.era_year.build?; assert_eq!; assert_eq!; // BCE years are always positive and can be at most 10000: assert!; assert!; # Ok::Example: overrides
DateWith::yearSetting this option will override any previous
DateWith::yearoption:use ; let d1 = date; let d2 = d1.with.year.era_year.build?; assert_eq!; # Ok::Similarly,
DateWith::yearwill override any previous call toDateWith::era_year:use ; let d1 = date; let d2 = d1.with.era_year.year.build?; assert_eq!; # Ok::fn month(self: Self, month: i8) -> DateWithSet the month field on a
Date.One can access this value via
Date::month.This overrides any previous month settings.
Errors
This returns an error when
DateWith::buildis called if the given month is outside the range1..=12. This can also return an error if the resulting date is otherwise invalid.Example
This shows how to create a new date with a different month:
use date; let d1 = date; assert_eq!; let d2 = d1.with.month.build?; assert_eq!; # Ok::Example: only changing the month can fail
For example, while
2024-10-31is valid,2024-11-31is not:use date; let d = date; assert!;fn day(self: Self, day: i8) -> DateWithSet the day field on a
Date.One can access this value via
Date::day.This overrides any previous day settings.
Errors
This returns an error when
DateWith::buildis called if the given given day is outside of allowable days for the corresponding year and month fields.Example
This shows some examples of setting the day, including a leap day:
use date; let d1 = date; assert_eq!; let d2 = d1.with.day.build?; assert_eq!; let d3 = d1.with.day.build?; assert_eq!; # Ok::Example: changing only the day can fail
This shows some examples that will fail:
use date; let d1 = date; // 2023 is not a leap year assert!; // September has 30 days, not 31. let d1 = date; assert!;fn day_of_year(self: Self, day: i16) -> DateWithSet the day field on a
Datevia the ordinal number of a day within a year.When used, any settings for month are ignored since the month is determined by the day of the year.
The valid values for
dayare1..=366. Note though that366is only valid for leap years.This overrides any previous day settings.
Errors
This returns an error when
DateWith::buildis called if the given day is outside the allowed range of1..=366, or when a value of366is given for a non-leap year.Example
This demonstrates that if a year is a leap year, then
60corresponds to February 29:use date; let d = date; assert_eq!; # Ok::But for non-leap years, day 60 is March 1:
use date; let d = date; assert_eq!; # Ok::And using
366for a non-leap year will result in an error, since non-leap years only have 365 days:use date; let d = date; assert!; // The maximal year is not a leap year, so it returns an error too. let d = date; assert!;fn day_of_year_no_leap(self: Self, day: i16) -> DateWithSet the day field on a
Datevia the ordinal number of a day within a year, but ignoring leap years.When used, any settings for month are ignored since the month is determined by the day of the year.
The valid values for
dayare1..=365. The value365always corresponds to the last day of the year, even for leap years. It is impossible for this routine to return a date corresponding to February 29.This overrides any previous day settings.
Errors
This returns an error when
DateWith::buildis called if the given day is outside the allowed range of1..=365.Example
This demonstrates that
60corresponds to March 1, regardless of whether the year is a leap year or not:use date; assert_eq!; assert_eq!; # Ok::And using
365for any year will always yield the last day of the year:use date; let d = date; assert_eq!; let d = date; assert_eq!; let d = date; assert_eq!; # Ok::A value of
366is out of bounds, even for leap years:use date; let d = date; assert!;
impl Clone for DateWith
fn clone(self: &Self) -> DateWith
impl Copy for DateWith
impl Debug for DateWith
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Freeze for DateWith
impl RefUnwindSafe for DateWith
impl Send for DateWith
impl Sync for DateWith
impl Unpin for DateWith
impl UnsafeUnpin for DateWith
impl UnwindSafe for DateWith
impl<T> Any for DateWith
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for DateWith
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for DateWith
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for DateWith
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for DateWith
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for DateWith
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for DateWith
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for DateWith
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for DateWith
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>