Struct DateTime
struct DateTime<Tz: TimeZone> { ... }
ISO 8601 combined date and time with time zone.
There are some constructors implemented here (the from_* methods), but
the general-purpose constructors are all via the methods on the
TimeZone implementations.
Implementations
impl DateTime<FixedOffset>
fn parse_from_rfc2822(s: &str) -> ParseResult<DateTime<FixedOffset>>Parses an RFC 2822 date-and-time string into a
DateTime<FixedOffset>value.This parses valid RFC 2822 datetime strings (such as
Tue, 1 Jul 2003 10:52:37 +0200) and returns a newDateTimeinstance with the parsed timezone as theFixedOffset.RFC 2822 is the internet message standard that specifies the representation of times in HTTP and email headers. It is the 2001 revision of RFC 822, and is itself revised as RFC 5322 in 2008.
Support for the obsolete date format
- A 2-digit year is interpreted to be a year in 1950-2049.
- The standard allows comments and whitespace between many of the tokens. See 4.3 and Appendix A.5
- Single letter 'military' time zone names are parsed as a
-0000offset. They were defined with the wrong sign in RFC 822 and corrected in RFC 2822. But because the meaning is now ambiguous, the standard says they should be be considered as-0000unless there is out-of-band information confirming their meaning. The exception isZ, which remains identical to+0000.
Example
# use ; assert_eq!;fn parse_from_rfc3339(s: &str) -> ParseResult<DateTime<FixedOffset>>Parses an RFC 3339 date-and-time string into a
DateTime<FixedOffset>value.Parses all valid RFC 3339 values (as well as the subset of valid ISO 8601 values that are also valid RFC 3339 date-and-time values) and returns a new
DateTimewith aFixedOffsetcorresponding to the parsed timezone. While RFC 3339 values come in a wide variety of shapes and sizes,1996-12-19T16:39:57-08:00is an example of the most commonly encountered variety of RFC 3339 formats.Why isn't this named
parse_from_iso8601? That's because ISO 8601 allows representing values in a wide range of formats, only some of which represent actual date-and-time instances (rather than periods, ranges, dates, or times). Some valid ISO 8601 values are also simultaneously valid RFC 3339 values, but not all RFC 3339 values are valid ISO 8601 values (or the other way around).fn parse_from_str(s: &str, fmt: &str) -> ParseResult<DateTime<FixedOffset>>Parses a string from a user-specified format into a
DateTime<FixedOffset>value.Note that this method requires a timezone in the input string. See
NaiveDateTime::parse_from_strfor a version that does not require a timezone in the to-be-parsed str. The returnedDateTimevalue will have aFixedOffsetreflecting the parsed timezone.See the
format::strftimemodule for supported format sequences.Example
use ; let dt = parse_from_str; assert_eq!;fn parse_and_remainder<'a>(s: &'a str, fmt: &str) -> ParseResult<(DateTime<FixedOffset>, &'a str)>Parses a string from a user-specified format into a
DateTime<FixedOffset>value, and a slice with the remaining portion of the string.Note that this method requires a timezone in the input string. See
NaiveDateTime::parse_and_remainderfor a version that does not require a timezone ins. The returnedDateTimevalue will have aFixedOffsetreflecting the parsed timezone.See the
format::strftimemodule for supported format sequences.Similar to
parse_from_str.Example
# use ; let = parse_and_remainder .unwrap; assert_eq!; assert_eq!;
impl DateTime<Utc>
const fn from_timestamp(secs: i64, nsecs: u32) -> Option<Self>Makes a new
DateTime<Utc>from the number of non-leap seconds since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp") and the number of nanoseconds since the last whole non-leap second.This is guaranteed to round-trip with regard to
timestampandtimestamp_subsec_nanos.If you need to create a
DateTimewith aTimeZonedifferent fromUtc, useTimeZone::timestamp_optorDateTime::with_timezone.The nanosecond part can exceed 1,000,000,000 in order to represent a leap second, but only when
secs % 60 == 59. (The true "UNIX timestamp" cannot represent a leap second unambiguously.)Errors
Returns
Noneon out-of-range number of seconds and/or invalid nanosecond, otherwise returnsSome(DateTime {...}).Example
use DateTime; let dt = from_timestamp.expect; assert_eq!; assert_eq!;const fn from_timestamp_millis(millis: i64) -> Option<Self>Makes a new
DateTime<Utc>from the number of non-leap milliseconds since January 1, 1970 0:00:00.000 UTC (aka "UNIX timestamp").This is guaranteed to round-trip with
timestamp_millis.If you need to create a
DateTimewith aTimeZonedifferent fromUtc, useTimeZone::timestamp_millis_optorDateTime::with_timezone.Errors
Returns
Noneon out-of-range number of milliseconds, otherwise returnsSome(DateTime {...}).Example
use DateTime; let dt = from_timestamp_millis.expect; assert_eq!; assert_eq!;const fn from_timestamp_micros(micros: i64) -> Option<Self>Creates a new
DateTime<Utc>from the number of non-leap microseconds since January 1, 1970 0:00:00.000 UTC (aka "UNIX timestamp").This is guaranteed to round-trip with
timestamp_micros.If you need to create a
DateTimewith aTimeZonedifferent fromUtc, useTimeZone::timestamp_microsorDateTime::with_timezone.Errors
Returns
Noneif the number of microseconds would be out of range for aNaiveDateTime(more than ca. 262,000 years away from common era)Example
use DateTime; let timestamp_micros: i64 = 1662921288000000; // Sun, 11 Sep 2022 18:34:48 UTC let dt = from_timestamp_micros; assert!; assert_eq!; // Negative timestamps (before the UNIX epoch) are supported as well. let timestamp_micros: i64 = -2208936075000000; // Mon, 1 Jan 1900 14:38:45 UTC let dt = from_timestamp_micros; assert!; assert_eq!;const fn from_timestamp_nanos(nanos: i64) -> SelfCreates a new [
DateTime<Utc>] from the number of non-leap nanoseconds since January 1, 1970 0:00:00.000 UTC (aka "UNIX timestamp").This is guaranteed to round-trip with
timestamp_nanos.If you need to create a
DateTimewith aTimeZonedifferent fromUtc, useTimeZone::timestamp_nanosorDateTime::with_timezone.The UNIX epoch starts on midnight, January 1, 1970, UTC.
An
i64with nanosecond precision can span a range of ~584 years. Because all values can be represented as aDateTimethis method never fails.Example
use DateTime; let timestamp_nanos: i64 = 1662921288_000_000_000; // Sun, 11 Sep 2022 18:34:48 UTC let dt = from_timestamp_nanos; assert_eq!; // Negative timestamps (before the UNIX epoch) are supported as well. let timestamp_nanos: i64 = -2208936075_000_000_000; // Mon, 1 Jan 1900 14:38:45 UTC let dt = from_timestamp_nanos; assert_eq!;
impl<Tz: TimeZone> DateTime<Tz>
fn format_with_items<'a, I, B>(self: &Self, items: I) -> DelayedFormat<I> where I: Iterator<Item = B> + Clone, B: Borrow<Item<'a>>Formats the combined date and time with the specified formatting items.
fn format<'a>(self: &Self, fmt: &'a str) -> DelayedFormat<StrftimeItems<'a>>Formats the combined date and time per the specified format string.
See the
crate::format::strftimemodule for the supported escape sequences.Example
use *; let date_time: = Utc.with_ymd_and_hms.unwrap; let formatted = format!; assert_eq!;
impl<Tz: TimeZone> DateTime<Tz>
const fn from_naive_utc_and_offset(datetime: NaiveDateTime, offset: <Tz as >::Offset) -> DateTime<Tz>Makes a new
DateTimefrom its components: aNaiveDateTimein UTC and anOffset.This is a low-level method, intended for use cases such as deserializing a
DateTimeor passing it through FFI.For regular use you will probably want to use a method such as
TimeZone::from_local_datetimeorNaiveDateTime::and_local_timezoneinstead.Example
#fn from_utc(datetime: NaiveDateTime, offset: <Tz as >::Offset) -> DateTime<Tz>Makes a new
DateTimefrom its components: aNaiveDateTimein UTC and anOffset.fn from_local(datetime: NaiveDateTime, offset: <Tz as >::Offset) -> DateTime<Tz>Makes a new
DateTimefrom aNaiveDateTimein local time and anOffset.Panics
Panics if the local datetime can't be converted to UTC because it would be out of range.
This can happen if
datetimeis near the end of the representable range ofNaiveDateTime, and the offset from UTC pushes it beyond that.fn date(self: &Self) -> Date<Tz>Retrieves the date component with an associated timezone.
Unless you are immediately planning on turning this into a
DateTimewith the same timezone you should use thedate_naivemethod.NaiveDateis a more well-defined type, and has more traits implemented on it, so should be preferred toDateany time you truly want to operate on dates.Panics
DateTimeinternally stores the date and time in UTC with aNaiveDateTime. This method will panic if the offset from UTC would push the local date outside of the representable range of aDate.fn date_naive(self: &Self) -> NaiveDateRetrieves the date component.
Panics
DateTimeinternally stores the date and time in UTC with aNaiveDateTime. This method will panic if the offset from UTC would push the local date outside of the representable range of aNaiveDate.Example
use *; let date: = Utc.with_ymd_and_hms.unwrap; let other: = east_opt.unwrap.with_ymd_and_hms.unwrap; assert_eq!;fn time(self: &Self) -> NaiveTimeRetrieves the time component.
const fn timestamp(self: &Self) -> i64Returns the number of non-leap seconds since January 1, 1970 0:00:00 UTC (aka "UNIX timestamp").
The reverse operation of creating a
DateTimefrom a timestamp can be performed usingfrom_timestamporTimeZone::timestamp_opt.use ; let dt: = Utc.with_ymd_and_hms.unwrap; assert_eq!; assert_eq!;const fn timestamp_millis(self: &Self) -> i64Returns the number of non-leap-milliseconds since January 1, 1970 UTC.
Example
use ; let dt = from_ymd_opt .unwrap .and_hms_milli_opt .unwrap .and_local_timezone .unwrap; assert_eq!; let dt = from_ymd_opt .unwrap .and_hms_milli_opt .unwrap .and_local_timezone .unwrap; assert_eq!;const fn timestamp_micros(self: &Self) -> i64Returns the number of non-leap-microseconds since January 1, 1970 UTC.
Example
use ; let dt = from_ymd_opt .unwrap .and_hms_micro_opt .unwrap .and_local_timezone .unwrap; assert_eq!; let dt = from_ymd_opt .unwrap .and_hms_micro_opt .unwrap .and_local_timezone .unwrap; assert_eq!;const fn timestamp_nanos(self: &Self) -> i64Returns the number of non-leap-nanoseconds since January 1, 1970 UTC.
Panics
An
i64with nanosecond precision can span a range of ~584 years. This function panics on an out of rangeDateTime.The dates that can be represented as nanoseconds are between 1677-09-21T00:12:43.145224192 and 2262-04-11T23:47:16.854775807.
const fn timestamp_nanos_opt(self: &Self) -> Option<i64>Returns the number of non-leap-nanoseconds since January 1, 1970 UTC.
Errors
An
i64with nanosecond precision can span a range of ~584 years. This function returnsNoneon an out of rangeDateTime.The dates that can be represented as nanoseconds are between 1677-09-21T00:12:43.145224192 and 2262-04-11T23:47:16.854775807.
Example
use ; let dt = from_ymd_opt .unwrap .and_hms_nano_opt .unwrap .and_local_timezone .unwrap; assert_eq!; let dt = from_ymd_opt .unwrap .and_hms_nano_opt .unwrap .and_local_timezone .unwrap; assert_eq!; let dt = from_ymd_opt .unwrap .and_hms_nano_opt .unwrap .and_local_timezone .unwrap; assert_eq!; let dt = from_ymd_opt .unwrap .and_hms_nano_opt .unwrap .and_local_timezone .unwrap; assert_eq!; let dt = from_ymd_opt .unwrap .and_hms_nano_opt .unwrap .and_local_timezone .unwrap; assert_eq!; let dt = from_ymd_opt .unwrap .and_hms_nano_opt .unwrap .and_local_timezone .unwrap; assert_eq!;const fn timestamp_subsec_millis(self: &Self) -> u32Returns the number of milliseconds since the last second boundary.
In event of a leap second this may exceed 999.
const fn timestamp_subsec_micros(self: &Self) -> u32Returns the number of microseconds since the last second boundary.
In event of a leap second this may exceed 999,999.
const fn timestamp_subsec_nanos(self: &Self) -> u32Returns the number of nanoseconds since the last second boundary
In event of a leap second this may exceed 999,999,999.
const fn offset(self: &Self) -> &<Tz as >::OffsetRetrieves an associated offset from UTC.
fn timezone(self: &Self) -> TzRetrieves an associated time zone.
fn with_timezone<Tz2: TimeZone>(self: &Self, tz: &Tz2) -> DateTime<Tz2>Changes the associated time zone. The returned
DateTimereferences the same instant of time from the perspective of the provided time zone.fn fixed_offset(self: &Self) -> DateTime<FixedOffset>Fix the offset from UTC to its current value, dropping the associated timezone information. This it useful for converting a generic
DateTime<Tz: Timezone>toDateTime<FixedOffset>.const fn to_utc(self: &Self) -> DateTime<Utc>Turn this
DateTimeinto aDateTime<Utc>, dropping the offset and associated timezone information.fn checked_add_signed(self: Self, rhs: TimeDelta) -> Option<DateTime<Tz>>Adds given
TimeDeltato the current date and time.Errors
Returns
Noneif the resulting date would be out of range.fn checked_add_months(self: Self, months: Months) -> Option<DateTime<Tz>>Adds given
Monthsto the current date and time.Uses the last day of the month if the day does not exist in the resulting month.
See
NaiveDate::checked_add_monthsfor more details on behavior.Errors
Returns
Noneif:- The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The resulting UTC datetime would be out of range.
- The resulting local datetime would be out of range (unless
monthsis zero).
fn checked_sub_signed(self: Self, rhs: TimeDelta) -> Option<DateTime<Tz>>Subtracts given
TimeDeltafrom the current date and time.Errors
Returns
Noneif the resulting date would be out of range.fn checked_sub_months(self: Self, months: Months) -> Option<DateTime<Tz>>Subtracts given
Monthsfrom the current date and time.Uses the last day of the month if the day does not exist in the resulting month.
See
NaiveDate::checked_sub_monthsfor more details on behavior.Errors
Returns
Noneif:- The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The resulting UTC datetime would be out of range.
- The resulting local datetime would be out of range (unless
monthsis zero).
fn checked_add_days(self: Self, days: Days) -> Option<Self>Add a duration in
Daysto the date part of theDateTime.Errors
Returns
Noneif:- The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The resulting UTC datetime would be out of range.
- The resulting local datetime would be out of range (unless
daysis zero).
fn checked_sub_days(self: Self, days: Days) -> Option<Self>Subtract a duration in
Daysfrom the date part of theDateTime.Errors
Returns
Noneif:- The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The resulting UTC datetime would be out of range.
- The resulting local datetime would be out of range (unless
daysis zero).
fn signed_duration_since<Tz2: TimeZone, impl Borrow<DateTime<Tz2>>: Borrow<DateTime<Tz2>>>(self: Self, rhs: impl Borrow<DateTime<Tz2>>) -> TimeDeltaSubtracts another
DateTimefrom the current date and time. This does not overflow or underflow at all.const fn naive_utc(self: &Self) -> NaiveDateTimeReturns a view to the naive UTC datetime.
fn naive_local(self: &Self) -> NaiveDateTimeReturns a view to the naive local datetime.
Panics
DateTimeinternally stores the date and time in UTC with aNaiveDateTime. This method will panic if the offset from UTC would push the local datetime outside of the representable range of aNaiveDateTime.fn years_since(self: &Self, base: Self) -> Option<u32>Retrieve the elapsed years from now to the given
DateTime.Errors
Returns
Noneifbase > self.fn to_rfc2822(self: &Self) -> StringReturns an RFC 2822 date and time string such as
Tue, 1 Jul 2003 10:52:37 +0200.Panics
Panics if the date can not be represented in this format: the year may not be negative and can not have more than 4 digits.
fn to_rfc3339(self: &Self) -> StringReturns an RFC 3339 and ISO 8601 date and time string such as
1996-12-19T16:39:57-08:00.fn to_rfc3339_opts(self: &Self, secform: SecondsFormat, use_z: bool) -> StringReturn an RFC 3339 and ISO 8601 date and time string with subseconds formatted as per
SecondsFormat.If
use_zis true and the timezone is UTC (offset 0), usesZas perFixed::TimezoneOffsetColonZ. Ifuse_zis false, usesFixed::TimezoneOffsetColonExamples
# use ; let dt = from_ymd_opt .unwrap .and_hms_micro_opt .unwrap .and_utc; assert_eq!; assert_eq!; assert_eq!; let pst = east_opt.unwrap; let dt = pst .from_local_datetime .unwrap; assert_eq!;fn with_time(self: &Self, time: NaiveTime) -> LocalResult<Self>Set the time to a new fixed time on the existing date.
Errors
Returns
LocalResult::Noneif the datetime is at the edge of the representable range for aDateTime, andwith_timewould push the value in UTC out of range.Example
#
impl Default for DateTime<FixedOffset>
fn default() -> Self
impl Default for DateTime<Local>
fn default() -> Self
impl Default for DateTime<Utc>
fn default() -> Self
impl From for DateTime<FixedOffset>
fn from(src: DateTime<Utc>) -> SelfConvert this
DateTime<Utc>instance into aDateTime<FixedOffset>instance.Conversion is done via
DateTime::with_timezone. Note that the converted value returned by this will be created with a fixed timezone offset of 0.
impl From for DateTime<FixedOffset>
fn from(src: DateTime<Local>) -> SelfConvert this
DateTime<Local>instance into aDateTime<FixedOffset>instance.Conversion is performed via
DateTime::with_timezone.
impl From for DateTime<Local>
fn from(src: DateTime<Utc>) -> SelfConvert this
DateTime<Utc>instance into aDateTime<Local>instance.Conversion is performed via
DateTime::with_timezone, accounting for the difference in timezones.
impl From for DateTime<Local>
fn from(t: SystemTime) -> DateTime<Local>
impl From for DateTime<Local>
fn from(src: DateTime<FixedOffset>) -> SelfConvert this
DateTime<FixedOffset>instance into aDateTime<Local>instance.Conversion is performed via
DateTime::with_timezone. Returns the equivalent value in local time.
impl From for DateTime<Utc>
fn from(src: DateTime<Local>) -> SelfConvert this
DateTime<Local>instance into aDateTime<Utc>instance.Conversion is performed via
DateTime::with_timezone, accounting for the difference in timezones.
impl From for DateTime<Utc>
fn from(src: DateTime<FixedOffset>) -> SelfConvert this
DateTime<FixedOffset>instance into aDateTime<Utc>instance.Conversion is performed via
DateTime::with_timezone, accounting for the timezone difference.
impl From for DateTime<Utc>
fn from(t: SystemTime) -> DateTime<Utc>
impl FromStr for DateTime<FixedOffset>
fn from_str(s: &str) -> ParseResult<DateTime<FixedOffset>>
impl FromStr for DateTime<Local>
fn from_str(s: &str) -> ParseResult<DateTime<Local>>
impl FromStr for DateTime<Utc>
fn from_str(s: &str) -> ParseResult<DateTime<Utc>>
impl<T> Any for DateTime<Tz>
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for DateTime<Tz>
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for DateTime<Tz>
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for DateTime<Tz>
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for DateTime<Tz>
fn from(t: T) -> TReturns the argument unchanged.
impl<T> SubsecRound for DateTime<Tz>
fn round_subsecs(self: Self, digits: u16) -> Tfn trunc_subsecs(self: Self, digits: u16) -> T
impl<T> ToOwned for DateTime<Tz>
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> ToString for DateTime<Tz>
fn to_string(self: &Self) -> String
impl<T, U> Into for DateTime<Tz>
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 DateTime<Tz>
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for DateTime<Tz>
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>
impl<Tz> Freeze for DateTime<Tz>
impl<Tz> RefUnwindSafe for DateTime<Tz>
impl<Tz> Send for DateTime<Tz>
impl<Tz> Sync for DateTime<Tz>
impl<Tz> Unpin for DateTime<Tz>
impl<Tz> UnsafeUnpin for DateTime<Tz>
impl<Tz> UnwindSafe for DateTime<Tz>
impl<Tz: $crate::clone::Clone + TimeZone> Clone for DateTime<Tz>
fn clone(self: &Self) -> DateTime<Tz>
impl<Tz: TimeZone> Add for DateTime<Tz>
fn add(self: Self, rhs: Months) -> <Self as >::Output
impl<Tz: TimeZone> Add for DateTime<Tz>
fn add(self: Self, days: Days) -> <Self as >::Output
impl<Tz: TimeZone> Add for DateTime<Tz>
fn add(self: Self, rhs: Duration) -> DateTime<Tz>
impl<Tz: TimeZone> Add for DateTime<Tz>
fn add(self: Self, rhs: FixedOffset) -> DateTime<Tz>
impl<Tz: TimeZone> Add for DateTime<Tz>
fn add(self: Self, rhs: TimeDelta) -> DateTime<Tz>
impl<Tz: TimeZone> AddAssign for DateTime<Tz>
fn add_assign(self: &mut Self, rhs: Duration)
impl<Tz: TimeZone> AddAssign for DateTime<Tz>
fn add_assign(self: &mut Self, rhs: TimeDelta)
impl<Tz: TimeZone> Copy for DateTime<Tz>
impl<Tz: TimeZone> Datelike for DateTime<Tz>
fn year(self: &Self) -> i32fn month(self: &Self) -> u32fn month0(self: &Self) -> u32fn day(self: &Self) -> u32fn day0(self: &Self) -> u32fn ordinal(self: &Self) -> u32fn ordinal0(self: &Self) -> u32fn weekday(self: &Self) -> Weekdayfn iso_week(self: &Self) -> IsoWeekfn with_year(self: &Self, year: i32) -> Option<DateTime<Tz>>Makes a new
DateTimewith the year number changed, while keeping the same month and day.See also the
NaiveDate::with_yearmethod.Errors
Returns
Noneif:- The resulting date does not exist (February 29 in a non-leap year).
- The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The resulting UTC datetime would be out of range.
- The resulting local datetime would be out of range (unless the year remains the same).
fn with_month(self: &Self, month: u32) -> Option<DateTime<Tz>>Makes a new
DateTimewith the month number (starting from 1) changed.Don't combine multiple
Datelike::with_*methods. The intermediate value may not exist.See also the
NaiveDate::with_monthmethod.Errors
Returns
Noneif:- The resulting date does not exist (for example
month(4)when day of the month is 31). - The value for
monthis invalid. - The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The resulting date does not exist (for example
fn with_month0(self: &Self, month0: u32) -> Option<DateTime<Tz>>Makes a new
DateTimewith the month number (starting from 0) changed.See also the
NaiveDate::with_month0method.Errors
Returns
Noneif:- The resulting date does not exist (for example
month0(3)when day of the month is 31). - The value for
month0is invalid. - The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The resulting date does not exist (for example
fn with_day(self: &Self, day: u32) -> Option<DateTime<Tz>>Makes a new
DateTimewith the day of month (starting from 1) changed.See also the
NaiveDate::with_daymethod.Errors
Returns
Noneif:- The resulting date does not exist (for example
day(31)in April). - The value for
dayis invalid. - The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The resulting date does not exist (for example
fn with_day0(self: &Self, day0: u32) -> Option<DateTime<Tz>>Makes a new
DateTimewith the day of month (starting from 0) changed.See also the
NaiveDate::with_day0method.Errors
Returns
Noneif:- The resulting date does not exist (for example
day(30)in April). - The value for
day0is invalid. - The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The resulting date does not exist (for example
fn with_ordinal(self: &Self, ordinal: u32) -> Option<DateTime<Tz>>Makes a new
DateTimewith the day of year (starting from 1) changed.See also the
NaiveDate::with_ordinalmethod.Errors
Returns
Noneif:- The resulting date does not exist (
with_ordinal(366)in a non-leap year). - The value for
ordinalis invalid. - The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The resulting date does not exist (
fn with_ordinal0(self: &Self, ordinal0: u32) -> Option<DateTime<Tz>>Makes a new
DateTimewith the day of year (starting from 0) changed.See also the
NaiveDate::with_ordinal0method.Errors
Returns
Noneif:- The resulting date does not exist (
with_ordinal0(365)in a non-leap year). - The value for
ordinal0is invalid. - The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The resulting date does not exist (
impl<Tz: TimeZone> Debug for DateTime<Tz>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<Tz: TimeZone> Display for DateTime<Tz>
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl<Tz: TimeZone> DurationRound for DateTime<Tz>
fn duration_round(self: Self, duration: TimeDelta) -> Result<Self, <Self as >::Err>fn duration_trunc(self: Self, duration: TimeDelta) -> Result<Self, <Self as >::Err>
impl<Tz: TimeZone> Eq for DateTime<Tz>
impl<Tz: TimeZone> Hash for DateTime<Tz>
fn hash<H: hash::Hasher>(self: &Self, state: &mut H)
impl<Tz: TimeZone> Ord for DateTime<Tz>
fn cmp(self: &Self, other: &DateTime<Tz>) -> Ordering
impl<Tz: TimeZone> Sub for DateTime<Tz>
fn sub(self: Self, rhs: Months) -> <Self as >::Output
impl<Tz: TimeZone> Sub for DateTime<Tz>
fn sub(self: Self, rhs: &DateTime<Tz>) -> TimeDelta
impl<Tz: TimeZone> Sub for DateTime<Tz>
fn sub(self: Self, rhs: Duration) -> DateTime<Tz>
impl<Tz: TimeZone> Sub for DateTime<Tz>
fn sub(self: Self, rhs: TimeDelta) -> DateTime<Tz>
impl<Tz: TimeZone> Sub for DateTime<Tz>
fn sub(self: Self, rhs: FixedOffset) -> DateTime<Tz>
impl<Tz: TimeZone> Sub for DateTime<Tz>
fn sub(self: Self, days: Days) -> <Self as >::Output
impl<Tz: TimeZone> Sub for DateTime<Tz>
fn sub(self: Self, rhs: DateTime<Tz>) -> TimeDelta
impl<Tz: TimeZone> SubAssign for DateTime<Tz>
fn sub_assign(self: &mut Self, rhs: Duration)
impl<Tz: TimeZone> SubAssign for DateTime<Tz>
fn sub_assign(self: &mut Self, rhs: TimeDelta)
impl<Tz: TimeZone> Timelike for DateTime<Tz>
fn hour(self: &Self) -> u32fn minute(self: &Self) -> u32fn second(self: &Self) -> u32fn nanosecond(self: &Self) -> u32fn with_hour(self: &Self, hour: u32) -> Option<DateTime<Tz>>Makes a new
DateTimewith the hour number changed.See also the
NaiveTime::with_hourmethod.Errors
Returns
Noneif:- The value for
houris invalid. - The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The value for
fn with_minute(self: &Self, min: u32) -> Option<DateTime<Tz>>Makes a new
DateTimewith the minute number changed.See also the
NaiveTime::with_minutemethod.Errors
- The value for
minuteis invalid. - The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The value for
fn with_second(self: &Self, sec: u32) -> Option<DateTime<Tz>>Makes a new
DateTimewith the second number changed.As with the
secondmethod, the input range is restricted to 0 through 59.See also the
NaiveTime::with_secondmethod.Errors
Returns
Noneif:- The value for
secondis invalid. - The local time at the resulting date does not exist or is ambiguous, for example during a daylight saving time transition.
- The value for
fn with_nanosecond(self: &Self, nano: u32) -> Option<DateTime<Tz>>Makes a new
DateTimewith nanoseconds since the whole non-leap second changed.Returns
Nonewhen the resultingNaiveDateTimewould be invalid. As with theNaiveDateTime::nanosecondmethod, the input range can exceed 1,000,000,000 for leap seconds.See also the
NaiveTime::with_nanosecondmethod.Errors
Returns
Noneifnanosecond >= 2,000,000,000.
impl<Tz: TimeZone, Tz2: TimeZone> PartialEq for DateTime<Tz>
fn eq(self: &Self, other: &DateTime<Tz2>) -> bool
impl<Tz: TimeZone, Tz2: TimeZone> PartialOrd for DateTime<Tz>
fn partial_cmp(self: &Self, other: &DateTime<Tz2>) -> Option<Ordering>Compare two DateTimes based on their true time, ignoring time zones
Example
use *; let earlier = Utc .with_ymd_and_hms .unwrap .with_timezone; let later = Utc .with_ymd_and_hms .unwrap .with_timezone; assert_eq!; assert_eq!; assert!;