Struct jiff::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 or weeks). Otherwise, an error will be returned.

When no relative datetime is provided, days are always 24 hours long.

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 and Zoned.

§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::{ToSpan, Unit};

let span = 123_456_789.seconds();
assert_eq!(span.total(Unit::Day)?, 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).intz("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);

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

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

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

source§

fn from((unit, zoned): (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): (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): (Unit, DateTime)) -> SpanTotal<'static>

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 T)

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

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.