Skip to main content

ExactDuration

Struct ExactDuration 

Source
pub struct ExactDuration { /* private fields */ }
Expand description

Exact-precision signed duration.

Internally an i128 of nanoseconds. Range ≈ ±5.4 × 10²¹ yr at 1 ns resolution (i128::MAX ≈ 1.7 × 10³⁸ ns ÷ 3.156 × 10¹⁶ ns/yr ≈ 5.4 × 10²¹ yr).

Construction:

Accessors:

Implementations§

Source§

impl ExactDuration

Source

pub const ZERO: Self

Zero duration.

Source

pub const NANOSECOND: Self

Smallest representable positive duration (1 ns).

Source

pub const SECOND: Self

One second.

Source

pub const MAX: Self

Maximum representable duration.

Source

pub const MIN: Self

Minimum (most negative) representable duration.

Source

pub const fn from_nanos(nanos: i128) -> Self

Build from a raw nanosecond count.

Source

pub const fn from_seconds_and_nanos( seconds: i64, nanos: i32, ) -> Result<Self, DurationError>

Build from (seconds, nanos) boundary projection.

The fractional nanos is interpreted with the same sign as seconds when seconds != 0; when seconds == 0, nanos carries the sign directly. This matches the unambiguous total result_nanos = seconds * 1e9 + nanos.

Returns DurationError::Overflow if the multiplication overflows.

Source

pub const fn from_canonical_seconds_nanos( seconds: i64, nanos: i32, ) -> Result<Self, DurationError>

Strict canonical constructor: requires nanos ∈ (-1_000_000_000, 1_000_000_000) and that seconds and nanos share the same sign (or nanos == 0).

The full invariant:

  • |nanos| < 1_000_000_000
  • if seconds > 0, then nanos >= 0
  • if seconds < 0, then nanos <= 0
  • if seconds == 0, either sign of nanos is valid

Returns DurationError::Overflow if |nanos| >= 1_000_000_000 or if the multiplication overflows, and DurationError::NonCanonical if the sign invariant is violated.

Source

pub fn try_from_quantity<U: TimeUnit>( q: Quantity<U>, ) -> Result<Self, DurationError>

Build from a qtty::Quantity<U> of any time unit. Returns DurationError::NonFinite for NaN/inf inputs and DurationError::Overflow if the value does not fit in i128 ns.

Source

pub fn from_quantity<U: TimeUnit>(q: Quantity<U>) -> Self

Infallible variant for callers that already know the input is finite and in-range. Panics on non-finite or overflowing input. For fallible conversion, use try_from_quantity.

Source

pub fn from_seconds_f64_lossy(seconds: f64) -> Option<Self>

Explicit lossy f64ExactDuration boundary. Named so the lossy step is visible in code review. Returns None on non-finite input or when the value does not fit in i128 ns.

Source

pub const fn as_nanos_i128(self) -> i128

Raw signed nanosecond count.

Source

pub const fn as_seconds_i64_nanos_checked( self, ) -> Result<(i64, i32), DurationError>

Exact boundary projection (seconds, nanos) such that seconds * 1_000_000_000 + nanos == self.as_nanos_i128() and nanos ∈ (-1_000_000_000, 1_000_000_000).

Returns DurationError::Overflow when the seconds component does not fit in i64 (i.e. |self| > i64::MAX * 1e9 ns ≈ ±292 billion years).

Use this as the canonical API when the invariant must be preserved. For guaranteed-small durations you may use as_seconds_i64_nanos (panics on overflow).

Source

pub const fn as_seconds_i64_nanos_saturating(self) -> (i64, i32)

Boundary projection (seconds, nanos) that saturates the seconds component to i64::MAX / i64::MIN for extreme values.

Lossy / non-canonical for durations outside the i64 seconds range (≈ ±292 billion years). The invariant seconds * 1_000_000_000 + nanos == as_nanos_i128() is not preserved when saturation occurs. Use as_seconds_i64_nanos_checked for exact behaviour.

Source

pub const fn as_seconds_i64_nanos(self) -> (i64, i32)

Boundary projection (seconds, nanos). Panics if the seconds component does not fit in i64.

For durations within ≈ ±292 billion years this never panics in practice. Use as_seconds_i64_nanos_checked to handle the overflow case explicitly.

Source

pub fn as_seconds_f64(self) -> f64

Explicit lossy ExactDurationf64 seconds boundary.

Source

pub fn from_nanoseconds_i(nanos: Nanosecond) -> Self

Build from a typed qtty::i64::Nanosecond integer quantity.

The i64 value is widened to i128 without loss; this conversion is always exact. For the low-level raw interface, see from_nanos.

Source

pub fn from_seconds_i(seconds: Second) -> Self

Build from a typed qtty::i64::Second integer quantity (whole-second precision).

The second value is multiplied by 1 × 10⁹ and widened to i128 without loss for any i64 input. For sub-second precision use from_canonical_seconds_nanos or from_nanoseconds_i.

Source

pub fn as_nanoseconds_i(self) -> Result<Nanosecond, DurationError>

Project to a typed qtty::i64::Nanosecond integer quantity.

Returns DurationError::Overflow when the stored nanosecond count does not fit in i64 (durations outside ≈ ±292 billion years at 1 ns resolution).

Source

pub fn as_quantity<U: TimeUnit>(self) -> Quantity<U>

Project back into a qtty::Quantity<U>. Lossy in general (f64).

Source

pub const fn is_zero(self) -> bool

True iff exactly zero.

Source

pub const fn is_negative(self) -> bool

True iff strictly negative.

Source

pub const fn checked_abs(self) -> Result<Self, DurationError>

Absolute value. Returns DurationError::Overflow on ExactDuration::MIN (i128::MIN has no representable positive).

Source

pub const fn checked_add(self, rhs: Self) -> Result<Self, DurationError>

Checked addition.

Source

pub const fn checked_sub(self, rhs: Self) -> Result<Self, DurationError>

Checked subtraction.

Source

pub const fn checked_neg(self) -> Result<Self, DurationError>

Checked negation.

Source

pub const fn saturating_add(self, rhs: Self) -> Self

Saturating addition.

Source

pub const fn saturating_sub(self, rhs: Self) -> Self

Saturating subtraction.

Source

pub const fn round_to(self, quantum: ExactDuration) -> Self

Round this duration to the nearest multiple of quantum (banker’s rounding / half-to-even). quantum must be strictly positive; a non-positive quantum returns self unchanged to avoid surprising errors in formatting paths.

Source

pub const fn floor_to(self, quantum: ExactDuration) -> Self

Floor this duration toward negative infinity at quantum.

Source

pub const fn ceil_to(self, quantum: ExactDuration) -> Self

Ceil this duration toward positive infinity at quantum.

Trait Implementations§

Source§

impl Add for ExactDuration

Source§

type Output = ExactDuration

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self

Performs the + operation. Read more
Source§

impl AddAssign for ExactDuration

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for ExactDuration

Source§

fn clone(&self) -> ExactDuration

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for ExactDuration

Source§

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

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

impl Default for ExactDuration

Source§

fn default() -> Self

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

impl Display for ExactDuration

Source§

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

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

impl Hash for ExactDuration

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

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

impl Neg for ExactDuration

Source§

type Output = ExactDuration

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self

Performs the unary - operation. Read more
Source§

impl Ord for ExactDuration

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for ExactDuration

Source§

fn eq(&self, other: &ExactDuration) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for ExactDuration

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Sub for ExactDuration

Source§

type Output = ExactDuration

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self

Performs the - operation. Read more
Source§

impl SubAssign for ExactDuration

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl Copy for ExactDuration

Source§

impl Eq for ExactDuration

Source§

impl StructuralPartialEq for ExactDuration

Auto Trait Implementations§

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, dest: *mut u8)

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

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
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> 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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. 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.