Skip to main content

TimeZone

Struct TimeZone 

Source
#[repr(align(8))]
pub struct TimeZone { pub std_abbrev: Abbreviation, pub std_offset: Offset, pub dst: Option<Dst>, }
Expand description

A representation of a POSIX time zone transition rule.

POSIX time zones are limited in what they can express. Notably, they can’t handle historic time zone transitions. They generally can only handle present rules.

Note that the internals of this type are completely exposed to make writing static data with these types easier.

§On “reasonable” POSIX time zones

Jiff only supports “reasonable” POSIX time zones. A “reasonable” POSIX time zone is a POSIX time zone that has a DST transition rule when it has a DST time zone abbreviation. Without the transition rule, it isn’t possible to know when DST starts and stops.

POSIX technically allows a DST time zone abbreviation without a transition rule, but the behavior is literally unspecified. So Jiff just rejects them.

Note that if you’re confused as to why Jiff accepts TZ=EST5EDT (where EST5EDT is an example of an unreasonable POSIX time zone), that’s because Jiff rejects EST5EDT and instead attempts to use it as an IANA time zone identifier. And indeed, the IANA Time Zone Database contains an entry for EST5EDT (presumably for legacy reasons).

Also, we expect TZ strings parsed from IANA v2+ formatted tzfiles to also be reasonable or parsing fails. This also seems to be consistent with the GNU C Library’s treatment of the TZ variable: it only documents support for reasonable POSIX time zone strings.

Note that a V2 TZ string is precisely identical to a POSIX TZ environment variable string. A V3 TZ string however supports signed DST transition times, and hours in the range 0..=167. The V2 and V3 here reference how TZ strings are defined in the TZif format specified by RFC 9636. V2 is the original version of it straight from POSIX, where as V3+ corresponds to an extension added to V3 (and newer versions) of the TZif format. V3 is a superset of V2, so in practice, Jiff just permits V3 everywhere.

§Example

use jiff_core::{tz::{Offset, posix::TimeZone}, Timestamp};

let tz = TimeZone::parse("EST5EDT,M3.2.0,M11.1.0").unwrap();

let ts = Timestamp::from_second(1783100574).unwrap();
let offset = tz.to_offset(ts);
assert_eq!(offset, Offset::from_seconds(-4 * 60 * 60).unwrap());

// Around 6 months later, we should be out of DST.
let ts = Timestamp::from_second(ts.as_second() + 6 * 30 * 86400).unwrap();
let offset = tz.to_offset(ts);
assert_eq!(offset, Offset::from_seconds(-5 * 60 * 60).unwrap());

Fields§

§std_abbrev: Abbreviation

The abbreviation for standard time.

§std_offset: Offset

The offset for standard time.

§dst: Option<Dst>

Whether there is any daylight saving time for this POSIX time zone.

Implementations§

Source§

impl TimeZone

Source

pub fn parse<B: AsRef<[u8]>>(bytes: B) -> Result<TimeZone, ParseError>

Parse a POSIX TZ environment variable, assuming it’s a rule and not an implementation defined value, from the given byte string.

§Errors

This returns an error if the given byte string is not a valid POSIX time zone transition rule.

This also returns an error if, after parsing the POSIX time zone transition rule, there are still bytes remaining in the string given.

Source

pub fn parse_prefix<'b, B: AsRef<[u8]> + ?Sized>( bytes: &'b B, ) -> Result<(TimeZone, usize), ParseError>

Like TimeZone::parse, but parses a prefix of the input given. In addition to returning a TimeZone, this also returns the offset into bytes pointing at the beginning of any remaining unparsed input.

§Errors

This returns an error if the given byte string is not a valid POSIX time zone transition rule.

Source§

impl TimeZone

Source

pub fn to_offset(&self, timestamp: Timestamp) -> Offset

Returns the appropriate time zone offset to use for the given timestamp.

If you need information like whether the offset is in DST or not, or the time zone abbreviation, then use TimeZone::to_offset_info. But that API may be more expensive to use, so only use it if you need the additional data.

Source

pub fn to_offset_info(&self, timestamp: Timestamp) -> OffsetInfo

Returns the appropriate time zone offset to use for the given timestamp.

This also includes whether the offset returned should be considered to be “DST” or not, along with the time zone abbreviation (e.g., EST for standard time in New York, and EDT for DST in New York).

Source

pub fn to_ambiguous_timestamp(&self, dt: DateTime) -> AmbiguousTimestamp

Returns a possibly ambiguous timestamp for the given civil datetime.

The given datetime should correspond to the “wall” clock time of what humans use to tell time for this time zone.

Note that “ambiguous timestamp” is represented by the possible selection of offsets that could be applied to the given datetime. In general, it is only ambiguous around transitions to-and-from DST. The ambiguity can arise as a “fold” (when a particular wall clock time is repeated) or as a “gap” (when a particular wall clock time is skipped entirely).

Source

pub fn previous_transition(&self, timestamp: Timestamp) -> Option<Transition>

Returns the timestamp of the most recent time zone transition prior to the timestamp given. If one doesn’t exist, None is returned.

Source

pub fn next_transition(&self, timestamp: Timestamp) -> Option<Transition>

Returns the timestamp of the soonest time zone transition after the timestamp given. If one doesn’t exist, None is returned.

Trait Implementations§

Source§

impl Clone for TimeZone

Source§

fn clone(&self) -> Self

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
Source§

impl Debug for TimeZone

Source§

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

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

impl Eq for TimeZone

Source§

impl PartialEq for TimeZone

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

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 = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

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.