Struct icu_timezone::TimeZoneIdMapper
source · pub struct TimeZoneIdMapper { /* private fields */ }
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::TimeZoneBcp47Id;
use icu::timezone::TimeZoneIdMapper;
use tinystr::tinystr;
let mapper = TimeZoneIdMapper::new();
// The IANA zone "Australia/Melbourne" is the BCP-47 zone "aumel":
assert_eq!(
mapper.iana_to_bcp47("Australia/Melbourne"),
TimeZoneBcp47Id(tinystr!(8, "aumel"))
);
// Lookup is ASCII-case-insensitive:
assert_eq!(
mapper.iana_to_bcp47("australia/melbourne"),
TimeZoneBcp47Id(tinystr!(8, "aumel"))
);
// The IANA zone "Australia/Victoria" is an alias:
assert_eq!(
mapper.iana_to_bcp47("Australia/Victoria"),
TimeZoneBcp47Id(tinystr!(8, "aumel"))
);
// The IANA zone "Australia/Boing_Boing" does not exist
// (maybe not *yet*), so it produces the special unknown
// timezone in order for this operation to be infallible:
assert_eq!(
mapper.iana_to_bcp47("Australia/Boing_Boing"),
TimeZoneBcp47Id::unknown()
);
// We can recover the canonical identifier from the mapper:
assert_eq!(
mapper.canonicalize_iana("Australia/Victoria").unwrap().0,
"Australia/Melbourne"
);
Implementations§
source§impl TimeZoneIdMapper
impl TimeZoneIdMapper
sourcepub fn new() -> TimeZoneIdMapperBorrowed<'static>
pub fn new() -> TimeZoneIdMapperBorrowed<'static>
Creates a new TimeZoneIdMapper
using compiled data.
See TimeZoneIdMapper
for an example.
✨ Enabled with the compiled_data
Cargo feature.
sourcepub fn try_new_with_any_provider(
provider: &(impl AnyProvider + ?Sized),
) -> Result<Self, DataError>
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
.
sourcepub fn try_new_with_buffer_provider(
provider: &(impl BufferProvider + ?Sized),
) -> Result<Self, DataError>
pub fn try_new_with_buffer_provider( provider: &(impl BufferProvider + ?Sized), ) -> Result<Self, DataError>
A version of [Self :: new
] that uses custom data provided by a BufferProvider
.
✨ Enabled with the serde
feature.
sourcepub fn try_new_unstable<P>(provider: &P) -> Result<Self, DataError>where
P: DataProvider<IanaToBcp47MapV3Marker> + ?Sized,
pub fn try_new_unstable<P>(provider: &P) -> Result<Self, DataError>where
P: DataProvider<IanaToBcp47MapV3Marker> + ?Sized,
A version of Self::new
that uses custom data provided by a DataProvider
.
sourcepub fn as_borrowed(&self) -> TimeZoneIdMapperBorrowed<'_>
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
impl AsRef<TimeZoneIdMapper> for TimeZoneIdMapper
source§fn as_ref(&self) -> &TimeZoneIdMapper
fn as_ref(&self) -> &TimeZoneIdMapper
source§impl Clone for TimeZoneIdMapper
impl Clone for TimeZoneIdMapper
source§fn clone(&self) -> TimeZoneIdMapper
fn clone(&self) -> TimeZoneIdMapper
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moreAuto Trait Implementations§
impl Freeze for TimeZoneIdMapper
impl RefUnwindSafe for TimeZoneIdMapper
impl Send for TimeZoneIdMapper
impl Sync for TimeZoneIdMapper
impl Unpin for TimeZoneIdMapper
impl UnwindSafe for TimeZoneIdMapper
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
source§unsafe fn clone_to_uninit(&self, dst: *mut T)
unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)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