DateTime
struct · ferroui-base · v0.5.0
struct DateTime
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.
Members
MIN_VALUEMAX_VALUEUNIX_EPOCHnewnew_with_timenew_with_millisecondtry_newtry_new_with_timefrom_ticksfrom_ticks_kindtry_from_ticksspecify_kindset_utc_now_providerutc_nownowtodaytickskinddatetime_of_dayyearmonthdayday_of_yearday_of_weekhourminutesecondmillisecondis_leap_yeardays_in_monthtry_add_ticksadd_ticksaddtry_addsubtractsubtract_date_timetry_add_daysadd_daystry_add_monthsadd_monthstry_add_yearsadd_yearscomparecompare_toto_universal_timeto_local_timeto_string_formattry_to_string_formatto_string_withto_string_providerparseparse_with_stylestry_parseparse_exactparse_exact_with_stylesparse_exact_multipletry_parse_exacttry_parse_exact_multiple
MIN_VALUE
const MIN_VALUE: DateTime
The smallest value, 0001-01-01T00:00:00.
MAX_VALUE
const MAX_VALUE: DateTime
The largest value, 9999-12-31T23:59:59.9999999.
UNIX_EPOCH
const UNIX_EPOCH: DateTime
1970-01-01T00:00:00 UTC.
new
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.
new_with_time
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.
new_with_millisecond
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.
try_new
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.
try_new_with_time
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.
from_ticks
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.
from_ticks_kind
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.
try_from_ticks
const fn try_from_ticks(ticks: i64, kind: DateTimeKind) -> Option<DateTime>
The value with the given number of ticks; None when out of range.
specify_kind
const fn specify_kind(self: Self, kind: DateTimeKind) -> DateTime
The same ticks with another kind (.NET DateTime.SpecifyKind).
set_utc_now_provider
fn set_utc_now_provider(provider: Option<UtcNowProvider>) -> Option<UtcNowProvider>
Installs (or with None removes) the source of the current instant
of the current thread and returns the previous one. Tests use it to
pin system_time_now, now, utc_now
and today.
utc_now
fn utc_now() -> DateTime
The current date and time in UTC (kind DateTimeKind::Utc): the
instant of system_time_now as a date. An instant outside of the
years 1 to 9999 is MIN_VALUE or
MAX_VALUE.
now
fn now() -> DateTime
The current date and time in local time (kind DateTimeKind::Local).
today
fn today() -> DateTime
The current local date at midnight (kind DateTimeKind::Local).
ticks
const fn ticks(self: Self) -> i64
The number of ticks since 0001-01-01T00:00:00.
kind
const fn kind(self: Self) -> DateTimeKind
The kind of time.
date
const fn date(self: Self) -> DateTime
The date at midnight, with the same kind.
time_of_day
const fn time_of_day(self: Self) -> SignedDuration
The time elapsed since midnight.
year
fn year(self: Self) -> i32
The year, 1..=9999.
month
fn month(self: Self) -> i32
The month, 1..=12.
day
fn day(self: Self) -> i32
The day of the month, 1..=31.
day_of_year
fn day_of_year(self: Self) -> i32
The day of the year, 1..=366.
day_of_week
fn day_of_week(self: Self) -> DayOfWeek
The day of the week.
hour
const fn hour(self: Self) -> i32
The hour, 0..=23.
minute
const fn minute(self: Self) -> i32
The minute, 0..=59.
second
const fn second(self: Self) -> i32
The second, 0..=59.
millisecond
const fn millisecond(self: Self) -> i32
The millisecond, 0..=999.
is_leap_year
fn is_leap_year(year: i32) -> bool
Whether year (1..=9999) is a leap year. Panics for other years.
days_in_month
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.
try_add_ticks
const fn try_add_ticks(self: Self, ticks: i64) -> Option<DateTime>
The value ticks later; None when out of range.
add_ticks
fn add_ticks(self: Self, ticks: i64) -> DateTime
The value ticks later. Panics when out of range.
add
fn add(self: Self, value: SignedDuration) -> DateTime
The value value later. Panics when out of range.
try_add
fn try_add(self: Self, value: SignedDuration) -> Option<DateTime>
The value value later; None when out of range.
subtract
fn subtract(self: Self, value: SignedDuration) -> DateTime
The value value earlier. Panics when out of range.
subtract_date_time
fn subtract_date_time(self: Self, other: DateTime) -> SignedDuration
The interval from other to this value.
try_add_days
fn try_add_days(self: Self, value: f64) -> Option<DateTime>
The value value (whole and fractional) days later; None when out of range.
add_days
fn add_days(self: Self, value: f64) -> DateTime
The value value (whole and fractional) days later. Panics when out of range.
try_add_months
fn try_add_months(self: 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.
add_months
fn add_months(self: Self, months: i32) -> DateTime
The value months months later; see try_add_months.
Panics when months or the result is out of range.
try_add_years
fn try_add_years(self: 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.
add_years
fn add_years(self: Self, years: i32) -> DateTime
The value years years later; see try_add_years.
Panics when years or the result is out of range.
compare
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).
compare_to
fn compare_to(self: Self, other: DateTime) -> i32
Compares this value with other: -1, 0 or 1.
to_universal_time
fn to_universal_time(self: 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.
to_local_time
fn to_local_time(self: 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.
to_string_format
fn to_string_format(self: 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).
try_to_string_format
fn try_to_string_format(self: Self, format: &str, provider: &impl DateTimeFormatProvider + ?Sized) -> Result<String, FormatError>
Like to_string_format, but gives the error
of an invalid format string.
to_string_with
fn to_string_with(self: 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.
to_string_provider
fn to_string_provider(self: Self, provider: &impl DateTimeFormatProvider + ?Sized) -> String
Formats with the general format "G" and the conventions of
provider (.NET ToString(IFormatProvider)).
parse
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, and a two-number date that is only valid in the
order the culture does not use. The genitive month names of a culture
that has them (DateTimeFormatInfo::month_genitive_names) are read
beside the plain ones, as in .NET.
parse_with_styles
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)).
try_parse
fn try_parse(text: &str, provider: &impl DateTimeFormatProvider + ?Sized, styles: DateTimeStyles) -> Option<DateTime>
Parses like parse_with_styles; None on failure
(.NET DateTime.TryParse).
parse_exact
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)).
parse_exact_with_styles
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)).
parse_exact_multiple
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[], ..)).
try_parse_exact
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, ..)).
try_parse_exact_multiple
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
SendSyncFreezeUnsafeUnpinUnpinUnwindSafeRefUnwindSafeResourceKeyObjectPropertyValueAnyValueClassDefaultsBorrow<T>BorrowMut<T>CloneToUninitInto<U>From<T>TryInto<U>TryFrom<U>AnyToOwnedToStringErasedDestructorIntoEitherCloneCopyDefaultPartialEqEqPartialOrdOrdHashAdd<SignedDuration>Sub<SignedDuration>SubAddAssign<SignedDuration>SubAssign<SignedDuration>DisplayDebugFromStrTryFrom<SystemTime>TryFrom<DateTime>From<DateTime>MarkupTypedValueTypeRegistration
Availability
Extracted for aarch64-apple-darwin. Cargo features are listed in the crate index.