Struct TimeZoneOffsetInfo
pub struct TimeZoneOffsetInfo<'t> { /* private fields */ }
dep_jiff
and alloc
only.Expand description
An offset along with DST status and a time zone abbreviation.
This information can be computed from a TimeZone
given a Timestamp
via TimeZone::to_offset_info
.
Generally, the extra information associated with the offset is not commonly needed. And indeed, inspecting the daylight saving time status of a particular instant in a time zone usually leads to bugs. For example, not all time zone transitions are the result of daylight saving time. Some are the result of permanent changes to the standard UTC offset of a region.
This information is available via an API distinct from
TimeZone::to_offset
because it is not commonly needed and because it
can sometimes be more expensive to compute.
The main use case for daylight saving time status or time zone
abbreviations is for formatting datetimes in an end user’s locale. If you
want this, consider using the icu
crate via jiff-icu
.
The lifetime parameter 't
corresponds to the lifetime of the TimeZone
that this info was extracted from.
§Example
use jiff::{tz::{self, Dst, TimeZone}, Timestamp};
let tz = TimeZone::get("America/New_York")?;
// A timestamp in DST in New York.
let ts = Timestamp::from_second(1_720_493_204)?;
let info = tz.to_offset_info(ts);
assert_eq!(info.offset(), tz::offset(-4));
assert_eq!(info.dst(), Dst::Yes);
assert_eq!(info.abbreviation(), "EDT");
assert_eq!(
info.offset().to_datetime(ts).to_string(),
"2024-07-08T22:46:44",
);
// A timestamp *not* in DST in New York.
let ts = Timestamp::from_second(1_704_941_204)?;
let info = tz.to_offset_info(ts);
assert_eq!(info.offset(), tz::offset(-5));
assert_eq!(info.dst(), Dst::No);
assert_eq!(info.abbreviation(), "EST");
assert_eq!(
info.offset().to_datetime(ts).to_string(),
"2024-01-10T21:46:44",
);
Implementations§
§impl<'t> TimeZoneOffsetInfo<'t>
impl<'t> TimeZoneOffsetInfo<'t>
pub fn offset(&self) -> Offset
pub fn offset(&self) -> Offset
Returns the offset.
The offset is duration, from UTC, that should be used to offset the civil time in a particular location.
§Example
use jiff::{civil, tz::{TimeZone, offset}};
let tz = TimeZone::get("US/Eastern")?;
// Get the offset for 2023-03-10 00:00:00.
let start = civil::date(2024, 3, 10).to_zoned(tz.clone())?.timestamp();
let info = tz.to_offset_info(start);
assert_eq!(info.offset(), offset(-5));
// Go forward a day and notice the offset changes due to DST!
let start = civil::date(2024, 3, 11).to_zoned(tz.clone())?.timestamp();
let info = tz.to_offset_info(start);
assert_eq!(info.offset(), offset(-4));
pub fn abbreviation(&self) -> &str ⓘ
pub fn abbreviation(&self) -> &str ⓘ
Returns the time zone abbreviation corresponding to this offset info.
Note that abbreviations can to be ambiguous. For example, the
abbreviation CST
can be used for the time zones Asia/Shanghai
,
America/Chicago
and America/Havana
.
The lifetime of the string returned is tied to this
TimeZoneOffsetInfo
, which may be shorter than 't
(the lifetime of
the time zone this transition was created from).
§Example
use jiff::{civil, tz::TimeZone};
let tz = TimeZone::get("US/Eastern")?;
// Get the time zone abbreviation for 2023-03-10 00:00:00.
let start = civil::date(2024, 3, 10).to_zoned(tz.clone())?.timestamp();
let info = tz.to_offset_info(start);
assert_eq!(info.abbreviation(), "EST");
// Go forward a day and notice the abbreviation changes due to DST!
let start = civil::date(2024, 3, 11).to_zoned(tz.clone())?.timestamp();
let info = tz.to_offset_info(start);
assert_eq!(info.abbreviation(), "EDT");
pub fn dst(&self) -> Dst
pub fn dst(&self) -> Dst
Returns whether daylight saving time is enabled for this offset info.
Callers should generally treat this as informational only. In particular, not all time zone transitions are related to daylight saving time. For example, some transitions are a result of a region permanently changing their offset from UTC.
§Example
use jiff::{civil, tz::{Dst, TimeZone}};
let tz = TimeZone::get("US/Eastern")?;
// Get the DST status of 2023-03-11 00:00:00.
let start = civil::date(2024, 3, 11).to_zoned(tz.clone())?.timestamp();
let info = tz.to_offset_info(start);
assert_eq!(info.dst(), Dst::Yes);
Trait Implementations§
§impl<'t> Clone for TimeZoneOffsetInfo<'t>
impl<'t> Clone for TimeZoneOffsetInfo<'t>
§fn clone(&self) -> TimeZoneOffsetInfo<'t>
fn clone(&self) -> TimeZoneOffsetInfo<'t>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more§impl<'t> Debug for TimeZoneOffsetInfo<'t>
impl<'t> Debug for TimeZoneOffsetInfo<'t>
§impl<'t> Hash for TimeZoneOffsetInfo<'t>
impl<'t> Hash for TimeZoneOffsetInfo<'t>
§impl<'t> PartialEq for TimeZoneOffsetInfo<'t>
impl<'t> PartialEq for TimeZoneOffsetInfo<'t>
impl<'t> Eq for TimeZoneOffsetInfo<'t>
impl<'t> StructuralPartialEq for TimeZoneOffsetInfo<'t>
Auto Trait Implementations§
impl<'t> Freeze for TimeZoneOffsetInfo<'t>
impl<'t> RefUnwindSafe for TimeZoneOffsetInfo<'t>
impl<'t> Send for TimeZoneOffsetInfo<'t>
impl<'t> Sync for TimeZoneOffsetInfo<'t>
impl<'t> Unpin for TimeZoneOffsetInfo<'t>
impl<'t> UnwindSafe for TimeZoneOffsetInfo<'t>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> ByteSized for T
impl<T> ByteSized for T
Source§const BYTE_ALIGN: usize = _
const BYTE_ALIGN: usize = _
Source§fn byte_align(&self) -> usize
fn byte_align(&self) -> usize
Source§fn ptr_size_ratio(&self) -> [usize; 2]
fn ptr_size_ratio(&self) -> [usize; 2]
Source§impl<T, R> Chain<R> for Twhere
T: ?Sized,
impl<T, R> Chain<R> for Twhere
T: ?Sized,
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.Source§impl<T> ExtAny for T
impl<T> ExtAny for T
Source§fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
fn type_hash_with<H: Hasher>(&self, hasher: H) -> u64
TypeId
of Self
using a custom hasher.Source§fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
fn as_any_mut(&mut self) -> &mut dyn Anywhere
Self: Sized,
Source§impl<T> ExtMem for Twhere
T: ?Sized,
impl<T> ExtMem for Twhere
T: ?Sized,
Source§const NEEDS_DROP: bool = _
const NEEDS_DROP: bool = _
Source§fn mem_align_of<T>() -> usize
fn mem_align_of<T>() -> usize
Source§fn mem_align_of_val(&self) -> usize
fn mem_align_of_val(&self) -> usize
Source§fn mem_size_of<T>() -> usize
fn mem_size_of<T>() -> usize
Source§fn mem_size_of_val(&self) -> usize
fn mem_size_of_val(&self) -> usize
Source§fn mem_needs_drop(&self) -> bool
fn mem_needs_drop(&self) -> bool
true
if dropping values of this type matters. Read moreSource§fn mem_forget(self)where
Self: Sized,
fn mem_forget(self)where
Self: Sized,
self
without running its destructor. Read moreSource§fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
fn mem_replace(&mut self, other: Self) -> Selfwhere
Self: Sized,
Source§unsafe fn mem_zeroed<T>() -> T
unsafe fn mem_zeroed<T>() -> T
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe fn mem_transmute_copy<Src, Dst>(src: &Src) -> Dst
unsafe_layout
only.T
represented by the all-zero byte-pattern. Read moreSource§fn mem_as_bytes(&self) -> &[u8] ⓘ
fn mem_as_bytes(&self) -> &[u8] ⓘ
unsafe_slice
only.§impl<S> FromSample<S> for S
impl<S> FromSample<S> for S
fn from_sample_(s: S) -> S
Source§impl<T> Hook for T
impl<T> Hook for T
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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