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
impl CustomTimeZone
sourcepub const fn new_with_offset(offset: UtcOffset) -> Self
pub const fn new_with_offset(offset: UtcOffset) -> Self
Creates a new CustomTimeZone
with the given UTC offset.
sourcepub const fn new_with_bcp47_id(time_zone_id: TimeZoneBcp47Id) -> Self
pub const fn new_with_bcp47_id(time_zone_id: TimeZoneBcp47Id) -> Self
Creates a new CustomTimeZone
with a given BCP47 time zone identifier.
sourcepub const fn unknown() -> Self
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.
sourcepub const fn from_parts(
offset_eighths_of_hour: i8,
time_zone_id: TinyAsciiStr<8>,
metazone_id: TinyAsciiStr<4>,
zone_variant: TinyAsciiStr<2>,
) -> Self
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.
sourcepub const fn utc() -> Self
pub const fn utc() -> Self
Creates a new CustomTimeZone
for the UTC time zone.
sourcepub fn from_str(s: &str) -> Self
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));
sourcepub fn from_utf8(code_units: &[u8]) -> Self
pub fn from_utf8(code_units: &[u8]) -> Self
See Self::from_str
sourcepub fn maybe_calculate_metazone(
&mut self,
metazone_calculator: &MetazoneCalculator,
local_datetime: &DateTime<Iso>,
) -> &mut Self
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"))));
sourcepub fn maybe_calculate_zone_variant(
&mut self,
zone_offset_calculator: &ZoneOffsetCalculator,
local_datetime: &DateTime<Iso>,
) -> &mut Self
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
impl Clone for CustomTimeZone
source§fn clone(&self) -> CustomTimeZone
fn clone(&self) -> CustomTimeZone
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for CustomTimeZone
impl Debug for CustomTimeZone
source§impl FromStr for CustomTimeZone
impl FromStr for CustomTimeZone
source§impl PartialEq for CustomTimeZone
impl PartialEq for CustomTimeZone
source§fn eq(&self, other: &CustomTimeZone) -> bool
fn eq(&self, other: &CustomTimeZone) -> bool
self
and other
values to be equal, and is used
by ==
.impl Copy for CustomTimeZone
impl Eq for CustomTimeZone
impl StructuralPartialEq for CustomTimeZone
Auto Trait Implementations§
impl Freeze for CustomTimeZone
impl RefUnwindSafe for CustomTimeZone
impl Send for CustomTimeZone
impl Sync for CustomTimeZone
impl Unpin for CustomTimeZone
impl UnwindSafe for CustomTimeZone
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> 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