Struct utc2k::LocalOffset
source · pub struct LocalOffset { /* private fields */ }
local
only.Expand description
Local Offset.
This struct attempts to determine the appropriate UTC offset for the local timezone in a thread-safe manner, but only for unix systems.
Instantiation will never fail, though.
If the platform isn’t supported or no offset can be determined, the “offset” will simply be zero (i.e. as if it were UTC).
Examples
To obtain the current local offset, use LocalOffset::now
.
To obtain the local offset for a specific unix timestamp, use its From<u32>
implementation instead.
use utc2k::LocalOffset;
// The current offset.
let now = LocalOffset::now();
// The offset for a specific time.
let then = LocalOffset::from(946_684_800_u32);
If all you want is the offset, you can grab that by calling
LocalOffset::offset
afterward.
This struct can, however, also be used to trick FmtUtc2k
and Utc2k
into representing local rather than UTC datetimes by using their
From<LocalOffset>
implementations, or the FmtUtc2k::now_local
/Utc2k::now_local
shorthands.
If you do this, however, it is worth noting that comparing tricked and untricked objects makes no logical sense, so you shouldn’t mix-and-match if you need to test for equality or ordering.
Additionally, you should avoid localizing a datetime if you plan on using the
to_rfc2822
or to_rfc3339
formatting helpers. They always assume they’re
representing a UTC timestamp, so will add the wrong suffix to their output
if your local offset is non-zero.
Other than that, the trick works perfectly well. ;)
use utc2k::{LocalOffset, Utc2k};
let now_utc = Utc2k::now();
// These two are equivalent.
let now_local = Utc2k::from(LocalOffset::now());
let now_local = Utc2k::now_local();
If you need to convert from a local timestamp into a UTC one, you can
leverage std::ops::Neg
to invert the offset, like:
use utc2k::{LocalOffset, Utc2k};
let offset = LocalOffset::from(946_684_800_u32);
let utc = Utc2k::from(-offset);
Implementations
sourceimpl LocalOffset
impl LocalOffset
Trait Implementations
sourceimpl Clone for LocalOffset
impl Clone for LocalOffset
sourcefn clone(&self) -> LocalOffset
fn clone(&self) -> LocalOffset
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresourceimpl Debug for LocalOffset
impl Debug for LocalOffset
sourceimpl Default for LocalOffset
impl Default for LocalOffset
sourcefn default() -> LocalOffset
fn default() -> LocalOffset
sourceimpl From<LocalOffset> for FmtUtc2k
impl From<LocalOffset> for FmtUtc2k
sourcefn from(src: LocalOffset) -> Self
fn from(src: LocalOffset) -> Self
sourceimpl From<LocalOffset> for Utc2k
impl From<LocalOffset> for Utc2k
sourcefn from(src: LocalOffset) -> Self
fn from(src: LocalOffset) -> Self
sourceimpl From<LocalOffset> for i32
impl From<LocalOffset> for i32
sourcefn from(src: LocalOffset) -> Self
fn from(src: LocalOffset) -> Self
sourceimpl From<Utc2k> for LocalOffset
impl From<Utc2k> for LocalOffset
sourcefn from(src: Utc2k) -> Self
fn from(src: Utc2k) -> Self
From Utc2k
Warning: this should only be used for Utc2k
instances holding honest
UTC datetimes. If you call this on a tricked/local instance, the offset
will get applied twice!