Struct icu_timezone::TimeZoneIdMapper

source ·
pub struct TimeZoneIdMapper {
    data: DataPayload<IanaToBcp47MapV2Marker>,
}
Expand description

A mapper between IANA time zone identifiers and BCP-47 time zone identifiers.

This mapper supports two-way mapping, but it is optimized for the case of IANA to BCP-47. It also supports normalizing and canonicalizing the IANA strings.

There are approximately 600 IANA identifiers and 450 BCP-47 identifiers.

BCP-47 time zone identifiers are 8 ASCII characters or less and currently average 5.1 characters long. Current IANA time zone identifiers are less than 40 ASCII characters and average 14.2 characters long.

These lists grow very slowly; in a typical year, 2-3 new identifiers are added.

§Normalization vs Canonicalization

Multiple IANA time zone identifiers can refer to the same BCP-47 time zone. For example, the following three IANA identifiers all map to "usind":

  • “America/Fort_Wayne”
  • “America/Indiana/Indianapolis”
  • “America/Indianapolis”
  • “US/East-Indiana”

There is only one canonical identifier, which is “America/Indiana/Indianapolis”. The canonicalization operation returns the canonical identifier. You should canonicalize if you need to compare time zones for equality. Note that the canonical identifier can change over time. For example, the identifier “Europe/Kiev” was renamed to the newly-added identifier “Europe/Kyiv” in 2022.

The normalization operation, on the other hand, keeps the input identifier but normalizes the casing. For example, “AMERICA/FORT_WAYNE” normalizes to “America/Fort_Wayne”. Normalization is a data-driven operation because there are no algorithmic casing rules that work for all IANA time zone identifiers.

Normalization is a cheap operation, but canonicalization might be expensive, since it might require searching over all IANA IDs to find the canonicalization. If you need canonicalization that is reliably fast, use TimeZoneIdMapperWithFastCanonicalization.

§Examples

use icu::timezone::TimeZoneIdMapper;

let mapper = TimeZoneIdMapper::new();
let mapper = mapper.as_borrowed();

// The IANA zone "Australia/Melbourne" is the BCP-47 zone "aumel":
assert_eq!(
    mapper.iana_to_bcp47("Australia/Melbourne"),
    Some("aumel".parse().unwrap())
);

// Lookup is ASCII-case-insensitive:
assert_eq!(
    mapper.iana_to_bcp47("australia/melbourne"),
    Some("aumel".parse().unwrap())
);

// The IANA zone "Australia/Victoria" is an alias:
assert_eq!(
    mapper.iana_to_bcp47("Australia/Victoria"),
    Some("aumel".parse().unwrap())
);

// We can recover the canonical identifier from the mapper:
assert_eq!(
    mapper.canonicalize_iana("Australia/Victoria").unwrap().0,
    "Australia/Melbourne"
);

Fields§

§data: DataPayload<IanaToBcp47MapV2Marker>

Implementations§

source§

impl TimeZoneIdMapper

source

pub fn new() -> Self

Creates a new TimeZoneIdMapper using compiled data.

See TimeZoneIdMapper for an example.

Enabled with the compiled_data Cargo feature.

📚 Help choosing a constructor

source

pub fn try_new_with_any_provider( provider: &(impl AnyProvider + ?Sized), ) -> Result<Self, DataError>

A version of Self::new that uses custom data provided by an AnyProvider.

📚 Help choosing a constructor

source

pub fn try_new_unstable<P>(provider: &P) -> Result<Self, DataError>

A version of Self::new 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 as_borrowed(&self) -> TimeZoneIdMapperBorrowed<'_>

Returns a borrowed version of the mapper that can be queried.

This avoids a small potential indirection cost when querying the mapper.

Trait Implementations§

source§

impl AsRef<TimeZoneIdMapper> for TimeZoneIdMapper

source§

fn as_ref(&self) -> &TimeZoneIdMapper

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl Clone for TimeZoneIdMapper

source§

fn clone(&self) -> TimeZoneIdMapper

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 Debug for TimeZoneIdMapper

source§

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

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

impl Default for TimeZoneIdMapper

source§

fn default() -> Self

Returns the “default value” for a type. 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> ToOwned for T
where T: Clone,

§

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

§

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

§

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