pub struct TimeFormatter(/* private fields */);
Expand description
TimeFormatter
is a structure of the icu::datetime
component that provides time formatting only.
When constructed, it uses data from the data provider, selected locale and provided preferences to
collect all data necessary to format any time into that locale.
For that reason, one should think of the process of formatting a time in two steps - first, a computational
heavy construction of TimeFormatter
, and then fast formatting of DateTimeInput
data using the instance.
๐ This item has a stack size of 1200 bytes on the stable toolchain at release date.
ยงExamples
use icu::calendar::DateTime;
use icu::datetime::{options::length::Time, TimeFormatter};
use icu::locid::locale;
use writeable::assert_writeable_eq;
let tf =
TimeFormatter::try_new_with_length(&locale!("en").into(), Time::Short)
.expect("Failed to create TimeFormatter instance.");
let datetime = DateTime::try_new_gregorian_datetime(2020, 9, 1, 12, 34, 28)
.expect("Failed to construct DateTime.");
assert_writeable_eq!(tf.format(&datetime), "12:34โฏPM");
This model replicates that of ICU
and ECMA402
.
Implementationsยง
Sourceยงimpl TimeFormatter
impl TimeFormatter
Sourcepub fn try_new_with_length(
locale: &DataLocale,
length: Time,
) -> Result<Self, DateTimeError>
pub fn try_new_with_length( locale: &DataLocale, length: Time, ) -> Result<Self, DateTimeError>
Constructor that takes a selected locale and a list of preferences, then collects all compiled data necessary to format date and time values into the given locale, using the given style.
โจ Enabled with the compiled_data
Cargo feature.
๐ Help choosing a constructor
ยงExamples
use icu::datetime::{options::length::Time, TimeFormatter};
use icu::locid::locale;
TimeFormatter::try_new_with_length(&locale!("en").into(), Time::Short)
.unwrap();
Sourcepub fn try_new_with_length_with_any_provider(
provider: &(impl AnyProvider + ?Sized),
locale: &DataLocale,
length: Time,
) -> Result<Self, DateTimeError>
pub fn try_new_with_length_with_any_provider( provider: &(impl AnyProvider + ?Sized), locale: &DataLocale, length: Time, ) -> Result<Self, DateTimeError>
A version of [Self :: try_new_with_length
] that uses custom data provided by an AnyProvider
.
Sourcepub fn try_new_with_length_with_buffer_provider(
provider: &(impl BufferProvider + ?Sized),
locale: &DataLocale,
length: Time,
) -> Result<Self, DateTimeError>
pub fn try_new_with_length_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), locale: &DataLocale, length: Time, ) -> Result<Self, DateTimeError>
A version of [Self :: try_new_with_length
] that uses custom data provided by a BufferProvider
.
โจ Enabled with the serde
feature.
Sourcepub fn try_new_with_length_unstable<D>(
provider: &D,
locale: &DataLocale,
length: Time,
) -> Result<Self, DateTimeError>where
D: DataProvider<TimeLengthsV1Marker> + DataProvider<TimeSymbolsV1Marker> + DataProvider<DecimalSymbolsV1Marker> + ?Sized,
pub fn try_new_with_length_unstable<D>(
provider: &D,
locale: &DataLocale,
length: Time,
) -> Result<Self, DateTimeError>where
D: DataProvider<TimeLengthsV1Marker> + DataProvider<TimeSymbolsV1Marker> + DataProvider<DecimalSymbolsV1Marker> + ?Sized,
A version of Self::try_new_with_length
that uses custom data provided by a DataProvider
.
๐ Help choosing a constructor
Sourcepub fn format<'l, T>(&'l self, value: &T) -> FormattedDateTime<'l>where
T: IsoTimeInput,
pub fn format<'l, T>(&'l self, value: &T) -> FormattedDateTime<'l>where
T: IsoTimeInput,
Takes a IsoTimeInput
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::calendar::DateTime;
use icu::datetime::{options::length::Time, TimeFormatter};
use icu::locid::locale;
use writeable::assert_writeable_eq;
let tf =
TimeFormatter::try_new_with_length(&locale!("en").into(), Time::Short)
.expect("Failed to create TimeFormatter instance.");
let datetime = DateTime::try_new_gregorian_datetime(2020, 9, 1, 12, 34, 28)
.expect("Failed to construct DateTime.");
assert_writeable_eq!(tf.format(&datetime), "12:34โฏPM");
Sourcepub fn format_to_string(&self, value: &impl IsoTimeInput) -> String
pub fn format_to_string(&self, value: &impl IsoTimeInput) -> String
Takes a IsoTimeInput
implementer and returns it formatted as a string.
ยงExamples
use icu::calendar::DateTime;
use icu::datetime::{options::length::Time, TimeFormatter};
use icu::locid::locale;
let tf =
TimeFormatter::try_new_with_length(&locale!("en").into(), Time::Short)
.expect("Failed to create TimeFormatter instance.");
let datetime = DateTime::try_new_gregorian_datetime(2020, 9, 1, 12, 34, 28)
.expect("Failed to construct DateTime.");
assert_eq!(tf.format_to_string(&datetime), "12:34โฏPM");
Trait Implementationsยง
Auto Trait Implementationsยง
impl Freeze for TimeFormatter
impl RefUnwindSafe for TimeFormatter
impl !Send for TimeFormatter
impl !Sync for TimeFormatter
impl Unpin for TimeFormatter
impl UnwindSafe for TimeFormatter
Blanket Implementationsยง
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> IntoEither for T
impl<T> IntoEither for T
Sourceยงfn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSourceยงfn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more