Struct icu_datetime::datetime::DateTimeFormat [−][src]
pub struct DateTimeFormat<'d> { /* fields omitted */ }
DateTimeFormat
is the main structure of the icu_datetime
component.
When constructed, it uses data from the DataProvider
, selected Locale
and provided options to
collect all data necessary to format any dates into that locale.
For that reason, one should think of the process of formatting a date in two steps - first, a computational
heavy construction of DateTimeFormat
, and then fast formatting of DateTimeInput
data using the instance.
Examples
use icu::locid::Locale; use icu::locid::macros::langid; use icu::datetime::{DateTimeFormat, options::length}; use icu::datetime::mock::datetime::MockDateTime; use icu_provider::inv::InvariantDataProvider; let locale: Locale = langid!("en").into(); let provider = InvariantDataProvider; let options = length::Bag { date: Some(length::Date::Medium), time: Some(length::Time::Short), ..Default::default() }; let dtf = DateTimeFormat::try_new(locale, &provider, &options.into()) .expect("Failed to create DateTimeFormat instance."); let datetime = MockDateTime::try_new(2020, 9, 1, 12, 34, 28) .expect("Failed to construct DateTime."); let value = dtf.format_to_string(&datetime);
This model replicates that of ICU
and ECMA402
. In the future this will become even more pronounced
when we introduce asynchronous DataProvider
and corresponding asynchronous constructor.
Implementations
impl<'d> DateTimeFormat<'d>
[src]
impl<'d> DateTimeFormat<'d>
[src]pub fn try_new<T: Into<Locale>, D: DataProvider<'d, DatesV1> + ?Sized>(
locale: T,
data_provider: &D,
options: &DateTimeFormatOptions
) -> Result<Self, DateTimeFormatError>
[src]
locale: T,
data_provider: &D,
options: &DateTimeFormatOptions
) -> Result<Self, DateTimeFormatError>
Constructor that takes a selected Locale
, reference to a DataProvider
and
a list of options, then collects all data necessary to format date and time values into the given locale.
Examples
use icu::locid::Locale; use icu::locid::macros::langid; use icu::datetime::{DateTimeFormat, DateTimeFormatOptions}; use icu::datetime::mock::datetime::MockDateTime; use icu_provider::inv::InvariantDataProvider; let locale: Locale = langid!("en").into(); let provider = InvariantDataProvider; let options = DateTimeFormatOptions::default(); let dtf = DateTimeFormat::try_new(locale, &provider, &options); assert_eq!(dtf.is_ok(), true);
pub fn format<'l, T>(&'l self, value: &'l T) -> FormattedDateTime<'l, T> where
T: DateTimeInput,
[src]
T: DateTimeInput,
Takes a DateTimeInput
implementer and returns an instance of a FormattedDateTime
that contains all information necessary to display a formatted date and operate on it.
Examples
use icu::locid::Locale; use icu::locid::macros::langid; use icu::datetime::{DateTimeFormat, DateTimeFormatOptions}; use icu::datetime::mock::datetime::MockDateTime; use icu_provider::inv::InvariantDataProvider; let dtf = DateTimeFormat::try_new(locale, &provider, &options) .expect("Failed to create DateTimeFormat instance."); let datetime = MockDateTime::try_new(2020, 9, 1, 12, 34, 28) .expect("Failed to construct DateTime."); let formatted_date = dtf.format(&datetime); let _ = format!("Date: {}", formatted_date);
At the moment, there’s little value in using that over one of the other format
methods,
but FormattedDateTime
will grow with methods for iterating over fields, extracting information
about formatted date and so on.
pub fn format_to_write(
&self,
w: &mut impl Write,
value: &impl DateTimeInput
) -> Result
[src]
&self,
w: &mut impl Write,
value: &impl DateTimeInput
) -> Result
Takes a mutable reference to anything that implements Write
trait
and a DateTimeInput
implementer and populates the buffer with a formatted value.
Examples
use icu::locid::Locale; use icu::locid::macros::langid; use icu::datetime::{DateTimeFormat, DateTimeFormatOptions}; use icu::datetime::mock::datetime::MockDateTime; use icu_provider::inv::InvariantDataProvider; let dtf = DateTimeFormat::try_new(locale, &provider, &options.into()) .expect("Failed to create DateTimeFormat instance."); let datetime = MockDateTime::try_new(2020, 9, 1, 12, 34, 28) .expect("Failed to construct DateTime."); let mut buffer = String::new(); dtf.format_to_write(&mut buffer, &datetime) .expect("Failed to write to a buffer."); let _ = format!("Date: {}", buffer);
pub fn format_to_string(&self, value: &impl DateTimeInput) -> String
[src]
Takes a DateTimeInput
implementer and returns it formatted as a string.
Examples
use icu::locid::Locale; use icu::locid::macros::langid; use icu::datetime::{DateTimeFormat, DateTimeFormatOptions}; use icu::datetime::mock::datetime::MockDateTime; use icu_provider::inv::InvariantDataProvider; let dtf = DateTimeFormat::try_new(locale, &provider, &options.into()) .expect("Failed to create DateTimeFormat instance."); let datetime = MockDateTime::try_new(2020, 9, 1, 12, 34, 28) .expect("Failed to construct DateTime."); let _ = dtf.format_to_string(&datetime);
Auto Trait Implementations
impl<'d> RefUnwindSafe for DateTimeFormat<'d>
impl<'d> RefUnwindSafe for DateTimeFormat<'d>
impl<'d> Send for DateTimeFormat<'d>
impl<'d> Send for DateTimeFormat<'d>
impl<'d> Sync for DateTimeFormat<'d>
impl<'d> Sync for DateTimeFormat<'d>
impl<'d> Unpin for DateTimeFormat<'d>
impl<'d> Unpin for DateTimeFormat<'d>
impl<'d> UnwindSafe for DateTimeFormat<'d>
impl<'d> UnwindSafe for DateTimeFormat<'d>