pub struct CustomTimeZone {
    pub gmt_offset: Option<GmtOffset>,
    pub time_zone_id: Option<TimeZoneBcp47Id>,
    pub metazone_id: Option<MetazoneId>,
    pub zone_variant: Option<ZoneVariant>,
}
Expand description

A utility type that can hold time zone information.

The GMT 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, GmtOffset};

let tz1 = CustomTimeZone {
    gmt_offset: Some(GmtOffset::default()),
    time_zone_id: None,
    metazone_id: None,
    zone_variant: None,
};

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

Fields§

§gmt_offset: Option<GmtOffset>

The GMT offset in seconds.

§time_zone_id: Option<TimeZoneBcp47Id>

The BCP47 time-zone identifier

§metazone_id: Option<MetazoneId>

The CLDR metazone identifier

§zone_variant: Option<ZoneVariant>

The time variant e.g. daylight or standard

Implementations§

source§

impl CustomTimeZone

source

pub const fn new_with_offset(gmt_offset: GmtOffset) -> Self

Creates a new CustomTimeZone with the given GMT offset.

source

pub const fn new_empty() -> 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 utc() -> Self

Creates a new CustomTimeZone with the GMT offset set to UTC.

All other fields are left empty.

source

pub fn try_from_bytes(bytes: &[u8]) -> Result<Self, TimeZoneError>

Parse a CustomTimeZone from a UTF-8 string representing a GMT Offset. See also GmtOffset.

Examples
use icu::timezone::CustomTimeZone;
use icu::timezone::GmtOffset;
use icu::timezone::TimeZoneError;

let tz0: CustomTimeZone = CustomTimeZone::try_from_bytes(b"Z")
    .expect("Failed to parse a time zone");
let tz1: CustomTimeZone = CustomTimeZone::try_from_bytes(b"+02")
    .expect("Failed to parse a time zone");
let tz2: CustomTimeZone = CustomTimeZone::try_from_bytes(b"-0230")
    .expect("Failed to parse a time zone");
let tz3: CustomTimeZone = CustomTimeZone::try_from_bytes(b"+02:30")
    .expect("Failed to parse a time zone");

assert_eq!(tz0.gmt_offset.map(GmtOffset::offset_seconds), Some(0));
assert_eq!(tz1.gmt_offset.map(GmtOffset::offset_seconds), Some(7200));
assert_eq!(tz2.gmt_offset.map(GmtOffset::offset_seconds), Some(-9000));
assert_eq!(tz3.gmt_offset.map(GmtOffset::offset_seconds), Some(9000));
source

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

Overwrite the metazone id in MockTimeZone.

Examples
use icu::timezone::provider::{MetazoneId, TimeZoneBcp47Id};
use icu::timezone::CustomTimeZone;
use icu::timezone::GmtOffset;
use icu::timezone::MetazoneCalculator;
use icu_calendar::DateTime;
use icu_locid::locale;
use tinystr::tinystr;

let mzc = MetazoneCalculator::try_new_unstable(&icu_testdata::unstable())
    .expect("data exists");
let mut tz = CustomTimeZone {
    gmt_offset: Some("+11".parse().expect("Failed to parse a GMT offset.")),
    time_zone_id: Some(TimeZoneBcp47Id(tinystr!(8, "gugum"))),
    metazone_id: None,
    zone_variant: None,
};
tz.maybe_calculate_metazone(
    &mzc,
    &DateTime::try_new_iso_datetime(1971, 10, 31, 2, 0, 0).unwrap(),
);
assert_eq!(tz.metazone_id, Some(MetazoneId(tinystr!(4, "guam"))));

Trait Implementations§

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

source§

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

Parse a CustomTimeZone from a string.

This utility is for easily creating time zones, not a complete robust solution.

The offset must range from GMT-12 to GMT+14. The string must be an ISO-8601 time zone designator: e.g. Z e.g. +05 e.g. +0500 e.g. +05:00

Examples
use icu::timezone::CustomTimeZone;
use icu::timezone::GmtOffset;

let tz0: CustomTimeZone = "Z".parse().expect("Failed to parse a time zone");
let tz1: CustomTimeZone =
    "+02".parse().expect("Failed to parse a time zone");
let tz2: CustomTimeZone =
    "-0230".parse().expect("Failed to parse a time zone");
let tz3: CustomTimeZone =
    "+02:30".parse().expect("Failed to parse a time zone");

assert_eq!(tz0.gmt_offset.map(GmtOffset::offset_seconds), Some(0));
assert_eq!(tz1.gmt_offset.map(GmtOffset::offset_seconds), Some(7200));
assert_eq!(tz2.gmt_offset.map(GmtOffset::offset_seconds), Some(-9000));
assert_eq!(tz3.gmt_offset.map(GmtOffset::offset_seconds), Some(9000));
§

type Err = TimeZoneError

The associated error which can be returned from parsing.

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

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

source§

impl<T> MaybeSendSync for T