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
impl TimeZone
pub fn from_iana_name(name: &str) -> Option<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
impl TimeZone
pub fn parse(input: &str) -> Result<TimeZone, ParseError>
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 withParseError::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 exampleUTC, is rejected withParseError::MissingOffset. - A rule is
Mm.w.d[/time],Jn[/time]orn[/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 fn standard_abbreviation(&self) -> &str
pub fn standard_abbreviation(&self) -> &str
The abbreviation of the standard offset, for example CET.
pub fn daylight_saving_abbreviation(&self) -> Option<&str>
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
pub fn standard_offset_seconds(&self) -> i32
The standard offset in seconds east of UTC.
pub fn daylight_saving_offset_seconds(&self) -> Option<i32>
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
pub fn has_daylight_saving(&self) -> bool
Whether the zone has a daylight saving period.
pub fn start_rule(&self) -> Rule
pub fn start_rule(&self) -> Rule
The rule that starts the daylight saving period.
pub fn end_rule(&self) -> Rule
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
pub fn is_daylight_saving_at(&self, utc_seconds: i64) -> bool
Whether daylight saving time applies at a UTC instant.
pub fn from_utc(&self, utc_seconds: i64) -> Local<'_>
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
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.