Struct SpanTotal
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 SpanTotalcomputes a total for the given unit in the receiver span forSpan::total.From<(Unit, civil::Date)> for SpanTotalcomputes a total for the given unit in the receiver span forSpan::total, relative to the given date. There are alsoFromimplementations forcivil::DateTime,ZonedandSpanRelativeTo.
§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§
§impl<'a> SpanTotal<'a>
impl<'a> SpanTotal<'a>
pub fn days_are_24_hours(self) -> SpanTotal<'a>
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§
§impl<'a> From<(Unit, SpanRelativeTo<'a>)> for SpanTotal<'a>
impl<'a> From<(Unit, SpanRelativeTo<'a>)> for SpanTotal<'a>
§fn from(_: (Unit, SpanRelativeTo<'a>)) -> SpanTotal<'a>
fn from(_: (Unit, SpanRelativeTo<'a>)) -> SpanTotal<'a>
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
§impl<T> Paint for Twhere
T: ?Sized,
impl<T> Paint for Twhere
T: ?Sized,
§fn fg(&self, value: Color) -> Painted<&T>
fn fg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the foreground set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like red() and
green(), which have the same functionality but are
pithier.
§Example
Set foreground color to white using fg():
use yansi::{Paint, Color};
painted.fg(Color::White);Set foreground color to white using white().
use yansi::Paint;
painted.white();§fn bright_black(&self) -> Painted<&T>
fn bright_black(&self) -> Painted<&T>
§fn bright_red(&self) -> Painted<&T>
fn bright_red(&self) -> Painted<&T>
§fn bright_green(&self) -> Painted<&T>
fn bright_green(&self) -> Painted<&T>
§fn bright_yellow(&self) -> Painted<&T>
fn bright_yellow(&self) -> Painted<&T>
§fn bright_blue(&self) -> Painted<&T>
fn bright_blue(&self) -> Painted<&T>
§fn bright_magenta(&self) -> Painted<&T>
fn bright_magenta(&self) -> Painted<&T>
§fn bright_cyan(&self) -> Painted<&T>
fn bright_cyan(&self) -> Painted<&T>
§fn bright_white(&self) -> Painted<&T>
fn bright_white(&self) -> Painted<&T>
§fn bg(&self, value: Color) -> Painted<&T>
fn bg(&self, value: Color) -> Painted<&T>
Returns a styled value derived from self with the background set to
value.
This method should be used rarely. Instead, prefer to use color-specific
builder methods like on_red() and
on_green(), which have the same functionality but
are pithier.
§Example
Set background color to red using fg():
use yansi::{Paint, Color};
painted.bg(Color::Red);Set background color to red using on_red().
use yansi::Paint;
painted.on_red();§fn on_primary(&self) -> Painted<&T>
fn on_primary(&self) -> Painted<&T>
§fn on_magenta(&self) -> Painted<&T>
fn on_magenta(&self) -> Painted<&T>
§fn on_bright_black(&self) -> Painted<&T>
fn on_bright_black(&self) -> Painted<&T>
§fn on_bright_red(&self) -> Painted<&T>
fn on_bright_red(&self) -> Painted<&T>
§fn on_bright_green(&self) -> Painted<&T>
fn on_bright_green(&self) -> Painted<&T>
§fn on_bright_yellow(&self) -> Painted<&T>
fn on_bright_yellow(&self) -> Painted<&T>
§fn on_bright_blue(&self) -> Painted<&T>
fn on_bright_blue(&self) -> Painted<&T>
§fn on_bright_magenta(&self) -> Painted<&T>
fn on_bright_magenta(&self) -> Painted<&T>
§fn on_bright_cyan(&self) -> Painted<&T>
fn on_bright_cyan(&self) -> Painted<&T>
§fn on_bright_white(&self) -> Painted<&T>
fn on_bright_white(&self) -> Painted<&T>
§fn attr(&self, value: Attribute) -> Painted<&T>
fn attr(&self, value: Attribute) -> Painted<&T>
Enables the styling [Attribute] value.
This method should be used rarely. Instead, prefer to use
attribute-specific builder methods like bold() and
underline(), which have the same functionality
but are pithier.
§Example
Make text bold using attr():
use yansi::{Paint, Attribute};
painted.attr(Attribute::Bold);Make text bold using using bold().
use yansi::Paint;
painted.bold();§fn rapid_blink(&self) -> Painted<&T>
fn rapid_blink(&self) -> Painted<&T>
§fn quirk(&self, value: Quirk) -> Painted<&T>
fn quirk(&self, value: Quirk) -> Painted<&T>
Enables the yansi [Quirk] value.
This method should be used rarely. Instead, prefer to use quirk-specific
builder methods like mask() and
wrap(), which have the same functionality but are
pithier.
§Example
Enable wrapping using .quirk():
use yansi::{Paint, Quirk};
painted.quirk(Quirk::Wrap);Enable wrapping using wrap().
use yansi::Paint;
painted.wrap();§fn clear(&self) -> Painted<&T>
👎Deprecated since 1.0.1: renamed to resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.
fn clear(&self) -> Painted<&T>
resetting() due to conflicts with Vec::clear().
The clear() method will be removed in a future release.§fn whenever(&self, value: Condition) -> Painted<&T>
fn whenever(&self, value: Condition) -> Painted<&T>
Conditionally enable styling based on whether the [Condition] value
applies. Replaces any previous condition.
See the crate level docs for more details.
§Example
Enable styling painted only when both stdout and stderr are TTYs:
use yansi::{Paint, Condition};
painted.red().on_yellow().whenever(Condition::STDOUTERR_ARE_TTY);