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.

This currently only works for unix systems. For everybody else, it will act as if there is no local offset (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

Now.

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

Examples
let now = utc2k::LocalOffset::now();
Offset.

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

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

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

Converts to this type from the input type.

Converts to this type from the input type.

Converts to this type from the input type.

Feeds this value into the given Hasher. Read more

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

The resulting type after applying the - operator.

Performs the unary - operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.