Struct DateTimeWith
struct DateTimeWith { ... }
A builder for setting the fields on a DateTime.
This builder is constructed via DateTime::with.
Example
The builder ensures one can chain together the individual components of a
datetime without it failing at an intermediate step. For example, if you
had a date of 2024-10-31T00:00:00 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 dt1 = date.at;
let dt2 = dt1.with.month.day.build?;
assert_eq!;
let dt1 = date.at;
let dt2 = dt1.with.day.month.build?;
assert_eq!;
# Ok::
Implementations
impl DateTimeWith
fn build(self: Self) -> Result<DateTime, Error>Create a new
DateTimefrom the fields set on this configuration.An error occurs when the fields combine to an invalid datetime.
For any fields not set on this configuration, the values are taken from the
DateTimethat originally created this configuration. When no values are set, this routine is guaranteed to succeed and will always return the original datetime without modification.Example
This creates a datetime corresponding to the last day in the year at noon:
use date; let dt = date.at; assert_eq!; // It also works with leap years for the same input: let dt = date.at; assert_eq!; # Ok::Example: error for invalid datetime
If the fields combine to form an invalid date, then an error is returned:
use date; let dt = date.at; assert!; let dt = date.at; assert!;fn date(self: Self, date: Date) -> DateTimeWithSet the year, month and day fields via the
Dategiven.This overrides any previous year, month or day settings.
Example
This shows how to create a new datetime with a different date:
use date; let dt1 = date.at; let dt2 = dt1.with.date.build?; // The date changes but the time remains the same. assert_eq!; # Ok::fn time(self: Self, time: Time) -> DateTimeWithSet the hour, minute, second, millisecond, microsecond and nanosecond fields via the
Timegiven.This overrides any previous hour, minute, second, millisecond, microsecond, nanosecond or subsecond nanosecond settings.
Example
This shows how to create a new datetime with a different time:
use ; let dt1 = date.at; let dt2 = dt1.with.time.build?; // The time changes but the date remains the same. assert_eq!; # Ok::fn year(self: Self, year: i16) -> DateTimeWithSet the year field on a
DateTime.One can access this value via
DateTime::year.This overrides any previous year settings.
Errors
This returns an error when
DateTimeWith::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 datetime with a different year:
use date; let dt1 = date.at; assert_eq!; let dt2 = dt1.with.year.build?; assert_eq!; # Ok::Example: only changing the year can fail
For example, while
2024-02-29T01:30:00is valid,2023-02-29T01:30:00is not:use date; let dt = date.at; assert!;fn era_year(self: Self, year: i16, era: Era) -> DateTimeWithSet year of a datetime via its era and its non-negative numeric component.
One can access this value via
DateTime::era_year.Errors
This returns an error when
DateTimeWith::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 dt1 = date.at; assert_eq!; let dt2 = dt1.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 dt1 = date.at; assert_eq!; assert_eq!; let dt2 = dt1.with.era_year.build?; assert_eq!; assert_eq!; let dt2 = dt1.with.era_year.build?; assert_eq!; assert_eq!; // BCE years are always positive and can be at most 10000: assert!; assert!; # Ok::Example: overrides
DateTimeWith::yearSetting this option will override any previous
DateTimeWith::yearoption:use ; let dt1 = date.at; let dt2 = dt1.with.year.era_year.build?; assert_eq!; # Ok::Similarly,
DateTimeWith::yearwill override any previous call toDateTimeWith::era_year:use ; let dt1 = date.at; let dt2 = dt1.with.era_year.year.build?; assert_eq!; # Ok::fn month(self: Self, month: i8) -> DateTimeWithSet the month field on a
DateTime.One can access this value via
DateTime::month.This overrides any previous month settings.
Errors
This returns an error when
DateTimeWith::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 datetime with a different month:
use date; let dt1 = date.at; assert_eq!; let dt2 = dt1.with.month.build?; assert_eq!; # Ok::Example: only changing the month can fail
For example, while
2024-10-31T00:00:00is valid,2024-11-31T00:00:00is not:use date; let dt = date.at; assert!;fn day(self: Self, day: i8) -> DateTimeWithSet the day field on a
DateTime.One can access this value via
DateTime::day.This overrides any previous day settings.
Errors
This returns an error when
DateTimeWith::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 dt1 = date.at; assert_eq!; let dt2 = dt1.with.day.build?; assert_eq!; let dt3 = dt1.with.day.build?; assert_eq!; # Ok::Example: changing only the day can fail
This shows some examples that will fail:
use date; let dt1 = date.at; // 2023 is not a leap year assert!; // September has 30 days, not 31. let dt1 = date.at; assert!;fn day_of_year(self: Self, day: i16) -> DateTimeWithSet the day field on a
DateTimevia 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
DateTimeWith::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 dt = date.at; assert_eq!; # Ok::But for non-leap years, day 60 is March 1:
use date; let dt = date.at; 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 dt = date.at; assert!; // The maximal year is not a leap year, so it returns an error too. let dt = date.at; assert!;fn day_of_year_no_leap(self: Self, day: i16) -> DateTimeWithSet the day field on a
DateTimevia 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 datetime corresponding to February 29.This overrides any previous day settings.
Errors
This returns an error when
DateTimeWith::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; let dt = date.at; assert_eq!; let dt = date.at; assert_eq!; # Ok::And using
365for any year will always yield the last day of the year:use date; let dt = date.at; assert_eq!; let dt = date.at; assert_eq!; let dt = date.at; assert_eq!; # Ok::A value of
366is out of bounds, even for leap years:use date; let dt = date.at; assert!;fn hour(self: Self, hour: i8) -> DateTimeWithSet the hour field on a
DateTime.One can access this value via
DateTime::hour.This overrides any previous hour settings.
Errors
This returns an error when
DateTimeWith::buildis called if the given hour is outside the range0..=23.Example
use time; let dt1 = time.on; assert_eq!; let dt2 = dt1.with.hour.build?; assert_eq!; # Ok::fn minute(self: Self, minute: i8) -> DateTimeWithSet the minute field on a
DateTime.One can access this value via
DateTime::minute.This overrides any previous minute settings.
Errors
This returns an error when
DateTimeWith::buildis called if the given minute is outside the range0..=59.Example
use time; let dt1 = time.on; assert_eq!; let dt2 = dt1.with.minute.build?; assert_eq!; # Ok::fn second(self: Self, second: i8) -> DateTimeWithSet the second field on a
DateTime.One can access this value via
DateTime::second.This overrides any previous second settings.
Errors
This returns an error when
DateTimeWith::buildis called if the given second is outside the range0..=59.Example
use time; let dt1 = time.on; assert_eq!; let dt2 = dt1.with.second.build?; assert_eq!; # Ok::fn millisecond(self: Self, millisecond: i16) -> DateTimeWithSet the millisecond field on a
DateTime.One can access this value via
DateTime::millisecond.This overrides any previous millisecond settings.
Errors
This returns an error when
DateTimeWith::buildis called if the given millisecond is outside the range0..=999, or if both this andDateTimeWith::subsec_nanosecondare set.Example
This shows the relationship between
DateTime::millisecondand [DateTime::subsec_nanosecond]:use time; let dt1 = time.on; let dt2 = dt1.with.millisecond.build?; assert_eq!; # Ok::fn microsecond(self: Self, microsecond: i16) -> DateTimeWithSet the microsecond field on a
DateTime.One can access this value via
DateTime::microsecond.This overrides any previous microsecond settings.
Errors
This returns an error when
DateTimeWith::buildis called if the given microsecond is outside the range0..=999, or if both this andDateTimeWith::subsec_nanosecondare set.Example
This shows the relationship between
DateTime::microsecondand [DateTime::subsec_nanosecond]:use time; let dt1 = time.on; let dt2 = dt1.with.microsecond.build?; assert_eq!; # Ok::fn nanosecond(self: Self, nanosecond: i16) -> DateTimeWithSet the nanosecond field on a
DateTime.One can access this value via
DateTime::nanosecond.This overrides any previous nanosecond settings.
Errors
This returns an error when
DateTimeWith::buildis called if the given nanosecond is outside the range0..=999, or if both this andDateTimeWith::subsec_nanosecondare set.Example
This shows the relationship between
DateTime::nanosecondand [DateTime::subsec_nanosecond]:use time; let dt1 = time.on; let dt2 = dt1.with.nanosecond.build?; assert_eq!; # Ok::fn subsec_nanosecond(self: Self, subsec_nanosecond: i32) -> DateTimeWithSet the subsecond nanosecond field on a
DateTime.If you want to access this value on
DateTime, then useDateTime::subsec_nanosecond.This overrides any previous subsecond nanosecond settings.
Errors
This returns an error when
DateTimeWith::buildis called if the given subsecond nanosecond is outside the range0..=999,999,999, or if both this and one ofDateTimeWith::millisecond,DateTimeWith::microsecondorDateTimeWith::nanosecondare set.Example
This shows the relationship between constructing a
DateTimevalue with subsecond nanoseconds and its individual subsecond fields:use time; let dt1 = time.on; let dt2 = dt1.with.subsec_nanosecond.build?; assert_eq!; assert_eq!; assert_eq!; # Ok::
impl Clone for DateTimeWith
fn clone(self: &Self) -> DateTimeWith
impl Copy for DateTimeWith
impl Debug for DateTimeWith
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Freeze for DateTimeWith
impl RefUnwindSafe for DateTimeWith
impl Send for DateTimeWith
impl Sync for DateTimeWith
impl Unpin for DateTimeWith
impl UnsafeUnpin for DateTimeWith
impl UnwindSafe for DateTimeWith
impl<T> Any for DateTimeWith
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for DateTimeWith
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for DateTimeWith
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for DateTimeWith
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for DateTimeWith
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for DateTimeWith
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T, U> Into for DateTimeWith
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 DateTimeWith
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for DateTimeWith
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>