Struct icu_datetime::TypedZonedDateTimeFormatter
source · [−]pub struct TypedZonedDateTimeFormatter<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::Gregorian;
use icu::datetime::mock::parse_zoned_gregorian_from_str;
use icu::datetime::{options::length, TypedZonedDateTimeFormatter};
use icu::locid::locale;
use icu_datetime::TimeZoneFormatterOptions;
let provider = icu_testdata::get_provider();
let options = length::Bag::from_date_time_style(length::Date::Medium, length::Time::Short);
let zdtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new_with_buffer_provider(
&provider,
&locale!("en").into(),
options.into(),
TimeZoneFormatterOptions::default(),
)
.expect("Failed to create TypedDateTimeFormatter instance.");
let (datetime, time_zone) = parse_zoned_gregorian_from_str("2021-04-08T16:12:37.000-07:00")
.expect("Failed to parse zoned datetime");
let value = zdtf.format_to_string(&datetime, &time_zone);
Implementations
sourceimpl<C: CldrCalendar> TypedZonedDateTimeFormatter<C>
impl<C: CldrCalendar> TypedZonedDateTimeFormatter<C>
sourcepub fn try_new_unstable<P>(
provider: &P,
locale: &DataLocale,
date_time_format_options: DateTimeFormatterOptions,
time_zone_format_options: TimeZoneFormatterOptions
) -> Result<Self, DateTimeFormatterError> where
P: DataProvider<<C as CldrCalendar>::DateSymbolsV1Marker> + DataProvider<<C as CldrCalendar>::DateLengthsV1Marker> + DataProvider<TimeSymbolsV1Marker> + DataProvider<TimeLengthsV1Marker> + DataProvider<DateSkeletonPatternsV1Marker> + DataProvider<WeekDataV1Marker> + DataProvider<TimeZoneFormatsV1Marker> + DataProvider<ExemplarCitiesV1Marker> + DataProvider<MetaZoneGenericNamesLongV1Marker> + DataProvider<MetaZoneGenericNamesShortV1Marker> + DataProvider<MetaZoneSpecificNamesLongV1Marker> + DataProvider<MetaZoneSpecificNamesShortV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<JapaneseErasV1Marker> + ?Sized,
pub fn try_new_unstable<P>(
provider: &P,
locale: &DataLocale,
date_time_format_options: DateTimeFormatterOptions,
time_zone_format_options: TimeZoneFormatterOptions
) -> Result<Self, DateTimeFormatterError> where
P: DataProvider<<C as CldrCalendar>::DateSymbolsV1Marker> + DataProvider<<C as CldrCalendar>::DateLengthsV1Marker> + DataProvider<TimeSymbolsV1Marker> + DataProvider<TimeLengthsV1Marker> + DataProvider<DateSkeletonPatternsV1Marker> + DataProvider<WeekDataV1Marker> + DataProvider<TimeZoneFormatsV1Marker> + DataProvider<ExemplarCitiesV1Marker> + DataProvider<MetaZoneGenericNamesLongV1Marker> + DataProvider<MetaZoneGenericNamesShortV1Marker> + DataProvider<MetaZoneSpecificNamesLongV1Marker> + DataProvider<MetaZoneSpecificNamesShortV1Marker> + DataProvider<OrdinalV1Marker> + DataProvider<DecimalSymbolsV1Marker> + DataProvider<JapaneseErasV1Marker> + ?Sized,
Constructor that takes a selected locale, a reference to a data provider for
dates, a data provider for time zones, and a list of DateTimeFormatterOptions
.
It collects all data necessary to format zoned datetime values into the given locale.
Examples
use icu::calendar::Gregorian;
use icu::datetime::{DateTimeFormatterOptions, TypedZonedDateTimeFormatter};
use icu::locid::locale;
use icu_datetime::TimeZoneFormatterOptions;
let provider = icu_testdata::get_provider();
let options = DateTimeFormatterOptions::default();
let zdtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new_with_buffer_provider(
&provider,
&locale!("en").into(),
options,
TimeZoneFormatterOptions::default(),
);
assert_eq!(zdtf.is_ok(), true);
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, DateTimeFormatterError>
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, DateTimeFormatterError>
Create a new instance using an AnyProvider
.
See also: Self::try_new_unstable
sourcepub fn try_new_with_buffer_provider(
provider: &impl BufferProvider + ?Sized,
locale: &DataLocale,
date_time_format_options: DateTimeFormatterOptions,
time_zone_format_options: TimeZoneFormatterOptions
) -> Result<Self, DateTimeFormatterError>
pub fn try_new_with_buffer_provider(
provider: &impl BufferProvider + ?Sized,
locale: &DataLocale,
date_time_format_options: DateTimeFormatterOptions,
time_zone_format_options: TimeZoneFormatterOptions
) -> Result<Self, DateTimeFormatterError>
Create a new instance using a BufferProvider
. Enabled with the "serde"
feature.
See also: Self::try_new_unstable
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::Gregorian;
use icu::datetime::mock::parse_zoned_gregorian_from_str;
use icu::datetime::TypedZonedDateTimeFormatter;
use icu_datetime::TimeZoneFormatterOptions;
let zdtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new_with_buffer_provider(
&provider,
&locale.into(),
options,
TimeZoneFormatterOptions::default(),
)
.expect("Failed to create TypedZonedDateTimeFormatter instance.");
let (datetime, time_zone) = parse_zoned_gregorian_from_str("2021-04-08T16:12:37.000-07:00")
.expect("Failed to parse zoned datetime");
let formatted_date = zdtf.format(&datetime, &time_zone);
let _ = format!("Date: {}", formatted_date);
At the moment, there’s little value in using that over one of the other format
methods,
but FormattedZonedDateTime
will grow with methods for iterating over fields, extracting information
about formatted date and so on.
sourcepub fn format_to_write(
&self,
w: &mut impl Write,
date: &impl DateTimeInput<Calendar = C>,
time_zone: &impl TimeZoneInput
) -> Result
pub fn format_to_write(
&self,
w: &mut impl Write,
date: &impl DateTimeInput<Calendar = C>,
time_zone: &impl TimeZoneInput
) -> Result
Takes a mutable reference to anything that implements the Write
trait
and a DateTimeInput
and a TimeZoneInput
, then populates the buffer with a formatted value.
Examples
use icu::calendar::Gregorian;
use icu::datetime::mock::parse_zoned_gregorian_from_str;
use icu::datetime::TypedZonedDateTimeFormatter;
use icu_datetime::TimeZoneFormatterOptions;
let zdtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new_with_buffer_provider(
&provider,
&locale.into(),
options.into(),
TimeZoneFormatterOptions::default(),
)
.expect("Failed to create TypedZonedDateTimeFormatter instance.");
let (datetime, time_zone) = parse_zoned_gregorian_from_str("2021-04-08T16:12:37.000-07:00")
.expect("Failed to parse zoned datetime");
let mut buffer = String::new();
zdtf.format_to_write(&mut buffer, &datetime, &time_zone)
.expect("Failed to write to a buffer.");
let _ = format!("Date: {}", buffer);
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::Gregorian;
use icu::datetime::mock::parse_zoned_gregorian_from_str;
use icu::datetime::TypedZonedDateTimeFormatter;
use icu_datetime::TimeZoneFormatterOptions;
let zdtf = TypedZonedDateTimeFormatter::<Gregorian>::try_new_with_buffer_provider(
&provider,
&locale.into(),
options.into(),
TimeZoneFormatterOptions::default(),
)
.expect("Failed to create TypedZonedDateTimeFormatter instance.");
let (datetime, time_zone) = parse_zoned_gregorian_from_str("2021-04-08T16:12:37.000-07:00")
.expect("Failed to parse zoned datetime");
let _ = zdtf.format_to_string(&datetime, &time_zone);
Auto Trait Implementations
impl<C> RefUnwindSafe for TypedZonedDateTimeFormatter<C> where
C: RefUnwindSafe,
impl<C> Send for TypedZonedDateTimeFormatter<C> where
C: Send,
impl<C> Sync for TypedZonedDateTimeFormatter<C> where
C: Sync,
impl<C> Unpin for TypedZonedDateTimeFormatter<C> where
C: Unpin,
impl<C> UnwindSafe for TypedZonedDateTimeFormatter<C> where
C: UnwindSafe,
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more