Struct icu_datetime::pattern::DateTimePatternFormatter

source ·
pub struct DateTimePatternFormatter<'a, C: CldrCalendar, FSet> { /* private fields */ }
Expand description

A formatter for a specific DateTimePattern.

❗ This type forgoes most internationalization functionality of the datetime crate. It assumes that the pattern is already localized for the customer’s locale. Most clients should use DateTimeFormatter instead of directly formatting with patterns.

Create one of these via factory methods on TypedDateTimeNames.

Implementations§

source§

impl<'a, C: CldrCalendar, FSet: DateTimeMarkers> DateTimePatternFormatter<'a, C, FSet>
where FSet::D: TypedDateDataMarkers<C> + DateInputMarkers, FSet::T: TimeMarkers, FSet::Z: ZoneMarkers,

source

pub fn format<I>(&self, datetime: &I) -> FormattedDateTimePattern<'a>
where I: ?Sized + InFixedCalendar<C> + AllInputMarkers<FSet>,

Formats a date and time of day with a custom date/time pattern.

§Examples

Format a date:

use icu::calendar::Date;
use icu::calendar::Gregorian;
use icu::datetime::fields;
use icu::datetime::fields::FieldLength;
use icu::datetime::fieldsets::enums::DateFieldSet;
use icu::datetime::pattern::DateTimePattern;
use icu::datetime::pattern::TypedDateTimeNames;
use icu::locale::locale;
use writeable::assert_try_writeable_eq;

// Create an instance that can format wide month and era names:
let mut names: TypedDateTimeNames<Gregorian, DateFieldSet> =
    TypedDateTimeNames::try_new(locale!("en-GB").into()).unwrap();
names
    .include_month_names(fields::Month::Format, FieldLength::Four)
    .unwrap()
    .include_year_names(FieldLength::Four)
    .unwrap();

// Create a pattern from a pattern string:
let pattern_str = "'The date is:' MMMM d, y GGGG";
let pattern: DateTimePattern = pattern_str.parse().unwrap();

// Test it with some different dates:
// Note: extended year -50 is year 51 BCE
let date_bce = Date::try_new_gregorian(-50, 3, 15).unwrap();
let date_ce = Date::try_new_gregorian(1700, 11, 20).unwrap();
assert_try_writeable_eq!(
    names.with_pattern_unchecked(&pattern).format(&date_bce),
    "The date is: March 15, 51 Before Christ"
);
assert_try_writeable_eq!(
    names.with_pattern_unchecked(&pattern).format(&date_ce),
    "The date is: November 20, 1700 Anno Domini"
);

Format a time:

use icu::calendar::Gregorian;
use icu::calendar::Time;
use icu::datetime::fields::FieldLength;
use icu::datetime::fieldsets::enums::TimeFieldSet;
use icu::datetime::pattern::DateTimePattern;
use icu::datetime::pattern::TypedDateTimeNames;
use icu::locale::locale;
use writeable::assert_try_writeable_eq;

// Create an instance that can format abbreviated day periods:
let mut names: TypedDateTimeNames<Gregorian, TimeFieldSet> =
    TypedDateTimeNames::try_new(locale!("en-US").into()).unwrap();
names.include_day_period_names(FieldLength::Three).unwrap();

// Create a pattern from a pattern string:
let pattern_str = "'The time is:' h:mm b";
let pattern: DateTimePattern = pattern_str.parse().unwrap();

// Test it with different times of day:
let time_am = Time::try_new(11, 4, 14, 0).unwrap();
let time_pm = Time::try_new(13, 41, 28, 0).unwrap();
let time_noon = Time::try_new(12, 0, 0, 0).unwrap();
let time_midnight = Time::try_new(0, 0, 0, 0).unwrap();
assert_try_writeable_eq!(
    names.with_pattern_unchecked(&pattern).format(&time_am),
    "The time is: 11:04 AM"
);
assert_try_writeable_eq!(
    names.with_pattern_unchecked(&pattern).format(&time_pm),
    "The time is: 1:41 PM"
);
assert_try_writeable_eq!(
    names.with_pattern_unchecked(&pattern).format(&time_noon),
    "The time is: 12:00 noon"
);
assert_try_writeable_eq!(
    names
        .with_pattern_unchecked(&pattern)
        .format(&time_midnight),
    "The time is: 12:00 midnight"
);

Format a time zone:

use icu::calendar::Gregorian;
use icu::datetime::fieldsets::enums::ZoneFieldSet;
use icu::datetime::pattern::DateTimePattern;
use icu::datetime::pattern::TypedDateTimeNames;
use icu::locale::locale;
use icu::timezone::IxdtfParser;
use writeable::assert_try_writeable_eq;

let mut london_winter = IxdtfParser::new()
    .try_from_str("2024-01-01T00:00:00+00:00[Europe/London]")
    .unwrap()
    .to_calendar(Gregorian);
let mut london_summer = IxdtfParser::new()
    .try_from_str("2024-07-01T00:00:00+01:00[Europe/London]")
    .unwrap()
    .to_calendar(Gregorian);

let mut names = TypedDateTimeNames::<Gregorian, ZoneFieldSet>::try_new(
    locale!("en-GB").into(),
)
.unwrap();

names.include_time_zone_essentials().unwrap();
names.include_time_zone_specific_short_names().unwrap();

// Create a pattern with symbol `z`:
let pattern_str = "'Your time zone is:' z";
let pattern: DateTimePattern = pattern_str.parse().unwrap();

assert_try_writeable_eq!(
    names
        .with_pattern_unchecked(&pattern)
        .format(&london_winter),
    "Your time zone is: GMT",
);
assert_try_writeable_eq!(
    names
        .with_pattern_unchecked(&pattern)
        .format(&london_summer),
    "Your time zone is: BST",
);

Trait Implementations§

source§

impl<'a, C: Clone + CldrCalendar, FSet: Clone> Clone for DateTimePatternFormatter<'a, C, FSet>

source§

fn clone(&self) -> DateTimePatternFormatter<'a, C, FSet>

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl<'a, C: Debug + CldrCalendar, FSet: Debug> Debug for DateTimePatternFormatter<'a, C, FSet>

source§

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

Formats the value using the given formatter. Read more
source§

impl<'a, C: Copy + CldrCalendar, FSet: Copy> Copy for DateTimePatternFormatter<'a, C, FSet>

Auto Trait Implementations§

§

impl<'a, C, FSet> Freeze for DateTimePatternFormatter<'a, C, FSet>

§

impl<'a, C, FSet> RefUnwindSafe for DateTimePatternFormatter<'a, C, FSet>
where C: RefUnwindSafe, FSet: RefUnwindSafe,

§

impl<'a, C, FSet> Send for DateTimePatternFormatter<'a, C, FSet>
where C: Send, FSet: Send,

§

impl<'a, C, FSet> Sync for DateTimePatternFormatter<'a, C, FSet>
where C: Sync, FSet: Sync,

§

impl<'a, C, FSet> Unpin for DateTimePatternFormatter<'a, C, FSet>
where C: Unpin, FSet: Unpin,

§

impl<'a, C, FSet> UnwindSafe for DateTimePatternFormatter<'a, C, FSet>
where C: UnwindSafe, FSet: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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> ToOwned for T
where T: Clone,

source§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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.
§

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

§

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