pub struct TypedZonedDateTimeFormatter<C>(ZonedDateTimeFormatter, PhantomData<C>);
Expand description
The composition of TypedDateTimeFormatter
and TimeZoneFormatter
.
TypedZonedDateTimeFormatter
is a formatter capable of formatting
date/times with time zones from a calendar selected at compile time. For the difference between this
and DateTimeFormatter
, please read the crate root docs.
TypedZonedDateTimeFormatter
uses data from the [data provider]s, the selected locale, and the
provided pattern to collect all data necessary to format a datetime with time zones into that locale.
The various pattern symbols specified in UTS-35 require different sets of data for formatting.
As such, TimeZoneFormatter
will pull in only the resources it needs to format that pattern
that is derived from the provided DateTimeFormatterOptions
.
For that reason, one should think of the process of formatting a zoned datetime in two steps:
first, a computationally heavy construction of TypedZonedDateTimeFormatter
, and then fast formatting
of the data using the instance.
§Examples
use icu::calendar::{DateTime, Gregorian};
use icu::datetime::time_zone::TimeZoneFormatterOptions;
use icu::datetime::{options::length, TypedZonedDateTimeFormatter};
use icu::locid::locale;
use icu::timezone::CustomTimeZone;
use std::str::FromStr;
use writeable::assert_writeable_eq;
let options = length::Bag::from_date_time_style(
length::Date::Medium,
length::Time::Long,
);
let zdtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new(
&locale!("en").into(),
options.into(),
TimeZoneFormatterOptions::default(),
)
.expect("Failed to create TypedDateTimeFormatter instance.");
let datetime =
DateTime::try_new_gregorian_datetime(2020, 9, 12, 12, 34, 28).unwrap();
let time_zone = CustomTimeZone::from_str("-07:00").unwrap();
let formatted_date = zdtf.format(&datetime, &time_zone);
assert_writeable_eq!(formatted_date, "Sep 12, 2020, 12:34:28 PM GMT-07:00");
Tuple Fields§
§0: ZonedDateTimeFormatter
§1: PhantomData<C>
Implementations§
source§impl<C: CldrCalendar> TypedZonedDateTimeFormatter<C>
impl<C: CldrCalendar> TypedZonedDateTimeFormatter<C>
sourcepub fn try_new(
locale: &DataLocale,
date_time_format_options: DateTimeFormatterOptions,
time_zone_format_options: TimeZoneFormatterOptions,
) -> Result<Self, DateTimeError>
pub fn try_new( locale: &DataLocale, date_time_format_options: DateTimeFormatterOptions, time_zone_format_options: TimeZoneFormatterOptions, ) -> Result<Self, DateTimeError>
Constructor that takes a selected locale and a list of DateTimeFormatterOptions
.
It collects all data necessary to format zoned datetime values into the given locale.
✨ Enabled with the compiled_data
Cargo feature.
§Examples
use icu::calendar::{DateTime, Gregorian};
use icu::datetime::time_zone::TimeZoneFormatterOptions;
use icu::datetime::{options::length, TypedZonedDateTimeFormatter};
use icu::locid::locale;
use icu::timezone::CustomTimeZone;
use writeable::assert_writeable_eq;
let options = length::Bag::from_date_time_style(
length::Date::Medium,
length::Time::Long,
);
let zdtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new(
&locale!("en").into(),
options.into(),
TimeZoneFormatterOptions::default(),
)
.unwrap();
let datetime =
DateTime::try_new_gregorian_datetime(2022, 8, 31, 1, 2, 3).unwrap();
assert_writeable_eq!(
zdtf.format(&datetime, &CustomTimeZone::utc()),
"Aug 31, 2022, 1:02:03 AM GMT",
);
sourcepub fn try_new_with_any_provider(
provider: &(impl AnyProvider + ?Sized),
locale: &DataLocale,
date_time_format_options: DateTimeFormatterOptions,
time_zone_format_options: TimeZoneFormatterOptions,
) -> Result<Self, DateTimeError>
pub fn try_new_with_any_provider( provider: &(impl AnyProvider + ?Sized), locale: &DataLocale, date_time_format_options: DateTimeFormatterOptions, time_zone_format_options: TimeZoneFormatterOptions, ) -> Result<Self, DateTimeError>
A version of Self::try_new
that uses custom data provided by an AnyProvider
.
sourcepub fn try_new_unstable<P>(
provider: &P,
locale: &DataLocale,
date_time_format_options: DateTimeFormatterOptions,
time_zone_format_options: TimeZoneFormatterOptions,
) -> Result<Self, DateTimeError>where
P: DataProvider<<C as CldrCalendar>::DateSymbolsV1Marker> + DataProvider<<C as CldrCalendar>::DateLengthsV1Marker> + DataProvider<TimeSymbolsV1Marker> + DataProvider<TimeLengthsV1Marker> + DataProvider<WeekDataV1Marker> + DataProvider<TimeZoneFormatsV1Marker> + DataProvider<ExemplarCitiesV1Marker> + DataProvider<MetazoneGenericNamesLongV1Marker> + DataProvider<MetazoneGenericNamesShortV1Marker> + DataProvider<MetazoneSpecificNamesLongV1Marker> + DataProvider<MetazoneSpecificNamesShortV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<DecimalSymbolsV1Marker> + ?Sized,
pub fn try_new_unstable<P>(
provider: &P,
locale: &DataLocale,
date_time_format_options: DateTimeFormatterOptions,
time_zone_format_options: TimeZoneFormatterOptions,
) -> Result<Self, DateTimeError>where
P: DataProvider<<C as CldrCalendar>::DateSymbolsV1Marker> + DataProvider<<C as CldrCalendar>::DateLengthsV1Marker> + DataProvider<TimeSymbolsV1Marker> + DataProvider<TimeLengthsV1Marker> + DataProvider<WeekDataV1Marker> + DataProvider<TimeZoneFormatsV1Marker> + DataProvider<ExemplarCitiesV1Marker> + DataProvider<MetazoneGenericNamesLongV1Marker> + DataProvider<MetazoneGenericNamesShortV1Marker> + DataProvider<MetazoneSpecificNamesLongV1Marker> + DataProvider<MetazoneSpecificNamesShortV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<DecimalSymbolsV1Marker> + ?Sized,
A version of Self::try_new
that uses custom data provided by a DataProvider
.
sourcepub fn format<'l>(
&'l self,
date: &impl DateTimeInput<Calendar = C>,
time_zone: &impl TimeZoneInput,
) -> FormattedZonedDateTime<'l>
pub fn format<'l>( &'l self, date: &impl DateTimeInput<Calendar = C>, time_zone: &impl TimeZoneInput, ) -> FormattedZonedDateTime<'l>
Takes a DateTimeInput
and a TimeZoneInput
and returns an instance of a FormattedZonedDateTime
that contains all information necessary to display a formatted zoned datetime and operate on it.
§Examples
use icu::calendar::{DateTime, Gregorian};
use icu::datetime::{options::length, TypedZonedDateTimeFormatter};
use icu::locid::locale;
use icu::timezone::CustomTimeZone;
use std::str::FromStr;
use writeable::assert_writeable_eq;
let options = length::Bag::from_date_time_style(
length::Date::Medium,
length::Time::Long,
);
let zdtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new(
&locale!("en").into(),
options.into(),
Default::default(),
)
.expect("Failed to create TypedZonedDateTimeFormatter instance.");
let datetime =
DateTime::try_new_gregorian_datetime(2020, 9, 12, 12, 34, 28).unwrap();
let time_zone = CustomTimeZone::from_str("-07:00").unwrap();
let formatted_date = zdtf.format(&datetime, &time_zone);
assert_writeable_eq!(formatted_date, "Sep 12, 2020, 12:34:28 PM GMT-07:00");
sourcepub fn format_to_string(
&self,
date: &impl DateTimeInput<Calendar = C>,
time_zone: &impl TimeZoneInput,
) -> String
pub fn format_to_string( &self, date: &impl DateTimeInput<Calendar = C>, time_zone: &impl TimeZoneInput, ) -> String
Takes a DateTimeInput
and a TimeZoneInput
and returns it formatted as a string.
§Examples
use icu::calendar::{DateTime, Gregorian};
use icu::datetime::{options::length, TypedZonedDateTimeFormatter};
use icu::locid::locale;
use icu::timezone::CustomTimeZone;
use std::str::FromStr;
let options = length::Bag::from_date_time_style(
length::Date::Medium,
length::Time::Long,
);
let zdtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new(
&locale!("en").into(),
options.into(),
Default::default(),
)
.expect("Failed to create TypedZonedDateTimeFormatter instance.");
let datetime =
DateTime::try_new_gregorian_datetime(2020, 9, 12, 12, 34, 28).unwrap();
let time_zone = CustomTimeZone::from_str("-07:00").unwrap();
let formatted_string = zdtf.format_to_string(&datetime, &time_zone);
assert_eq!(formatted_string, "Sep 12, 2020, 12:34:28 PM GMT-07:00");
Trait Implementations§
Auto Trait Implementations§
impl<C> Freeze for TypedZonedDateTimeFormatter<C>
impl<C> RefUnwindSafe for TypedZonedDateTimeFormatter<C>where
C: RefUnwindSafe,
impl<C> !Send for TypedZonedDateTimeFormatter<C>
impl<C> !Sync for TypedZonedDateTimeFormatter<C>
impl<C> Unpin for TypedZonedDateTimeFormatter<C>where
C: Unpin,
impl<C> UnwindSafe for TypedZonedDateTimeFormatter<C>where
C: UnwindSafe,
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