Struct Offset
struct Offset { ... }
Represents a fixed time zone offset.
Negative offsets correspond to time zones west of the prime meridian, while
positive offsets correspond to time zones east of the prime meridian.
Equivalently, in all cases, civil-time - offset = UTC.
Display format
This type implements the std::fmt::Display trait. It
will convert the offset to a string format in the form
{sign}{hours}[:{minutes}[:{seconds}]], where minutes and seconds are
only present when non-zero. For example:
use tz;
let o = offset;
assert_eq!;
let o = from_seconds.unwrap;
assert_eq!;
let o = from_seconds.unwrap;
assert_eq!;
let o = from_seconds.unwrap;
assert_eq!;
// The min value.
let o = from_seconds.unwrap;
assert_eq!;
// The max value.
let o = from_seconds.unwrap;
assert_eq!;
// No offset.
let o = offset;
assert_eq!;
Example
This shows how to create a zoned datetime with a time zone using a fixed offset:
use ;
let offset = offset.to_time_zone;
let zdt = date.at.to_zoned?;
assert_eq!;
# Ok::
Notice that the zoned datetime still includes a time zone annotation. But since there is no time zone identifier, the offset instead is repeated as an additional assertion that a fixed offset datetime was intended.
Implementations
impl Offset
const fn constant(hours: i8) -> OffsetCreates a new time zone offset in a
constcontext from a given number of hours.Negative offsets correspond to time zones west of the prime meridian, while positive offsets correspond to time zones east of the prime meridian. Equivalently, in all cases,
civil-time - offset = UTC.The fallible non-const version of this constructor is
Offset::from_hours.Panics
This routine panics when the given number of hours is out of range. Namely,
hoursmust be in the range-25..=25.Example
use Offset; let o = constant; assert_eq!; let o = constant; assert_eq!;Alternatively, one can use the terser
jiff::tz::offsetfree function:use tz; let o = offset; assert_eq!; let o = offset; assert_eq!;fn from_hours(hours: i8) -> Result<Offset, Error>Creates a new time zone offset from a given number of hours.
Negative offsets correspond to time zones west of the prime meridian, while positive offsets correspond to time zones east of the prime meridian. Equivalently, in all cases,
civil-time - offset = UTC.Errors
This routine returns an error when the given number of hours is out of range. Namely,
hoursmust be in the range-25..=25.Example
use Offset; let o = from_hours?; assert_eq!; let o = from_hours?; assert_eq!; # Ok::fn from_seconds(seconds: i32) -> Result<Offset, Error>Creates a new time zone offset in a
constcontext from a given number of seconds.Negative offsets correspond to time zones west of the prime meridian, while positive offsets correspond to time zones east of the prime meridian. Equivalently, in all cases,
civil-time - offset = UTC.Errors
This routine returns an error when the given number of seconds is out of range. The range corresponds to the offsets
-25:59:59..=25:59:59. In units of seconds, that corresponds to-93,599..=93,599.Example
use Offset; let o = from_seconds?; assert_eq!; let o = from_seconds?; assert_eq!; # Ok::fn seconds(self: Self) -> i32Returns the total number of seconds in this offset.
The value returned is guaranteed to represent an offset in the range
-25:59:59..=25:59:59. Or more precisely, the value will be in units of seconds in the range-93,599..=93,599.Negative offsets correspond to time zones west of the prime meridian, while positive offsets correspond to time zones east of the prime meridian. Equivalently, in all cases,
civil-time - offset = UTC.Example
use tz; let o = offset; assert_eq!; let o = offset; assert_eq!;fn negate(self: Self) -> OffsetReturns the negation of this offset.
A negative offset will become positive and vice versa. This is a no-op if the offset is zero.
This never panics.
Example
use tz; assert_eq!; // It's also available via the `-` operator: assert_eq!;fn signum(self: Self) -> i8Returns the "sign number" or "signum" of this offset.
The number returned is
-1when this offset is negative,0when this offset is zero and1when this span is positive.Example
use tz; assert_eq!; assert_eq!; assert_eq!;fn is_positive(self: Self) -> boolReturns true if and only if this offset is positive.
This returns false when the offset is zero or negative.
Example
use tz; assert!; assert!; assert!;fn is_negative(self: Self) -> boolReturns true if and only if this offset is less than zero.
Example
use tz; assert!; assert!; assert!;fn is_zero(self: Self) -> boolReturns true if and only if this offset is zero.
Or equivalently, when this offset corresponds to
Offset::UTC.Example
use tz; assert!; assert!; assert!;fn to_time_zone(self: Self) -> TimeZoneConverts this offset into a
TimeZone.This is a convenience function for calling
TimeZone::fixedwith this offset.Example
use offset; let tz = offset.to_time_zone; assert_eq!;fn to_datetime(self: Self, timestamp: Timestamp) -> DateTimeConverts the given timestamp to a civil datetime using this offset.
Example
use ; assert_eq!;fn to_timestamp(self: Self, dt: DateTime) -> Result<Timestamp, Error>Converts the given civil datetime to a timestamp using this offset.
Errors
This returns an error if this would have returned a timestamp outside of its minimum and maximum values.
Example
This example shows how to find the timestamp corresponding to
1969-12-31T16:00:00-08.use ; assert_eq!; # Ok::This example shows some maximum boundary conditions where this routine will fail:
use ; let dt = date.at; assert!; // If the offset is big enough, then converting it to a UTC // timestamp will fit, even when using the maximum civil datetime. let dt = date.at; assert_eq!; // But adjust the offset down 1 second is enough to go out-of-bounds. assert!;Same as above, but for minimum values:
use ; let dt = date.at; assert!; // If the offset is small enough, then converting it to a UTC // timestamp will fit, even when using the minimum civil datetime. let dt = date.at; assert_eq!; // But adjust the offset up 1 second is enough to go out-of-bounds. assert!;fn checked_add<A: Into<OffsetArithmetic>>(self: Self, duration: A) -> Result<Offset, Error>Adds the given span of time to this offset.
Since time zone offsets have second resolution, any fractional seconds in the duration given are ignored.
This operation accepts three different duration types:
Span,SignedDurationorstd::time::Duration. This is achieved viaFromtrait implementations for theOffsetArithmetictype.Errors
This returns an error if the result of adding the given span would exceed the minimum or maximum allowed
Offsetvalue.This also returns an error if the span given contains any non-zero units bigger than hours.
Example
This example shows how to add one hour to an offset (if the offset corresponds to standard time, then adding an hour will usually give you DST time):
use ; let off = offset; assert_eq!;And note that while fractional seconds are ignored, units less than seconds aren't ignored if they sum up to a duration at least as big as one second:
use ; let off = offset; let span = 900.milliseconds .microseconds .nanoseconds; assert_eq!; // Any leftover fractional part is ignored. let span = 901.milliseconds .microseconds .nanoseconds; assert_eq!;This example shows some cases where checked addition will fail.
use ; // Adding units above 'hour' always results in an error. assert!; assert!; assert!; assert!; // Adding even 1 second to the max, or subtracting 1 from the min, // will result in overflow and thus an error will be returned. assert!; assert!;Example: adding absolute durations
This shows how to add signed and unsigned absolute durations to an
Offset. Like withSpans, any fractional seconds are ignored.use Duration; use ; let off = offset; let dur = from_hours; assert_eq!; assert_eq!; // Any leftover time is truncated. That is, only // whole seconds from the duration are considered. let dur = new; assert_eq!; # Ok::fn checked_sub<A: Into<OffsetArithmetic>>(self: Self, duration: A) -> Result<Offset, Error>This routine is identical to
Offset::checked_addwith the duration negated.Errors
This has the same error conditions as
Offset::checked_add.Example
use Duration; use ; let off = offset; assert_eq!; assert_eq!; assert_eq!; # Ok::fn saturating_add<A: Into<OffsetArithmetic>>(self: Self, duration: A) -> OffsetThis routine is identical to
Offset::checked_add, except the result saturates on overflow. That is, instead of overflow, eitherOffset::MINorOffset::MAXis returned.Example
This example shows some cases where saturation will occur.
use ; // Adding units above 'day' always results in saturation. assert_eq!; assert_eq!; assert_eq!; // Adding even 1 second to the max, or subtracting 1 from the min, // will result in saturationg. assert_eq!; assert_eq!; // Adding absolute durations also saturates as expected. assert_eq!; assert_eq!; assert_eq!;fn saturating_sub<A: Into<OffsetArithmetic>>(self: Self, duration: A) -> OffsetThis routine is identical to
Offset::saturating_addwith the span parameter negated.Example
This example shows some cases where saturation will occur.
use ; // Adding units above 'day' always results in saturation. assert_eq!; assert_eq!; assert_eq!; // Adding even 1 second to the max, or subtracting 1 from the min, // will result in saturationg. assert_eq!; assert_eq!; // Adding absolute durations also saturates as expected. assert_eq!; assert_eq!; assert_eq!;fn until(self: Self, other: Offset) -> SpanReturns the span of time from this offset until the other given.
When the
otheroffset is more west (i.e., more negative) of the prime meridian than this offset, then the span returned will be negative.Properties
Adding the span returned to this offset will always equal the
otheroffset given.Examples
use ; assert_eq!; // Flipping the operands in this case results in a negative span. assert_eq!;fn since(self: Self, other: Offset) -> SpanReturns the span of time since the other offset given from this offset.
When the
otheris more east (i.e., more positive) of the prime meridian than this offset, then the span returned will be negative.Properties
Adding the span returned to the
otheroffset will always equal this offset.Examples
use ; assert_eq!; // Flipping the operands in this case results in a negative span. assert_eq!;fn duration_until(self: Self, other: Offset) -> SignedDurationReturns an absolute duration representing the difference in time from this offset until the given
otheroffset.When the
otheroffset is more west (i.e., more negative) of the prime meridian than this offset, then the duration returned will be negative.Unlike
Offset::until, this returns a duration corresponding to a 96-bit integer of nanoseconds between two offsets.When should I use this versus
Offset::until?See the type documentation for
SignedDurationfor the section on when one should useSpanand when one should useSignedDuration. In short, useSpan(and thereforeOffset::until) unless you have a specific reason to do otherwise.Examples
use ; assert_eq!; // Flipping the operands in this case results in a negative span. assert_eq!;fn duration_since(self: Self, other: Offset) -> SignedDurationThis routine is identical to
Offset::duration_until, but the order of the parameters is flipped.Examples
use ; assert_eq!; assert_eq!;fn round<R: Into<OffsetRound>>(self: Self, options: R) -> Result<Offset, Error>Returns a new offset that is rounded according to the given configuration.
Rounding an offset has a number of parameters, all of which are optional. When no parameters are given, then no rounding is done, and the offset as given is returned. That is, it's a no-op.
As is consistent with
Offsetitself, rounding only supports units of hours, minutes or seconds. If any other unit is provided, then an error is returned.The parameters are, in brief:
OffsetRound::smallestsets the smallestUnitthat is allowed to be non-zero in the offset returned. By default, it is set toUnit::Second, i.e., no rounding occurs. When the smallest unit is set to something bigger than seconds, then the non-zero units in the offset smaller than the smallest unit are used to determine how the offset should be rounded. For example, rounding+01:59to the nearest hour using the default rounding mode would produce+02:00.OffsetRound::modedetermines how to handle the remainder when rounding. The default isRoundMode::HalfExpand, which corresponds to how you were likely taught to round in school. Alternative modes, likeRoundMode::Trunc, exist too. For example, a truncating rounding of+01:59to the nearest hour would produce+01:00.OffsetRound::incrementsets the rounding granularity to use for the configured smallest unit. For example, if the smallest unit is minutes and the increment is15, then the offset returned will always have its minute component set to a multiple of15.
Errors
In general, there are two main ways for rounding to fail: an improper configuration like trying to round an offset to the nearest unit other than hours/minutes/seconds, or when overflow occurs. Overflow can occur when the offset would exceed the minimum or maximum
Offsetvalues. Typically, this can only realistically happen if the offset before rounding is already close to its minimum or maximum value.Example: rounding to the nearest multiple of 15 minutes
Most time zone offsets fall on an hour boundary, but some fall on the half-hour or even 15 minute boundary:
use ; let offset = from_seconds.unwrap; let rounded = offset.round?; assert_eq!; # Ok::Example: rounding can fail via overflow
use ; assert_eq!; assert_eq!;
impl Add for Offset
fn add(self: Self, rhs: SignedDuration) -> Offset
impl Add for Offset
fn add(self: Self, rhs: Span) -> Offset
impl Add for Offset
fn add(self: Self, rhs: UnsignedDuration) -> Offset
impl AddAssign for Offset
fn add_assign(self: &mut Self, rhs: SignedDuration)
impl AddAssign for Offset
fn add_assign(self: &mut Self, rhs: Span)
impl AddAssign for Offset
fn add_assign(self: &mut Self, rhs: UnsignedDuration)
impl Clone for Offset
fn clone(self: &Self) -> Offset
impl Copy for Offset
impl Debug for Offset
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Display for Offset
fn fmt(self: &Self, f: &mut Formatter<'_>) -> Result
impl Eq for Offset
impl Freeze for Offset
impl Hash for Offset
fn hash<__H: $crate::hash::Hasher>(self: &Self, state: &mut __H)
impl Neg for Offset
fn neg(self: Self) -> Offset
impl Ord for Offset
fn cmp(self: &Self, other: &Offset) -> Ordering
impl PartialEq for Offset
fn eq(self: &Self, other: &Offset) -> bool
impl PartialOrd for Offset
fn partial_cmp(self: &Self, other: &Offset) -> Option<Ordering>
impl RefUnwindSafe for Offset
impl Send for Offset
impl StructuralPartialEq for Offset
impl Sub for Offset
fn sub(self: Self, rhs: Span) -> Offset
impl Sub for Offset
fn sub(self: Self, rhs: SignedDuration) -> Offset
impl Sub for Offset
fn sub(self: Self, rhs: UnsignedDuration) -> Offset
impl Sub for Offset
fn sub(self: Self, rhs: Offset) -> Span
impl SubAssign for Offset
fn sub_assign(self: &mut Self, rhs: Span)
impl SubAssign for Offset
fn sub_assign(self: &mut Self, rhs: SignedDuration)
impl SubAssign for Offset
fn sub_assign(self: &mut Self, rhs: UnsignedDuration)
impl Sync for Offset
impl TryFrom for Offset
fn try_from(sdur: SignedDuration) -> Result<Offset, Error>
impl Unpin for Offset
impl UnsafeUnpin for Offset
impl UnwindSafe for Offset
impl<T> Any for Offset
fn type_id(self: &Self) -> TypeId
impl<T> Borrow for Offset
fn borrow(self: &Self) -> &T
impl<T> BorrowMut for Offset
fn borrow_mut(self: &mut Self) -> &mut T
impl<T> CloneToUninit for Offset
unsafe fn clone_to_uninit(self: &Self, dest: *mut u8)
impl<T> From for Offset
fn from(t: T) -> TReturns the argument unchanged.
impl<T> ToOwned for Offset
fn to_owned(self: &Self) -> Tfn clone_into(self: &Self, target: &mut T)
impl<T> ToString for Offset
fn to_string(self: &Self) -> String
impl<T, U> Into for Offset
fn into(self: Self) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses to do.
impl<T, U> TryFrom for Offset
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
impl<T, U> TryInto for Offset
fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>