rustmax::jiff

Struct SpanTotal

Source
pub struct SpanTotal<'a> { /* private fields */ }
Expand description

Options for Span::total.

This type provides a way to ergonomically determine the number of a particular unit in a span, with a potentially fractional component, with an optional relative datetime. Namely, a relative datetime is only needed when the span has a non-zero calendar unit (years, months, weeks or days). Otherwise, an error will be returned.

Callers may use SpanTotal::days_are_24_hours to opt into 24-hour invariant days (and 7-day weeks) without providing a relative datetime.

The main way to construct values of this type is with its From trait implementations:

  • From<Unit> for SpanTotal computes a total for the given unit in the receiver span for Span::total.
  • From<(Unit, civil::Date)> for SpanTotal computes a total for the given unit in the receiver span for Span::total, relative to the given date. There are also From implementations for civil::DateTime, Zoned and SpanRelativeTo.

§Example

This example shows how to find the number of seconds in a particular span:

use jiff::{ToSpan, Unit};

let span = 3.hours().minutes(10);
assert_eq!(span.total(Unit::Second)?, 11_400.0);

§Example: 24 hour days

This shows how to find the total number of 24 hour days in 123,456,789 seconds.

use jiff::{SpanTotal, ToSpan, Unit};

let span = 123_456_789.seconds();
assert_eq!(
    span.total(SpanTotal::from(Unit::Day).days_are_24_hours())?,
    1428.8980208333332,
);

§Example: DST is taken into account

The month of March 2024 in America/New_York had 31 days, but one of those days was 23 hours long due a transition into daylight saving time:

use jiff::{civil::date, ToSpan, Unit};

let span = 744.hours();
let relative = date(2024, 3, 1).in_tz("America/New_York")?;
// Because of the short day, 744 hours is actually a little *more* than
// 1 month starting from 2024-03-01.
assert_eq!(span.total((Unit::Month, &relative))?, 1.0013888888888889);

Now compare what happens when the relative datetime is civil and not time zone aware:

use jiff::{civil::date, ToSpan, Unit};

let span = 744.hours();
let relative = date(2024, 3, 1);
assert_eq!(span.total((Unit::Month, relative))?, 1.0);

Implementations§

Source§

impl<'a> SpanTotal<'a>

Source

pub fn days_are_24_hours(self) -> SpanTotal<'a>

This is a convenience function for setting the relative option on this configuration to SpanRelativeTo::days_are_24_hours.

§Example

When computing the total duration for spans involving days, either a relative datetime must be provided, or a special assertion opting into 24-hour days is required. Otherwise, you get an error.

use jiff::{civil::date, SpanTotal, ToSpan, Unit};

let span = 2.days().hours(12);

// No relative date provided, which results in an error.
assert_eq!(
    span.total(Unit::Hour).unwrap_err().to_string(),
    "using unit 'day' in a span or configuration requires that either \
     a relative reference time be given or \
     `SpanRelativeTo::days_are_24_hours()` is used to indicate \
     invariant 24-hour days, but neither were provided",
);

// If we can assume all days are 24 hours, then we can assert it:
let total = span.total(
    SpanTotal::from(Unit::Hour).days_are_24_hours(),
)?;
assert_eq!(total, 60.0);

// Or provide a relative datetime, which is preferred if possible:
let total = span.total((Unit::Hour, date(2025, 1, 26)))?;
assert_eq!(total, 60.0);

Trait Implementations§

Source§

impl<'a> Clone for SpanTotal<'a>

Source§

fn clone(&self) -> SpanTotal<'a>

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<'a> Debug for SpanTotal<'a>

Source§

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

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

impl<'a> From<(Unit, &'a Zoned)> for SpanTotal<'a>

Source§

fn from(_: (Unit, &'a Zoned)) -> SpanTotal<'a>

Converts to this type from the input type.
Source§

impl From<(Unit, Date)> for SpanTotal<'static>

Source§

fn from(_: (Unit, Date)) -> SpanTotal<'static>

Converts to this type from the input type.
Source§

impl From<(Unit, DateTime)> for SpanTotal<'static>

Source§

fn from(_: (Unit, DateTime)) -> SpanTotal<'static>

Converts to this type from the input type.
Source§

impl<'a> From<(Unit, SpanRelativeTo<'a>)> for SpanTotal<'a>

Source§

fn from(_: (Unit, SpanRelativeTo<'a>)) -> SpanTotal<'a>

Converts to this type from the input type.
Source§

impl From<Unit> for SpanTotal<'static>

Source§

fn from(unit: Unit) -> SpanTotal<'static>

Converts to this type from the input type.
Source§

impl<'a> Copy for SpanTotal<'a>

Auto Trait Implementations§

§

impl<'a> Freeze for SpanTotal<'a>

§

impl<'a> RefUnwindSafe for SpanTotal<'a>

§

impl<'a> Send for SpanTotal<'a>

§

impl<'a> Sync for SpanTotal<'a>

§

impl<'a> Unpin for SpanTotal<'a>

§

impl<'a> UnwindSafe for SpanTotal<'a>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize = _

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> QuickClone<T> for T
where T: Clone,

Source§

fn C(&self) -> T

Source§

impl<T> QuickToOwned for T
where T: ToOwned,

Source§

type Owned = <T as ToOwned>::Owned

Source§

fn O(&self) -> <T as QuickToOwned>::Owned

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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

Source§

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

Source§

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.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T