icu_datetime

Struct TimeFormatter

Source
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

Source

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();
Source

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.

๐Ÿ“š Help choosing a constructor

Source

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.

๐Ÿ“š Help choosing a constructor

Source

pub fn try_new_with_length_unstable<D>( provider: &D, locale: &DataLocale, length: Time, ) -> Result<Self, DateTimeError>

A version of Self::try_new_with_length that uses custom data provided by a DataProvider.

๐Ÿ“š Help choosing a constructor

โš ๏ธ The bounds on provider may change over time, including in SemVer minor releases.
Source

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");
Source

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ยง

Sourceยง

impl Debug for TimeFormatter

Sourceยง

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

Formats the value using the given formatter. 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> 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> 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> IntoEither for T

Sourceยง

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Sourceยง

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Sourceยง

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

Sourceยง

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.
Sourceยง

impl<T> ErasedDestructor for T
where T: 'static,

Sourceยง

impl<T> MaybeSendSync for T