Struct TimeZoneInfo

Source
pub struct TimeZoneInfo<Model: TimeZoneModel> { /* private fields */ }
Expand description

A utility type that can hold time zone information.

The primary definition of this type is in the icu_time crate. Other ICU4X crates re-export it for convenience.

See the docs on zone for more information.

§Examples

use icu::calendar::Date;
use icu::locale::subtags::subtag;
use icu::time::zone::IanaParser;
use icu::time::zone::TimeZoneVariant;
use icu::time::DateTime;
use icu::time::Time;
use icu::time::TimeZone;

// Parse the IANA ID
let id = IanaParser::new().parse("America/Chicago");

// Alternatively, use the BCP47 ID directly
let id = TimeZone(subtag!("uschi"));

// Create a TimeZoneInfo<Base> by associating the ID with an offset
let time_zone = id.with_offset("-0600".parse().ok());

// Extend to a TimeZoneInfo<AtTime> by adding a local time
let time_zone_at_time = time_zone.at_date_time_iso(DateTime {
    date: Date::try_new_iso(2023, 12, 2).unwrap(),
    time: Time::start_of_day(),
});

// Extend to a TimeZoneInfo<Full> by adding a zone variant
let time_zone_with_variant =
    time_zone_at_time.with_variant(TimeZoneVariant::Standard);

Implementations§

Source§

impl<Model: TimeZoneModel> TimeZoneInfo<Model>

Source

pub fn id(self) -> TimeZone

The BCP47 time-zone identifier.

Source

pub fn offset(self) -> Option<UtcOffset>

The UTC offset, if known.

This field is not enforced to be consistent with the time zone id.

Source§

impl<Model> TimeZoneInfo<Model>
where Model: TimeZoneModel<ZoneNameTimestamp = ZoneNameTimestamp>,

Source

pub fn zone_name_timestamp(self) -> ZoneNameTimestamp

The time at which to interpret the time zone.

Source§

impl<Model> TimeZoneInfo<Model>
where Model: TimeZoneModel<TimeZoneVariant = TimeZoneVariant>,

Source

pub fn variant(self) -> TimeZoneVariant

The time variant e.g. daylight or standard, if known.

This field is not enforced to be consistent with the time zone id and offset.

Source§

impl TimeZoneInfo<Base>

Source

pub const fn unknown() -> Self

Creates a time zone info with no information.

Source

pub const fn utc() -> Self

Creates a new TimeZoneInfo for the UTC time zone.

Source

pub fn with_zone_name_timestamp( self, zone_name_timestamp: ZoneNameTimestamp, ) -> TimeZoneInfo<AtTime>

Sets the ZoneNameTimestamp field.

Source

pub fn at_date_time_iso(self, date_time: DateTime<Iso>) -> TimeZoneInfo<AtTime>

Sets the ZoneNameTimestamp to the given local datetime.

Source§

impl TimeZoneInfo<AtTime>

Source

pub const fn with_variant(self, variant: TimeZoneVariant) -> TimeZoneInfo<Full>

Sets a TimeZoneVariant on this time zone.

Source

pub fn infer_variant( self, calculator: VariantOffsetsCalculatorBorrowed<'_>, ) -> TimeZoneInfo<Full>

Sets the zone variant by calculating it using a VariantOffsetsCalculator.

If offset() is None, or if it doesn’t match either of the timezone’s standard or daylight offset around local_time(), the variant will be set to TimeZoneVariant::Standard and the time zone to TimeZone::UNKNOWN.

§Example
use icu::calendar::Date;
use icu::locale::subtags::subtag;
use icu::time::zone::TimeZoneVariant;
use icu::time::zone::VariantOffsetsCalculator;
use icu::time::DateTime;
use icu::time::Time;
use icu::time::TimeZone;

// Chicago at UTC-6
let info = TimeZone(subtag!("uschi"))
    .with_offset("-0600".parse().ok())
    .at_date_time_iso(DateTime {
        date: Date::try_new_iso(2023, 12, 2).unwrap(),
        time: Time::start_of_day(),
    })
    .infer_variant(VariantOffsetsCalculator::new());

assert_eq!(info.variant(), TimeZoneVariant::Standard);

// Chicago at at UTC-5
let info = TimeZone(subtag!("uschi"))
    .with_offset("-0500".parse().ok())
    .at_date_time_iso(DateTime {
        date: Date::try_new_iso(2023, 6, 2).unwrap(),
        time: Time::start_of_day(),
    })
    .infer_variant(VariantOffsetsCalculator::new());

assert_eq!(info.variant(), TimeZoneVariant::Daylight);

// Chicago at UTC-7
let info = TimeZone(subtag!("uschi"))
    .with_offset("-0700".parse().ok())
    .at_date_time_iso(DateTime {
        date: Date::try_new_iso(2023, 12, 2).unwrap(),
        time: Time::start_of_day(),
    })
    .infer_variant(VariantOffsetsCalculator::new());

// Whatever it is, it's not Chicago
assert_eq!(info.id(), TimeZone::UNKNOWN);
assert_eq!(info.variant(), TimeZoneVariant::Standard);

Trait Implementations§

Source§

impl<Model: TimeZoneModel> Clone for TimeZoneInfo<Model>

Source§

fn clone(&self) -> Self

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<Model: Debug + TimeZoneModel> Debug for TimeZoneInfo<Model>

Source§

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

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

impl<Model: PartialEq + TimeZoneModel> PartialEq for TimeZoneInfo<Model>

Source§

fn eq(&self, other: &TimeZoneInfo<Model>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<Model: TimeZoneModel> Copy for TimeZoneInfo<Model>

Source§

impl<Model: Eq + TimeZoneModel> Eq for TimeZoneInfo<Model>
where Model::ZoneNameTimestamp: Eq, Model::TimeZoneVariant: Eq,

Source§

impl<Model: TimeZoneModel> StructuralPartialEq for TimeZoneInfo<Model>

Auto Trait Implementations§

§

impl<Model> Freeze for TimeZoneInfo<Model>

§

impl<Model> RefUnwindSafe for TimeZoneInfo<Model>

§

impl<Model> Send for TimeZoneInfo<Model>

§

impl<Model> Sync for TimeZoneInfo<Model>

§

impl<Model> Unpin for TimeZoneInfo<Model>

§

impl<Model> UnwindSafe for TimeZoneInfo<Model>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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,

Source§

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>,

Source§

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>,

Source§

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,