pub struct TimeFormatter(_);
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.

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_unstable(
    &icu_testdata::unstable(),
    &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_unstable<D>( data_provider: &D, locale: &DataLocale, length: Time ) -> Result<Self, DateTimeError>where D: DataProvider<TimeLengthsV1Marker> + DataProvider<TimeSymbolsV1Marker> + DataProvider<DecimalSymbolsV1Marker> + ?Sized,

Constructor that takes a selected locale, reference to a data provider and a list of preferences, then collects all data necessary to format date and time values into the given locale, using the short style.

📚 Help choosing a constructor

⚠️ The bounds on this function may change over time, including in SemVer minor releases.
Examples
use icu::datetime::{options::length::Time, TimeFormatter};
use icu::locid::locale;

TimeFormatter::try_new_with_length_unstable(
    &icu_testdata::unstable(),
    &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>

Creates a new instance using an AnyProvider.

For details on the behavior of this function, see: Self::try_new_with_length_unstable

📚 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>

Enabled with the "serde" feature.

Creates a new instance using a BufferProvider.

For details on the behavior of this function, see: Self::try_new_with_length_unstable

📚 Help choosing a constructor

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_unstable(
    &icu_testdata::unstable(),
    &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_unstable(
    &icu_testdata::unstable(),
    &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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere 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 Twhere 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, U> TryFrom<U> for Twhere U: Into<T>,

§

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 Twhere U: TryFrom<T>,

§

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.
§

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

source§

impl<T> MaybeSendSync for Twhere T: Send + Sync,