Struct icu_timezone::CustomTimeZone
source · 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
impl CustomTimeZone
sourcepub const fn new_with_offset(gmt_offset: GmtOffset) -> Self
pub const fn new_with_offset(gmt_offset: GmtOffset) -> Self
Creates a new CustomTimeZone
with the given GMT offset.
sourcepub const fn new_empty() -> Self
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.
sourcepub const fn utc() -> Self
pub const fn utc() -> Self
Creates a new CustomTimeZone
with the GMT offset set to UTC.
All other fields are left empty.
sourcepub fn try_from_bytes(bytes: &[u8]) -> Result<Self, TimeZoneError>
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;
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));
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
Overwrite the metazone id in MockTimeZone.
§Examples
use icu::calendar::DateTime;
use icu::timezone::provider::{MetazoneId, TimeZoneBcp47Id};
use icu::timezone::CustomTimeZone;
use icu::timezone::MetazoneCalculator;
use tinystr::tinystr;
let mzc = MetazoneCalculator::new();
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
impl Debug for CustomTimeZone
source§impl FromStr for CustomTimeZone
impl FromStr for CustomTimeZone
source§fn from_str(input: &str) -> Result<Self, Self::Err>
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));