pub struct TimeZone { /* private fields */ }Expand description
A time zone: UTC, a fixed offset, or a named IANA zone.
Values are cheap to copy. Named zones are backed by the embedded database
(or, with the timezone-system feature, by data read from the system) and
never require allocation to use.
§Invariants
A value can only be constructed through TimeZone::UTC,
TimeZone::fixed, TimeZone::named or TimeZone::system, so:
- a fixed offset is always validated to lie within
-25:59:59 ..= 25:59:59, and - a named zone always carries rule data that parsed successfully; a
lookup or a read of malformed data fails with
ErrorKind::UnknownTimeZoneorErrorKind::UnknownSystemTimeZoneinstead of inventing offsets.
Implementations§
Source§impl TimeZone
impl TimeZone
Sourcepub const fn fixed(offset: i32) -> Result<TimeZone, Error>
pub const fn fixed(offset: i32) -> Result<TimeZone, Error>
Returns a fixed-offset time zone.
§Errors
Returns an error if offset is outside -25:59:59 ..= 25:59:59.
Sourcepub fn named(name: &str) -> Result<TimeZone, Error>
pub fn named(name: &str) -> Result<TimeZone, Error>
Looks up an IANA time zone by name.
"UTC", "GMT", "Etc/UTC" and the other UTC names in the database
are returned as ordinary named zones, so their canonical name,
abbreviation and RFC 9557 annotation are preserved. The bare "z"
convenience alias resolves to TimeZone::UTC. "local" resolves to
the system time zone when the timezone-system feature is enabled.
Matching is case-insensitive.
§Errors
Returns ErrorKind::UnknownTimeZone
if name is not present in the database.
Sourcepub const fn name(&self) -> Option<&'static str>
pub const fn name(&self) -> Option<&'static str>
Returns the canonical name of this time zone, if it has one.
Sourcepub fn ambiguity(
self,
year: i32,
month: u8,
day: u8,
hour: u8,
minute: u8,
second: u8,
) -> Result<Ambiguity, Error>
pub fn ambiguity( self, year: i32, month: u8, day: u8, hour: u8, minute: u8, second: u8, ) -> Result<Ambiguity, Error>
Classifies how the given civil time maps to offsets in this zone.
§Errors
Returns an error if a field is out of range.
Sourcepub fn system() -> Result<TimeZone, Error>
pub fn system() -> Result<TimeZone, Error>
Determines the system’s local time zone.
This reads the TZ environment variable, then /etc/localtime, then
the platform zoneinfo directory. It never calls localtime_r. The
time zone data that a detected value uses must parse; otherwise the
lookup fails. The detected zone is determined once per process and
cached, so a later change of TZ (or of /etc/localtime) is not
observed, and the process leaks one small allocation of detected data
at most.
§Errors
Returns ErrorKind::UnknownSystemTimeZone
when no system zone can be determined, or
ErrorKind::UnknownTimeZone when
TZ names a zone that cannot be found.