Skip to main content

DateTime

Struct DateTime 

Source
pub struct DateTime { /* private fields */ }
Expand description

A date and time of day with a resolution of 100 nanoseconds.

Equality, ordering and hashing consider the ticks only; the kind is ignored, as in .NET.

Implementations§

Source§

impl DateTime

Source

pub const MIN_VALUE: DateTime

The smallest value, 0001-01-01T00:00:00.

Source

pub const MAX_VALUE: DateTime

The largest value, 9999-12-31T23:59:59.9999999.

Source

pub const UNIX_EPOCH: DateTime

1970-01-01T00:00:00 UTC.

Source

pub fn new(year: i32, month: i32, day: i32) -> DateTime

The date year-month-day at midnight, of unspecified kind. Panics when the arguments do not name a date in 0001..=9999.

Source

pub fn new_with_time( year: i32, month: i32, day: i32, hour: i32, minute: i32, second: i32, ) -> DateTime

The given date and time of day, of unspecified kind. Panics when the arguments do not name a valid date and time.

Source

pub fn new_with_millisecond( year: i32, month: i32, day: i32, hour: i32, minute: i32, second: i32, millisecond: i32, ) -> DateTime

The given date and time of day with milliseconds, of unspecified kind. Panics when the arguments do not name a valid date and time.

Source

pub fn try_new(year: i32, month: i32, day: i32) -> Option<DateTime>

The date year-month-day at midnight; None when the arguments do not name a date in 0001..=9999.

Source

pub fn try_new_with_time( year: i32, month: i32, day: i32, hour: i32, minute: i32, second: i32, ) -> Option<DateTime>

The given date and time of day; None when the arguments do not name a valid date and time.

Source

pub const fn from_ticks(ticks: i64) -> DateTime

The value with the given number of ticks, of unspecified kind. Panics when ticks is outside the range of the type.

Source

pub const fn from_ticks_kind(ticks: i64, kind: DateTimeKind) -> DateTime

The value with the given number of ticks and kind. Panics when ticks is outside the range of the type.

Source

pub const fn try_from_ticks(ticks: i64, kind: DateTimeKind) -> Option<DateTime>

The value with the given number of ticks; None when out of range.

Source

pub const fn specify_kind(self, kind: DateTimeKind) -> DateTime

The same ticks with another kind (.NET DateTime.SpecifyKind).

Source

pub fn set_utc_now_provider( provider: Option<UtcNowProvider>, ) -> Option<UtcNowProvider>

Installs (or with None removes) the source of the current UTC time of the current thread and returns the previous one. Tests use it to pin now, utc_now and today.

Source

pub fn utc_now() -> DateTime

The current date and time in UTC (kind DateTimeKind::Utc): the time of the pinned source of this thread (set_utc_now_provider) when there is one, else the wall clock of the registered IRuntimePlatform, else the one of the standard runtime platform.

Source

pub fn now() -> DateTime

The current date and time in local time (kind DateTimeKind::Local).

Source

pub fn today() -> DateTime

The current local date at midnight (kind DateTimeKind::Local).

Source

pub const fn ticks(self) -> i64

The number of ticks since 0001-01-01T00:00:00.

Source

pub const fn kind(self) -> DateTimeKind

The kind of time.

Source

pub const fn date(self) -> DateTime

The date at midnight, with the same kind.

Source

pub const fn time_of_day(self) -> TimeSpan

The time elapsed since midnight.

Source

pub fn year(self) -> i32

The year, 1..=9999.

Source

pub fn month(self) -> i32

The month, 1..=12.

Source

pub fn day(self) -> i32

The day of the month, 1..=31.

Source

pub fn day_of_year(self) -> i32

The day of the year, 1..=366.

Source

pub fn day_of_week(self) -> DayOfWeek

The day of the week.

Source

pub const fn hour(self) -> i32

The hour, 0..=23.

Source

pub const fn minute(self) -> i32

The minute, 0..=59.

Source

pub const fn second(self) -> i32

The second, 0..=59.

Source

pub const fn millisecond(self) -> i32

The millisecond, 0..=999.

Source

pub fn is_leap_year(year: i32) -> bool

Whether year (1..=9999) is a leap year. Panics for other years.

Source

pub fn days_in_month(year: i32, month: i32) -> i32

The number of days of month in year. Panics when the month is not 1..=12 or the year not 1..=9999.

Source

pub const fn try_add_ticks(self, ticks: i64) -> Option<DateTime>

The value ticks later; None when out of range.

Source

pub fn add_ticks(self, ticks: i64) -> DateTime

The value ticks later. Panics when out of range.

Source

pub fn add(self, value: TimeSpan) -> DateTime

The value value later. Panics when out of range.

Source

pub fn try_add(self, value: TimeSpan) -> Option<DateTime>

The value value later; None when out of range.

Source

pub fn subtract(self, value: TimeSpan) -> DateTime

The value value earlier. Panics when out of range.

Source

pub fn subtract_date_time(self, other: DateTime) -> TimeSpan

The interval from other to this value.

Source

pub fn try_add_days(self, value: f64) -> Option<DateTime>

The value value (whole and fractional) days later; None when out of range.

Source

pub fn add_days(self, value: f64) -> DateTime

The value value (whole and fractional) days later. Panics when out of range.

Source

pub fn try_add_months(self, months: i32) -> Option<DateTime>

The value months months later (earlier when negative). The day is kept, or becomes the last day of the resulting month when that month is shorter. None when months is not in -120000..=120000 or the result is out of range.

Source

pub fn add_months(self, months: i32) -> DateTime

The value months months later; see try_add_months. Panics when months or the result is out of range.

Source

pub fn try_add_years(self, years: i32) -> Option<DateTime>

The value years years later (earlier when negative); February 29 becomes February 28 in a year that is not a leap year. None when years is not in -10000..=10000 or the result is out of range.

Source

pub fn add_years(self, years: i32) -> DateTime

The value years years later; see try_add_years. Panics when years or the result is out of range.

Source

pub fn compare(t1: DateTime, t2: DateTime) -> i32

Compares two values by their ticks: negative, zero or positive (.NET DateTime.Compare, which returns -1, 0 or 1).

Source

pub fn compare_to(self, other: DateTime) -> i32

Compares this value with other: -1, 0 or 1.

Source

pub fn to_universal_time(self) -> DateTime

The value converted to UTC. A value of unspecified kind is taken as local time. A result outside the range is clamped to it.

Source

pub fn to_local_time(self) -> DateTime

The value converted to local time. A value of unspecified kind is taken as UTC. A result outside the range is clamped to it.

Source

pub fn to_string_format( self, format: &str, provider: &(impl DateTimeFormatProvider + ?Sized), ) -> String

Formats with a standard or custom .NET date and time format string and the conventions of provider (a CultureInfo or a DateTimeFormatInfo). An empty format is the general format "G". Panics when the format string is invalid (.NET throws FormatException).

Source

pub fn try_to_string_format( self, format: &str, provider: &(impl DateTimeFormatProvider + ?Sized), ) -> Result<String, FormatError>

Like to_string_format, but gives the error of an invalid format string.

Source

pub fn to_string_with(self, format: &str) -> String

Formats with a format string and the conventions of the current culture (.NET ToString(string)). Panics when the format is invalid.

Source

pub fn to_string_provider( self, provider: &(impl DateTimeFormatProvider + ?Sized), ) -> String

Formats with the general format "G" and the conventions of provider (.NET ToString(IFormatProvider)).

Source

pub fn parse( text: &str, provider: &(impl DateTimeFormatProvider + ?Sized), ) -> Result<DateTime, FormatError>

Parses a date and time written in the conventions of provider (.NET DateTime.Parse(string, IFormatProvider)).

This is not the heuristic parser of .NET. It is checked against results recorded from .NET (date_time_net_data.rs, tables PARSE, PARSE_ROUND_TRIP and PARSE_STYLES) for the invariant culture, en, en-US and en-GB, which covers what the date picker feeds it: the text it writes itself with the short and the long date pattern of the culture, and typed dates of these forms:

  • numeric dates with /, -, . or spaces in the order of the short date pattern; a year of three or more digits written first gives year-month-day; two-digit years are 1950..=2049;
  • month names and abbreviations (of the culture or English), with the day before or after and an optional, matching day name;
  • a month and a year alone (the first of the month), a month and a day alone (the current year);
  • an offset from UTC (Z, GMT, a signed hh:mm, hhmm or h[h]) after the time or after a date without a time; the result is then the local time of that instant;
  • ISO 8601 yyyy-MM-ddTHH:mm:ss[.fffffff] with Z or an offset, the sortable, universal sortable and RFC 1123 formats;
  • a time H:mm[:ss[.fffffff]] with an optional designator before or after the date, or alone (the current date).

Everything else is rejected, and has not been compared with .NET beyond the recorded rows: other time zone names than GMT and Z (.NET rejects UTC too), era names, a two-number date that is only valid in the order the culture does not use, and the inflected (genitive) month names of cultures that have them, which .NET accepts.

Source

pub fn parse_with_styles( text: &str, provider: &(impl DateTimeFormatProvider + ?Sized), styles: DateTimeStyles, ) -> Result<DateTime, FormatError>

Parses a date and time written in the conventions of provider (.NET DateTime.Parse(string, IFormatProvider, DateTimeStyles)).

Source

pub fn try_parse( text: &str, provider: &(impl DateTimeFormatProvider + ?Sized), styles: DateTimeStyles, ) -> Option<DateTime>

Parses like parse_with_styles; None on failure (.NET DateTime.TryParse).

Source

pub fn parse_exact( text: &str, format: &str, provider: &(impl DateTimeFormatProvider + ?Sized), ) -> Result<DateTime, FormatError>

Parses a date and time that has to match format exactly (.NET DateTime.ParseExact(string, string, IFormatProvider)).

Source

pub fn parse_exact_with_styles( text: &str, format: &str, provider: &(impl DateTimeFormatProvider + ?Sized), styles: DateTimeStyles, ) -> Result<DateTime, FormatError>

Parses a date and time that has to match format exactly (.NET DateTime.ParseExact(string, string, IFormatProvider, DateTimeStyles)).

Source

pub fn parse_exact_multiple( text: &str, formats: &[&str], provider: &(impl DateTimeFormatProvider + ?Sized), styles: DateTimeStyles, ) -> Result<DateTime, FormatError>

Parses a date and time that has to match one of formats exactly; the first format that matches wins (.NET DateTime.ParseExact(string, string[], ..)).

Source

pub fn try_parse_exact( text: &str, format: &str, provider: &(impl DateTimeFormatProvider + ?Sized), styles: DateTimeStyles, ) -> Option<DateTime>

Like parse_exact_with_styles; None on failure (.NET DateTime.TryParseExact(string, string, ..)).

Source

pub fn try_parse_exact_multiple( text: &str, formats: &[&str], provider: &(impl DateTimeFormatProvider + ?Sized), styles: DateTimeStyles, ) -> Option<DateTime>

Like parse_exact_multiple; None on failure (.NET DateTime.TryParseExact(string, string[], ..)).

Trait Implementations§

Source§

impl Add<TimeSpan> for DateTime

Source§

type Output = DateTime

The resulting type after applying the + operator.
Source§

fn add(self, rhs: TimeSpan) -> DateTime

Performs the + operation. Read more
Source§

impl AddAssign<TimeSpan> for DateTime

Source§

fn add_assign(&mut self, rhs: TimeSpan)

Performs the += operation. Read more
Source§

impl Clone for DateTime

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for DateTime

Source§

impl Debug for DateTime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for DateTime

Source§

fn default() -> Self

DateTime::MIN_VALUE, the C# default(DateTime).

Source§

impl Display for DateTime

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

The general format "G" of the current culture (.NET ToString()).

Source§

impl Eq for DateTime

Source§

impl From<DateTime> for DateTimeOffset

Source§

fn from(value: DateTime) -> Self

The C# implicit conversion; see DateTimeOffset::from_date_time.

Source§

impl FromStr for DateTime

Source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses with the conventions of the current culture (.NET DateTime.Parse(string)).

Source§

type Err = FormatError

The associated error which can be returned from parsing.
Source§

impl Hash for DateTime

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl MarkupTyped for DateTime

Source§

const MARKUP: &'static MarkupType

The markup metadata of the type.
Source§

impl Ord for DateTime

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for DateTime

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl PartialOrd for DateTime

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub for DateTime

Source§

type Output = TimeSpan

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: DateTime) -> TimeSpan

Performs the - operation. Read more
Source§

impl Sub<TimeSpan> for DateTime

Source§

type Output = DateTime

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: TimeSpan) -> DateTime

Performs the - operation. Read more
Source§

impl SubAssign<TimeSpan> for DateTime

Source§

fn sub_assign(&mut self, rhs: TimeSpan)

Performs the -= operation. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> AnyValue for T
where T: PartialEq + 'static,

Source§

fn any_value_as_any(&self) -> &(dyn Any + 'static)

Source§

fn any_value_eq(&self, other: &(dyn AnyValue + 'static)) -> bool

Source§

fn any_value_type_name(&self) -> &'static str

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> ClassDefaults for T
where T: ?Sized,

Source§

fn static_constructor()

The static constructor of a type: work done once per thread, after the properties of the type are registered and before its first instance exists (class handlers, affects_render, metadata overrides). Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> PropertyValue for T
where T: Clone + PartialEq + 'static,

Source§

impl<T> ResourceKeyObject for T
where T: Eq + Hash + 'static,

Source§

fn as_any(&self) -> &(dyn Any + 'static)

Source§

fn dyn_eq(&self, other: &(dyn ResourceKeyObject + 'static)) -> bool

Source§

fn dyn_hash(&self, state: &mut dyn Hasher)

Source§

fn key_type_name(&self) -> &'static str

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.