Struct icu_timezone::TimeZoneIdMapperBorrowed
source · pub struct TimeZoneIdMapperBorrowed<'a> { /* private fields */ }
Expand description
A borrowed wrapper around the time zone ID mapper, returned by
TimeZoneIdMapper::as_borrowed()
. More efficient to query.
Implementations§
source§impl TimeZoneIdMapperBorrowed<'static>
impl TimeZoneIdMapperBorrowed<'static>
sourcepub fn new() -> Self
pub fn new() -> Self
Creates a new TimeZoneIdMapperBorrowed
using compiled data.
See TimeZoneIdMapperBorrowed
for an example.
✨ Enabled with the compiled_data
Cargo feature.
sourcepub fn static_to_owned(&self) -> TimeZoneIdMapper
pub fn static_to_owned(&self) -> TimeZoneIdMapper
Cheaply converts a [TimeZoneIdMapperBorrowed<'static>
] into a TimeZoneIdMapper
.
Note: Due to branching and indirection, using TimeZoneIdMapper
might inhibit some
compile-time optimizations that are possible with TimeZoneIdMapperBorrowed
.
source§impl TimeZoneIdMapperBorrowed<'_>
impl TimeZoneIdMapperBorrowed<'_>
sourcepub fn iana_to_bcp47(&self, iana_id: &str) -> TimeZoneBcp47Id
pub fn iana_to_bcp47(&self, iana_id: &str) -> TimeZoneBcp47Id
Gets the BCP-47 time zone ID from an IANA time zone ID with a case-insensitive lookup.
Returns TimeZoneBcp47Id::unknown()
if the IANA ID is not found.
§Examples
use icu_timezone::TimeZoneBcp47Id;
use icu_timezone::TimeZoneIdMapper;
let mapper = TimeZoneIdMapper::new();
let result = mapper.iana_to_bcp47("Asia/CALCUTTA");
assert_eq!(*result, "inccu");
// Unknown IANA time zone ID:
assert_eq!(
mapper.iana_to_bcp47("America/San_Francisco"),
TimeZoneBcp47Id::unknown()
);
sourcepub fn iana_bytes_to_bcp47(&self, iana_id: &[u8]) -> TimeZoneBcp47Id
pub fn iana_bytes_to_bcp47(&self, iana_id: &[u8]) -> TimeZoneBcp47Id
Same as Self::iana_to_bcp47()
but works with potentially ill-formed UTF-8.
sourcepub fn normalize_iana<'s>(
&self,
iana_id: &'s str,
) -> Option<(Cow<'s, str>, TimeZoneBcp47Id)>
pub fn normalize_iana<'s>( &self, iana_id: &'s str, ) -> Option<(Cow<'s, str>, TimeZoneBcp47Id)>
Normalizes the syntax of an IANA time zone ID.
Also returns the BCP-47 time zone ID.
Returns None
if the IANA ID is not found.
§Examples
use icu_timezone::TimeZoneBcp47Id;
use icu_timezone::TimeZoneIdMapper;
use std::borrow::Cow;
let mapper = TimeZoneIdMapper::new();
let result = mapper.normalize_iana("Asia/CALCUTTA").unwrap();
assert_eq!(result.0, "Asia/Calcutta");
assert!(matches!(result.0, Cow::Owned(_)));
assert_eq!(*result.1, "inccu");
// Borrows when able:
let result = mapper.normalize_iana("America/Chicago").unwrap();
assert_eq!(result.0, "America/Chicago");
assert!(matches!(result.0, Cow::Borrowed(_)));
// Unknown IANA time zone ID:
assert_eq!(mapper.normalize_iana("America/San_Francisco"), None);
sourcepub fn canonicalize_iana<'s>(
&self,
iana_id: &'s str,
) -> Option<(Cow<'s, str>, TimeZoneBcp47Id)>
pub fn canonicalize_iana<'s>( &self, iana_id: &'s str, ) -> Option<(Cow<'s, str>, TimeZoneBcp47Id)>
Returns the canonical, normalized identifier of the given IANA time zone.
Also returns the BCP-47 time zone ID.
Returns None
if the IANA ID is not found.
§Examples
use icu_timezone::TimeZoneBcp47Id;
use icu_timezone::TimeZoneIdMapper;
use std::borrow::Cow;
let mapper = TimeZoneIdMapper::new();
let result = mapper.canonicalize_iana("Asia/CALCUTTA").unwrap();
assert_eq!(result.0, "Asia/Kolkata");
assert!(matches!(result.0, Cow::Owned(_)));
assert_eq!(*result.1, "inccu");
// Borrows when able:
let result = mapper.canonicalize_iana("America/Chicago").unwrap();
assert_eq!(result.0, "America/Chicago");
assert!(matches!(result.0, Cow::Borrowed(_)));
// Unknown IANA time zone ID:
assert_eq!(mapper.canonicalize_iana("America/San_Francisco"), None);
sourcepub fn find_canonical_iana_from_bcp47(
&self,
bcp47_id: TimeZoneBcp47Id,
) -> Option<String>
pub fn find_canonical_iana_from_bcp47( &self, bcp47_id: TimeZoneBcp47Id, ) -> Option<String>
Returns the canonical, normalized IANA ID of the given BCP-47 ID.
This function performs a linear search over all IANA IDs. If this is problematic, consider one of the following functions instead:
TimeZoneIdMapperBorrowed::canonicalize_iana()
is faster if you have an IANA ID.TimeZoneIdMapperWithFastCanonicalizationBorrowed::canonical_iana_from_bcp47()
is faster, but it requires loading additional data (seeTimeZoneIdMapperWithFastCanonicalization
).
Returns None
if the BCP-47 ID is not found.
§Examples
use icu_timezone::TimeZoneBcp47Id;
use icu_timezone::TimeZoneIdMapper;
use std::borrow::Cow;
use tinystr::tinystr;
let mapper = TimeZoneIdMapper::new();
let bcp47_id = TimeZoneBcp47Id(tinystr!(8, "inccu"));
let result = mapper.find_canonical_iana_from_bcp47(bcp47_id).unwrap();
assert_eq!(result, "Asia/Kolkata");
// Unknown BCP-47 time zone ID:
let bcp47_id = TimeZoneBcp47Id(tinystr!(8, "ussfo"));
assert_eq!(mapper.find_canonical_iana_from_bcp47(bcp47_id), None);
sourcepub fn iter_bcp47(&self) -> TimeZoneBcp47Iter<'_> ⓘ
pub fn iter_bcp47(&self) -> TimeZoneBcp47Iter<'_> ⓘ
Returns an iterator over BCP-47 time zone identifiers in alphabetical order.
To iterate over canonical IANA time zone IDs, use
TimeZoneIdMapperWithFastCanonicalizationBorrowed::iter_canonical_iana()
.
§Examples
use icu::timezone::TimeZoneIdMapper;
let ids = TimeZoneIdMapper::new()
.iter_bcp47()
.skip(30)
.take(5)
.map(|id| id.to_string())
.collect::<Vec<_>>();
assert_eq!(
ids,
&[
"arush",
"asppg",
"atvie",
"auadl",
"aubhq",
]
);
Trait Implementations§
source§impl<'a> Clone for TimeZoneIdMapperBorrowed<'a>
impl<'a> Clone for TimeZoneIdMapperBorrowed<'a>
source§fn clone(&self) -> TimeZoneIdMapperBorrowed<'a>
fn clone(&self) -> TimeZoneIdMapperBorrowed<'a>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'a> Debug for TimeZoneIdMapperBorrowed<'a>
impl<'a> Debug for TimeZoneIdMapperBorrowed<'a>
source§impl Default for TimeZoneIdMapperBorrowed<'static>
impl Default for TimeZoneIdMapperBorrowed<'static>
impl<'a> Copy for TimeZoneIdMapperBorrowed<'a>
Auto Trait Implementations§
impl<'a> Freeze for TimeZoneIdMapperBorrowed<'a>
impl<'a> RefUnwindSafe for TimeZoneIdMapperBorrowed<'a>
impl<'a> Send for TimeZoneIdMapperBorrowed<'a>
impl<'a> Sync for TimeZoneIdMapperBorrowed<'a>
impl<'a> Unpin for TimeZoneIdMapperBorrowed<'a>
impl<'a> UnwindSafe for TimeZoneIdMapperBorrowed<'a>
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