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
Returns a copy of the value. Read more
1.0.0 · sourcefn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from source
. Read more
sourceimpl Debug for LocalOffset
impl Debug for LocalOffset
sourceimpl Default for LocalOffset
impl Default for LocalOffset
sourcefn default() -> LocalOffset
fn default() -> LocalOffset
Returns the “default value” for a type. Read more
sourceimpl From<LocalOffset> for FmtUtc2k
impl From<LocalOffset> for FmtUtc2k
sourcefn from(src: LocalOffset) -> Self
fn from(src: LocalOffset) -> Self
Converts to this type from the input type.
sourceimpl From<LocalOffset> for Utc2k
impl From<LocalOffset> for Utc2k
sourcefn from(src: LocalOffset) -> Self
fn from(src: LocalOffset) -> Self
Converts to this type from the input type.
sourceimpl From<LocalOffset> for i32
impl From<LocalOffset> for i32
sourcefn from(src: LocalOffset) -> Self
fn from(src: LocalOffset) -> Self
Converts to this type from the input type.
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!
sourceimpl From<u32> for LocalOffset
impl From<u32> for LocalOffset
sourceimpl Hash for LocalOffset
impl Hash for LocalOffset
sourceimpl Neg for LocalOffset
impl Neg for LocalOffset
sourceimpl PartialEq<LocalOffset> for LocalOffset
impl PartialEq<LocalOffset> for LocalOffset
sourcefn eq(&self, other: &LocalOffset) -> bool
fn eq(&self, other: &LocalOffset) -> bool
This method tests for self
and other
values to be equal, and is used
by ==
. Read more
impl Copy for LocalOffset
impl Eq for LocalOffset
impl StructuralEq for LocalOffset
impl StructuralPartialEq for LocalOffset
Auto Trait Implementations
impl RefUnwindSafe for LocalOffset
impl Send for LocalOffset
impl Sync for LocalOffset
impl Unpin for LocalOffset
impl UnwindSafe for LocalOffset
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more