Struct TimeDelta

struct TimeDelta { ... }

Time duration with nanosecond precision.

This also allows for negative durations; see individual methods for details.

A TimeDelta is represented internally as a complement of seconds and nanoseconds. The range is restricted to that of i64 milliseconds, with the minimum value notably being set to -i64::MAX rather than allowing the full range of i64::MIN. This is to allow easy flipping of sign, so that for instance abs() can be called without any checks.

Implementations

impl TimeDelta

const fn new(secs: i64, nanos: u32) -> Option<TimeDelta>

Makes a new TimeDelta with given number of seconds and nanoseconds.

Errors

Returns None when the duration is out of bounds, or if nanos ≥ 1,000,000,000.

const fn weeks(weeks: i64) -> TimeDelta

Makes a new TimeDelta with the given number of weeks.

Equivalent to TimeDelta::seconds(weeks * 7 * 24 * 60 * 60) with overflow checks.

Panics

Panics when the duration is out of bounds.

const fn try_weeks(weeks: i64) -> Option<TimeDelta>

Makes a new TimeDelta with the given number of weeks.

Equivalent to TimeDelta::try_seconds(weeks * 7 * 24 * 60 * 60) with overflow checks.

Errors

Returns None when the TimeDelta would be out of bounds.

const fn days(days: i64) -> TimeDelta

Makes a new TimeDelta with the given number of days.

Equivalent to TimeDelta::seconds(days * 24 * 60 * 60) with overflow checks.

Panics

Panics when the TimeDelta would be out of bounds.

const fn try_days(days: i64) -> Option<TimeDelta>

Makes a new TimeDelta with the given number of days.

Equivalent to TimeDelta::try_seconds(days * 24 * 60 * 60) with overflow checks.

Errors

Returns None when the TimeDelta would be out of bounds.

const fn hours(hours: i64) -> TimeDelta

Makes a new TimeDelta with the given number of hours.

Equivalent to TimeDelta::seconds(hours * 60 * 60) with overflow checks.

Panics

Panics when the TimeDelta would be out of bounds.

const fn try_hours(hours: i64) -> Option<TimeDelta>

Makes a new TimeDelta with the given number of hours.

Equivalent to TimeDelta::try_seconds(hours * 60 * 60) with overflow checks.

Errors

Returns None when the TimeDelta would be out of bounds.

const fn minutes(minutes: i64) -> TimeDelta

Makes a new TimeDelta with the given number of minutes.

Equivalent to TimeDelta::seconds(minutes * 60) with overflow checks.

Panics

Panics when the TimeDelta would be out of bounds.

const fn try_minutes(minutes: i64) -> Option<TimeDelta>

Makes a new TimeDelta with the given number of minutes.

Equivalent to TimeDelta::try_seconds(minutes * 60) with overflow checks.

Errors

Returns None when the TimeDelta would be out of bounds.

const fn seconds(seconds: i64) -> TimeDelta

Makes a new TimeDelta with the given number of seconds.

Panics

Panics when seconds is more than i64::MAX / 1_000 or less than -i64::MAX / 1_000 (in this context, this is the same as i64::MIN / 1_000 due to rounding).

const fn try_seconds(seconds: i64) -> Option<TimeDelta>

Makes a new TimeDelta with the given number of seconds.

Errors

Returns None when seconds is more than i64::MAX / 1_000 or less than -i64::MAX / 1_000 (in this context, this is the same as i64::MIN / 1_000 due to rounding).

const fn milliseconds(milliseconds: i64) -> TimeDelta

Makes a new TimeDelta with the given number of milliseconds.

Panics

Panics when the TimeDelta would be out of bounds, i.e. when milliseconds is more than i64::MAX or less than -i64::MAX. Notably, this is not the same as i64::MIN.

const fn try_milliseconds(milliseconds: i64) -> Option<TimeDelta>

Makes a new TimeDelta with the given number of milliseconds.

Errors

Returns None the TimeDelta would be out of bounds, i.e. when milliseconds is more than i64::MAX or less than -i64::MAX. Notably, this is not the same as i64::MIN.

const fn microseconds(microseconds: i64) -> TimeDelta

Makes a new TimeDelta with the given number of microseconds.

The number of microseconds acceptable by this constructor is less than the total number that can actually be stored in a TimeDelta, so it is not possible to specify a value that would be out of bounds. This function is therefore infallible.

const fn nanoseconds(nanos: i64) -> TimeDelta

Makes a new TimeDelta with the given number of nanoseconds.

The number of nanoseconds acceptable by this constructor is less than the total number that can actually be stored in a TimeDelta, so it is not possible to specify a value that would be out of bounds. This function is therefore infallible.

const fn num_weeks(self: &Self) -> i64

Returns the total number of whole weeks in the TimeDelta.

const fn num_days(self: &Self) -> i64

Returns the total number of whole days in the TimeDelta.

const fn num_hours(self: &Self) -> i64

Returns the total number of whole hours in the TimeDelta.

const fn num_minutes(self: &Self) -> i64

Returns the total number of whole minutes in the TimeDelta.

const fn num_seconds(self: &Self) -> i64

Returns the total number of whole seconds in the TimeDelta.

const fn subsec_nanos(self: &Self) -> i32

Returns the number of nanoseconds such that subsec_nanos() + num_seconds() * NANOS_PER_SEC is the total number of nanoseconds in the TimeDelta.

const fn num_milliseconds(self: &Self) -> i64

Returns the total number of whole milliseconds in the TimeDelta.

const fn num_microseconds(self: &Self) -> Option<i64>

Returns the total number of whole microseconds in the TimeDelta, or None on overflow (exceeding 2^63 microseconds in either direction).

const fn num_nanoseconds(self: &Self) -> Option<i64>

Returns the total number of whole nanoseconds in the TimeDelta, or None on overflow (exceeding 2^63 nanoseconds in either direction).

const fn checked_add(self: &Self, rhs: &TimeDelta) -> Option<TimeDelta>

Add two TimeDeltas, returning None if overflow occurred.

const fn checked_sub(self: &Self, rhs: &TimeDelta) -> Option<TimeDelta>

Subtract two TimeDeltas, returning None if overflow occurred.

const fn checked_mul(self: &Self, rhs: i32) -> Option<TimeDelta>

Multiply a TimeDelta with a i32, returning None if overflow occurred.

const fn checked_div(self: &Self, rhs: i32) -> Option<TimeDelta>

Divide a TimeDelta with a i32, returning None if dividing by 0.

const fn abs(self: &Self) -> TimeDelta

Returns the TimeDelta as an absolute (non-negative) value.

const fn min_value() -> TimeDelta

The minimum possible TimeDelta: -i64::MAX milliseconds.

const fn max_value() -> TimeDelta

The maximum possible TimeDelta: i64::MAX milliseconds.

const fn zero() -> TimeDelta

A TimeDelta where the stored seconds and nanoseconds are equal to zero.

const fn is_zero(self: &Self) -> bool

Returns true if the TimeDelta equals TimeDelta::zero().

const fn from_std(duration: Duration) -> Result<TimeDelta, OutOfRangeError>

Creates a TimeDelta object from std::time::Duration

This function errors when original duration is larger than the maximum value supported for this type.

const fn to_std(self: &Self) -> Result<Duration, OutOfRangeError>

Creates a std::time::Duration object from a TimeDelta.

This function errors when duration is less than zero. As standard library implementation is limited to non-negative values.

impl Add for TimeDelta

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

impl AddAssign for TimeDelta

fn add_assign(self: &mut Self, rhs: TimeDelta)

impl Clone for TimeDelta

fn clone(self: &Self) -> TimeDelta

impl Copy for TimeDelta

impl Debug for TimeDelta

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

impl Default for TimeDelta

fn default() -> TimeDelta

impl Display for TimeDelta

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

Format a TimeDelta using the ISO 8601 format

impl Div for TimeDelta

fn div(self: Self, rhs: i32) -> TimeDelta

impl Eq for TimeDelta

impl Freeze for TimeDelta

impl Hash for TimeDelta

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

impl Mul for TimeDelta

fn mul(self: Self, rhs: i32) -> TimeDelta

impl Neg for TimeDelta

fn neg(self: Self) -> TimeDelta

impl Ord for TimeDelta

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

impl PartialEq for TimeDelta

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

impl PartialOrd for TimeDelta

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

impl RefUnwindSafe for TimeDelta

impl Send for TimeDelta

impl StructuralPartialEq for TimeDelta

impl Sub for TimeDelta

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

impl SubAssign for TimeDelta

fn sub_assign(self: &mut Self, rhs: TimeDelta)

impl Sum for TimeDelta

fn sum<I: Iterator<Item = TimeDelta>>(iter: I) -> TimeDelta

impl Sync for TimeDelta

impl Unpin for TimeDelta

impl UnsafeUnpin for TimeDelta

impl UnwindSafe for TimeDelta

impl<'a> Sum for TimeDelta

fn sum<I: Iterator<Item = &'a TimeDelta>>(iter: I) -> TimeDelta

impl<T> Any for TimeDelta

fn type_id(self: &Self) -> TypeId

impl<T> Borrow for TimeDelta

fn borrow(self: &Self) -> &T

impl<T> BorrowMut for TimeDelta

fn borrow_mut(self: &mut Self) -> &mut T

impl<T> CloneToUninit for TimeDelta

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

impl<T> From for TimeDelta

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for TimeDelta

fn to_owned(self: &Self) -> T
fn clone_into(self: &Self, target: &mut T)

impl<T> ToString for TimeDelta

fn to_string(self: &Self) -> String

impl<T, U> Into for TimeDelta

fn into(self: Self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of [From]<T> for U chooses to do.

impl<T, U> TryFrom for TimeDelta

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

impl<T, U> TryInto for TimeDelta

fn try_into(self: Self) -> Result<U, <U as TryFrom<T>>::Error>