Struct icu_timezone::CustomTimeZone

source ·
pub struct CustomTimeZone {
    pub offset: Option<UtcOffset>,
    pub time_zone_id: TimeZoneBcp47Id,
    pub metazone_id: Option<Option<MetazoneId>>,
    pub zone_variant: Option<ZoneVariant>,
}
Expand description

A utility type that can hold time zone information.

The UTC offset is used as a final fallback for formatting. The other three fields are used for more human-friendly rendering of the time zone.

This type does not enforce that the four fields are consistent with each other. If they do not represent a real time zone, unexpected results when formatting may occur.

§Examples

use icu::timezone::CustomTimeZone;

let tz1 = CustomTimeZone::utc();

let tz2: CustomTimeZone =
    "+05:00".parse().expect("Failed to parse a time zone.");

Fields§

§offset: Option<UtcOffset>

The UTC offset.

§time_zone_id: TimeZoneBcp47Id

The BCP47 time-zone identifier

§metazone_id: Option<Option<MetazoneId>>

The CLDR metazone identifier

This is Some(None) if the metazone has been resolved, but the time zone does not have a metazone.

§zone_variant: Option<ZoneVariant>

The time variant e.g. daylight or standard

Implementations§

source§

impl CustomTimeZone

source

pub const fn new_with_offset(offset: UtcOffset) -> Self

Creates a new CustomTimeZone with the given UTC offset.

source

pub const fn new_with_bcp47_id(time_zone_id: TimeZoneBcp47Id) -> Self

Creates a new CustomTimeZone with a given BCP47 time zone identifier.

source

pub const fn unknown() -> Self

Creates a time zone with no information.

One or more fields must be specified before this time zone is usable.

source

pub const fn from_parts( offset_eighths_of_hour: i8, time_zone_id: TinyAsciiStr<8>, metazone_id: TinyAsciiStr<4>, zone_variant: TinyAsciiStr<2>, ) -> Self

Creates a time zone infallibly from raw parts.

source

pub const fn utc() -> Self

Creates a new CustomTimeZone for the UTC time zone.

source

pub fn from_str(s: &str) -> Self

Parse a CustomTimeZone from a UTF-8 string representing a UTC offset or an IANA time zone identifier.

This is a convenience constructor that uses compiled data. For a custom data provider, use UtcOffset or TimeZoneIdMapper directly.

To parse from an IXDTF string, use CustomZonedDateTime::try_iso_from_str.

Enabled with the compiled_data Cargo feature.

§Examples
use icu::timezone::CustomTimeZone;
use icu::timezone::UtcOffset;

let tz0: CustomTimeZone = CustomTimeZone::from_str("Z");
let tz1: CustomTimeZone = CustomTimeZone::from_str("+02");
let tz2: CustomTimeZone = CustomTimeZone::from_str("-0230");
let tz3: CustomTimeZone = CustomTimeZone::from_str("+02:30");

assert_eq!(tz0.offset.map(UtcOffset::to_seconds), Some(0));
assert_eq!(tz1.offset.map(UtcOffset::to_seconds), Some(7200));
assert_eq!(tz2.offset.map(UtcOffset::to_seconds), Some(-9000));
assert_eq!(tz3.offset.map(UtcOffset::to_seconds), Some(9000));
source

pub fn from_utf8(code_units: &[u8]) -> Self

source

pub fn maybe_calculate_metazone( &mut self, metazone_calculator: &MetazoneCalculator, local_datetime: &DateTime<Iso>, ) -> &mut Self

Infer the metazone ID.

§Examples
use icu::calendar::DateTime;
use icu::timezone::MetazoneId;
use icu::timezone::TimeZoneBcp47Id;
use icu::timezone::CustomTimeZone;
use icu::timezone::MetazoneCalculator;
use tinystr::tinystr;

let mzc = MetazoneCalculator::new();
let mut tz = CustomTimeZone::new_with_bcp47_id(TimeZoneBcp47Id(tinystr!(8, "gugum")));
tz.maybe_calculate_metazone(
    &mzc,
    &DateTime::try_new_iso_datetime(1971, 10, 31, 2, 0, 0).unwrap(),
);
assert_eq!(tz.metazone_id.unwrap(), Some(MetazoneId(tinystr!(4, "guam"))));
source

pub fn maybe_calculate_zone_variant( &mut self, zone_offset_calculator: &ZoneOffsetCalculator, local_datetime: &DateTime<Iso>, ) -> &mut Self

Infer the zone variant.

§Examples
use icu::calendar::DateTime;
use icu::timezone::ZoneVariant;
use icu::timezone::TimeZoneBcp47Id;
use icu::timezone::CustomTimeZone;
use icu::timezone::ZoneOffsetCalculator;
use tinystr::tinystr;

let zoc = ZoneOffsetCalculator::new();
let mut tz = CustomTimeZone {
    offset: Some("+10".parse().expect("Failed to parse a UTC offset.")),
    time_zone_id: TimeZoneBcp47Id(tinystr!(8, "gugum")),
    metazone_id: None,
    zone_variant: None,
};
tz.maybe_calculate_zone_variant(
    &zoc,
    &DateTime::try_new_iso_datetime(1971, 10, 31, 2, 0, 0).unwrap(),
);
assert_eq!(tz.zone_variant, Some(ZoneVariant::standard()));

Trait Implementations§

source§

impl Clone for CustomTimeZone

source§

fn clone(&self) -> CustomTimeZone

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 CustomTimeZone

source§

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

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

impl FromStr for CustomTimeZone

§

type Err = Infallible

The associated error which can be returned from parsing.
source§

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
source§

impl PartialEq for CustomTimeZone

source§

fn eq(&self, other: &CustomTimeZone) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for CustomTimeZone

source§

impl Eq for CustomTimeZone

source§

impl StructuralPartialEq for CustomTimeZone

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> IntoEither for T

source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
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.
§

impl<T> ErasedDestructor for T
where T: 'static,

§

impl<T> MaybeSendSync for T
where T: Send + Sync,