Struct icu_timezone::TimeZoneIdMapperBorrowed

source ·
pub struct TimeZoneIdMapperBorrowed<'a> {
    data: &'a IanaToBcp47MapV2<'a>,
}
Expand description

A borrowed wrapper around the time zone ID mapper, returned by TimeZoneIdMapper::as_borrowed(). More efficient to query.

Fields§

§data: &'a IanaToBcp47MapV2<'a>

Implementations§

source§

impl<'a> TimeZoneIdMapperBorrowed<'a>

source

pub fn iana_to_bcp47(&self, iana_id: &str) -> Option<TimeZoneBcp47Id>

Gets the BCP-47 time zone ID from an IANA time zone ID with a case-insensitive lookup.

Returns None if the IANA ID is not found.

§Examples
use icu_timezone::TimeZoneBcp47Id;
use icu_timezone::TimeZoneIdMapper;

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

let result = mapper.iana_to_bcp47("Asia/CALCUTTA").unwrap();

assert_eq!(*result, "inccu");

// Unknown IANA time zone ID:
assert_eq!(mapper.iana_to_bcp47("America/San_Francisco"), None);
source

pub fn iana_bytes_to_bcp47(&self, iana_id: &[u8]) -> Option<TimeZoneBcp47Id>

Same as Self::iana_to_bcp47() but works with potentially ill-formed UTF-8.

source

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 mapper = mapper.as_borrowed();

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);
source

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 mapper = mapper.as_borrowed();

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);
source

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:

  1. TimeZoneIdMapperBorrowed::canonicalize_iana() is faster if you have an IANA ID.
  2. TimeZoneIdMapperWithFastCanonicalizationBorrowed::canonical_iana_from_bcp47() is faster, but it requires loading additional data (see TimeZoneIdMapperWithFastCanonicalization).

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 mapper = mapper.as_borrowed();

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);
source

fn iana_lookup_quick(&self, iana_id: impl AsRef<[u8]>) -> Option<IanaTrieValue>

Queries the data for iana_id without recording the normalized string. This is a fast, no-alloc lookup.

source

fn iana_lookup_with_normalization<'l, 's>( &'l self, iana_id: &'s str, cursor_fn: impl FnMut(&ZeroAsciiIgnoreCaseTrieCursor<'l>), ) -> Option<(IanaTrieValue, Cow<'s, str>)>

Queries the data for iana_id while keeping track of the normalized string. This is a fast lookup, but it may require allocating memory.

Performs a reverse lookup by walking the trie with an optional start position. This is not a fast operation since it requires a linear search.

Trait Implementations§

source§

impl<'a> Clone for TimeZoneIdMapperBorrowed<'a>

source§

fn clone(&self) -> TimeZoneIdMapperBorrowed<'a>

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> Debug for TimeZoneIdMapperBorrowed<'a>

source§

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

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

impl<'a> Copy for TimeZoneIdMapperBorrowed<'a>

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