Struct utc2k::LocalOffset

source ·
pub struct LocalOffset { /* private fields */ }
Available on crate feature 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§

source§

impl LocalOffset

source

pub fn now() -> Self

§Now.

Return the current, local offset. If no offset can be determined, UTC will be assumed.

§Examples
let now = utc2k::LocalOffset::now();
source

pub const fn localtime(self) -> u32

§Local Timestamp.

Return the sum of the unix timestamp and the offset.

source

pub const fn offset(self) -> i32

§Offset.

Return the local offset (in seconds). Zero is returned if there is no offset, or no offset could be determined.

source

pub const fn unixtime(self) -> u32

§Unixtime.

Return the unix timestamp this instance applies to (i.e. the value it was seeded with).

§Examples
use utc2k::LocalOffset;

let offset = LocalOffset::from(946_684_800_u32);
assert_eq!(offset.unixtime(), 946_684_800_u32);

Trait Implementations§

source§

impl Clone for LocalOffset

source§

fn clone(&self) -> LocalOffset

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for LocalOffset

source§

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

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

impl Default for LocalOffset

source§

fn default() -> LocalOffset

Returns the “default value” for a type. Read more
source§

impl From<LocalOffset> for FmtUtc2k

source§

fn from(src: LocalOffset) -> Self

Converts to this type from the input type.
source§

impl From<LocalOffset> for Utc2k

source§

fn from(src: LocalOffset) -> Self

Converts to this type from the input type.
source§

impl From<LocalOffset> for i32

source§

fn from(src: LocalOffset) -> Self

Converts to this type from the input type.
source§

impl From<Utc2k> for LocalOffset

source§

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!

source§

impl From<u32> for LocalOffset

source§

fn from(unixtime: u32) -> Self

Converts to this type from the input type.
source§

impl Hash for LocalOffset

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl Neg for LocalOffset

§

type Output = LocalOffset

The resulting type after applying the - operator.
source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
source§

impl PartialEq for LocalOffset

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Copy for LocalOffset

source§

impl Eq for LocalOffset

source§

impl StructuralPartialEq for LocalOffset

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> 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,

§

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>,

§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

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.