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
impl DateTime
Sourcepub const UNIX_EPOCH: DateTime
pub const UNIX_EPOCH: DateTime
1970-01-01T00:00:00 UTC.
Sourcepub fn new(year: i32, month: i32, day: i32) -> DateTime
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.
Sourcepub fn new_with_time(
year: i32,
month: i32,
day: i32,
hour: i32,
minute: i32,
second: i32,
) -> DateTime
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.
Sourcepub fn new_with_millisecond(
year: i32,
month: i32,
day: i32,
hour: i32,
minute: i32,
second: i32,
millisecond: i32,
) -> DateTime
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.
Sourcepub fn try_new(year: i32, month: i32, day: i32) -> Option<DateTime>
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.
Sourcepub fn try_new_with_time(
year: i32,
month: i32,
day: i32,
hour: i32,
minute: i32,
second: i32,
) -> Option<DateTime>
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.
Sourcepub const fn from_ticks(ticks: i64) -> DateTime
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.
Sourcepub const fn from_ticks_kind(ticks: i64, kind: DateTimeKind) -> DateTime
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.
Sourcepub const fn try_from_ticks(ticks: i64, kind: DateTimeKind) -> Option<DateTime>
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.
Sourcepub const fn specify_kind(self, kind: DateTimeKind) -> DateTime
pub const fn specify_kind(self, kind: DateTimeKind) -> DateTime
The same ticks with another kind (.NET DateTime.SpecifyKind).
Sourcepub fn set_utc_now_provider(
provider: Option<UtcNowProvider>,
) -> Option<UtcNowProvider>
pub fn set_utc_now_provider( provider: Option<UtcNowProvider>, ) -> Option<UtcNowProvider>
Sourcepub fn utc_now() -> DateTime
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.
Sourcepub fn now() -> DateTime
pub fn now() -> DateTime
The current date and time in local time (kind DateTimeKind::Local).
Sourcepub fn today() -> DateTime
pub fn today() -> DateTime
The current local date at midnight (kind DateTimeKind::Local).
Sourcepub const fn kind(self) -> DateTimeKind
pub const fn kind(self) -> DateTimeKind
The kind of time.
Sourcepub const fn time_of_day(self) -> TimeSpan
pub const fn time_of_day(self) -> TimeSpan
The time elapsed since midnight.
Sourcepub fn day_of_year(self) -> i32
pub fn day_of_year(self) -> i32
The day of the year, 1..=366.
Sourcepub fn day_of_week(self) -> DayOfWeek
pub fn day_of_week(self) -> DayOfWeek
The day of the week.
Sourcepub const fn millisecond(self) -> i32
pub const fn millisecond(self) -> i32
The millisecond, 0..=999.
Sourcepub fn is_leap_year(year: i32) -> bool
pub fn is_leap_year(year: i32) -> bool
Whether year (1..=9999) is a leap year. Panics for other years.
Sourcepub fn days_in_month(year: i32, month: i32) -> i32
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.
Sourcepub const fn try_add_ticks(self, ticks: i64) -> Option<DateTime>
pub const fn try_add_ticks(self, ticks: i64) -> Option<DateTime>
The value ticks later; None when out of range.
Sourcepub fn add_ticks(self, ticks: i64) -> DateTime
pub fn add_ticks(self, ticks: i64) -> DateTime
The value ticks later. Panics when out of range.
Sourcepub fn try_add(self, value: TimeSpan) -> Option<DateTime>
pub fn try_add(self, value: TimeSpan) -> Option<DateTime>
The value value later; None when out of range.
Sourcepub fn subtract(self, value: TimeSpan) -> DateTime
pub fn subtract(self, value: TimeSpan) -> DateTime
The value value earlier. Panics when out of range.
Sourcepub fn subtract_date_time(self, other: DateTime) -> TimeSpan
pub fn subtract_date_time(self, other: DateTime) -> TimeSpan
The interval from other to this value.
Sourcepub fn try_add_days(self, value: f64) -> Option<DateTime>
pub fn try_add_days(self, value: f64) -> Option<DateTime>
The value value (whole and fractional) days later; None when out of range.
Sourcepub fn add_days(self, value: f64) -> DateTime
pub fn add_days(self, value: f64) -> DateTime
The value value (whole and fractional) days later. Panics when out of range.
Sourcepub fn try_add_months(self, months: i32) -> Option<DateTime>
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.
Sourcepub fn add_months(self, months: i32) -> DateTime
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.
Sourcepub fn try_add_years(self, years: i32) -> Option<DateTime>
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.
Sourcepub fn add_years(self, years: i32) -> DateTime
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.
Sourcepub fn compare(t1: DateTime, t2: DateTime) -> i32
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).
Sourcepub fn compare_to(self, other: DateTime) -> i32
pub fn compare_to(self, other: DateTime) -> i32
Compares this value with other: -1, 0 or 1.
Sourcepub fn to_universal_time(self) -> DateTime
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.
Sourcepub fn to_local_time(self) -> DateTime
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.
Sourcepub fn to_string_format(
self,
format: &str,
provider: &(impl DateTimeFormatProvider + ?Sized),
) -> String
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).
Sourcepub fn try_to_string_format(
self,
format: &str,
provider: &(impl DateTimeFormatProvider + ?Sized),
) -> Result<String, FormatError>
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.
Sourcepub fn to_string_with(self, format: &str) -> String
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.
Sourcepub fn to_string_provider(
self,
provider: &(impl DateTimeFormatProvider + ?Sized),
) -> String
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)).
Sourcepub fn parse(
text: &str,
provider: &(impl DateTimeFormatProvider + ?Sized),
) -> Result<DateTime, FormatError>
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 signedhh:mm,hhmmorh[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]withZor 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.
Sourcepub fn parse_with_styles(
text: &str,
provider: &(impl DateTimeFormatProvider + ?Sized),
styles: DateTimeStyles,
) -> Result<DateTime, FormatError>
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)).
Sourcepub fn try_parse(
text: &str,
provider: &(impl DateTimeFormatProvider + ?Sized),
styles: DateTimeStyles,
) -> Option<DateTime>
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).
Sourcepub fn parse_exact(
text: &str,
format: &str,
provider: &(impl DateTimeFormatProvider + ?Sized),
) -> Result<DateTime, FormatError>
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)).
Sourcepub fn parse_exact_with_styles(
text: &str,
format: &str,
provider: &(impl DateTimeFormatProvider + ?Sized),
styles: DateTimeStyles,
) -> Result<DateTime, FormatError>
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)).
Sourcepub fn parse_exact_multiple(
text: &str,
formats: &[&str],
provider: &(impl DateTimeFormatProvider + ?Sized),
styles: DateTimeStyles,
) -> Result<DateTime, FormatError>
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[], ..)).
Sourcepub fn try_parse_exact(
text: &str,
format: &str,
provider: &(impl DateTimeFormatProvider + ?Sized),
styles: DateTimeStyles,
) -> Option<DateTime>
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, ..)).
Sourcepub fn try_parse_exact_multiple(
text: &str,
formats: &[&str],
provider: &(impl DateTimeFormatProvider + ?Sized),
styles: DateTimeStyles,
) -> Option<DateTime>
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 AddAssign<TimeSpan> for DateTime
impl AddAssign<TimeSpan> for DateTime
Source§fn add_assign(&mut self, rhs: TimeSpan)
fn add_assign(&mut self, rhs: TimeSpan)
+= operation. Read moreimpl Copy for DateTime
Source§impl Default for DateTime
impl Default for DateTime
Source§fn default() -> Self
fn default() -> Self
DateTime::MIN_VALUE, the C# default(DateTime).
impl Eq for DateTime
Source§impl From<DateTime> for DateTimeOffset
impl From<DateTime> for DateTimeOffset
Source§fn from(value: DateTime) -> Self
fn from(value: DateTime) -> Self
The C# implicit conversion; see DateTimeOffset::from_date_time.
Source§impl MarkupTyped for DateTime
impl MarkupTyped for DateTime
Source§const MARKUP: &'static MarkupType
const MARKUP: &'static MarkupType
Source§impl Ord for DateTime
impl Ord for DateTime
1.21.0 (const: unstable) · Source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
1.21.0 (const: unstable) · Source§fn min(self, other: Self) -> Selfwhere
Self: Sized,
fn min(self, other: Self) -> Selfwhere
Self: Sized,
Source§impl PartialOrd for DateTime
impl PartialOrd for DateTime
Auto Trait Implementations§
impl Freeze for DateTime
impl RefUnwindSafe for DateTime
impl Send for DateTime
impl Sync for DateTime
impl Unpin for DateTime
impl UnsafeUnpin for DateTime
impl UnwindSafe for DateTime
Blanket Implementations§
Source§impl<T> AnyValue for Twhere
T: PartialEq + 'static,
impl<T> AnyValue for Twhere
T: PartialEq + 'static,
fn any_value_as_any(&self) -> &(dyn Any + 'static)
fn any_value_eq(&self, other: &(dyn AnyValue + 'static)) -> bool
fn any_value_type_name(&self) -> &'static str
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ClassDefaults for Twhere
T: ?Sized,
impl<T> ClassDefaults for Twhere
T: ?Sized,
Source§fn static_constructor()
fn static_constructor()
affects_render, metadata
overrides). Read more