Struct icu_timezone::ids::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
impl TimeZoneIdMapper
sourcepub fn new() -> Self
pub fn new() -> Self
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_unstable<P>(provider: &P) -> Result<Self, DataError>
pub fn try_new_unstable<P>(provider: &P) -> Result<Self, DataError>
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 moresource§impl Debug for TimeZoneIdMapper
impl Debug for TimeZoneIdMapper
Auto 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
)