Skip to main content

TimeZone

Struct TimeZone 

pub struct TimeZone { /* private fields */ }
Expand description

A parsed POSIX TZ string.

The value is Copy, owns its two abbreviations in fixed-size buffers and stores the two 32-bit [Rule] values and the two offsets inline. It is 32 bytes on every target (size_of::<TimeZone>() == 32) and performs no allocation, so a TZ string read from settings can be parsed once and the value kept by value.

Offsets are seconds east of UTC, so Europe/Paris has a standard offset of 3600 and a daylight saving offset of 7200.

Implementations§

§

impl TimeZone

pub fn from_iana_name(name: &str) -> Option<TimeZone>

Parse the POSIX TZ string of an IANA zone name, for example Europe/Paris. Returns None when the table does not have the name.

Every string in the generated table parses, so the parse error path is not reachable here; it is still handled to keep the function total.

§

impl TimeZone

pub fn parse(input: &str) -> Result<TimeZone, ParseError>

Parse a POSIX TZ string, for example CET-1CEST,M3.5.0,M10.5.0/3 for Europe/Paris.

The format is the TZ format of POSIX tzset: std offset [dst [offset] [,rule,rule]].

  • A name is either at least three letters (CET) or quoted in angle brackets with at least three ASCII alphanumeric characters, + or - (<+03>). A name longer than 7 bytes is rejected with ParseError::NameTooLong.
  • An offset is [+-]hh[:mm[:ss]] of at most 24 hours; minutes and seconds are 0 to 59. A bare name without an offset, for example UTC, is rejected with ParseError::MissingOffset.
  • A rule is Mm.w.d[/time], Jn[/time] or n[/time]. A rule time is a signed [+-]hh[:mm[:ss]] of at most 167 hours (POSIX 2024), with minutes and seconds of 0 to 59, and it defaults to 02:00:00.
  • A daylight saving name without rules uses the United States rules M3.2.0/2,M11.1.0/2, and a daylight saving offset without a value is one hour ahead of the standard offset.

Invalid strings return a ParseError that says which part is wrong.

pub const fn utc() -> TimeZone

UTC (UTC0) as a time zone without parsing at run time.

pub fn standard_abbreviation(&self) -> &str

The abbreviation of the standard offset, for example CET.

pub fn daylight_saving_abbreviation(&self) -> Option<&str>

The abbreviation of the daylight saving offset, if the zone has one.

pub fn standard_offset_seconds(&self) -> i32

The standard offset in seconds east of UTC.

pub fn daylight_saving_offset_seconds(&self) -> Option<i32>

The daylight saving offset in seconds east of UTC, if the zone has one.

pub fn has_daylight_saving(&self) -> bool

Whether the zone has a daylight saving period.

pub fn start_rule(&self) -> Rule

The rule that starts the daylight saving period.

pub fn end_rule(&self) -> Rule

The rule that ends the daylight saving period.

pub fn is_daylight_saving_at(&self, utc_seconds: i64) -> bool

Whether daylight saving time applies at a UTC instant.

pub fn offset_at(&self, utc_seconds: i64) -> i32

The offset in seconds east of UTC at a UTC instant.

pub fn from_utc(&self, utc_seconds: i64) -> Local<'_>

Convert a UTC instant, in seconds since the Unix epoch, to local time.

The result holds the local date and time, the offset in seconds east of UTC, the zone abbreviation and whether daylight saving time applies. The conversion is total for every i64 second count.

pub fn to_utc(&self, local: DateTime) -> LocalToUtc

Convert a local date and time to UTC, in seconds since the Unix epoch.

The result is explicit about repeated and missing local times: [LocalToUtc::Ambiguous] for a local time that happens twice (the repeated hour in autumn) and [LocalToUtc::Nonexistent] for a local time that never happens (the skipped hour in spring).

The input fields are not checked; see [DateTime::is_valid]. A local time whose instants all fall outside the range that i64 seconds can hold also returns [LocalToUtc::Nonexistent], because no UTC instant can represent it.

Trait Implementations§

§

impl Clone for TimeZone

§

fn clone(&self) -> TimeZone

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for TimeZone

§

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

Formats the value using the given formatter. Read more
§

impl PartialEq for TimeZone

§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · 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.
§

impl Copy for TimeZone

§

impl Eq for TimeZone

§

impl StructuralPartialEq for TimeZone

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