Struct TimeDelta

pub struct TimeDelta { /* private fields */ }

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

Returns the total number of whole weeks in the TimeDelta.

const fn num_days(&self) -> i64

Returns the total number of whole days in the TimeDelta.

const fn num_hours(&self) -> i64

Returns the total number of whole hours in the TimeDelta.

const fn num_minutes(&self) -> i64

Returns the total number of whole minutes in the TimeDelta.

const fn num_seconds(&self) -> i64

Returns the total number of whole seconds in the TimeDelta.

fn as_seconds_f64(self) -> f64

Returns the fractional number of seconds in the TimeDelta.

fn as_seconds_f32(self) -> f32

Returns the fractional number of seconds in the TimeDelta.

const fn num_milliseconds(&self) -> i64

Returns the total number of whole milliseconds in the TimeDelta.

const fn subsec_millis(&self) -> i32

Returns the number of milliseconds in the fractional part of the duration.

This is the number of milliseconds such that subsec_millis() + num_seconds() * 1_000 is the truncated number of milliseconds in the duration.

const fn num_microseconds(&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 subsec_micros(&self) -> i32

Returns the number of microseconds in the fractional part of the duration.

This is the number of microseconds such that subsec_micros() + num_seconds() * 1_000_000 is the truncated number of microseconds in the duration.

const fn num_nanoseconds(&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 subsec_nanos(&self) -> i32

Returns the number of nanoseconds in the fractional part of the duration.

This is the number of nanoseconds such that subsec_nanos() + num_seconds() * 1_000_000_000 is the total number of nanoseconds in the TimeDelta.

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

Add two TimeDeltas, returning None if overflow occurred.

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

Subtract two TimeDeltas, returning None if overflow occurred.

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

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

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

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

const fn abs(&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) -> 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) -> 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.

const MIN: Self = MIN;

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

const MAX: Self = MAX;

The maximum possible TimeDelta: i64::MAX milliseconds.

Trait Implementations

impl Add for TimeDelta

type Output = TimeDelta;
fn add(self, rhs: TimeDelta) -> TimeDelta

impl AddAssign for TimeDelta

fn add_assign(&mut self, rhs: TimeDelta)

impl Clone for TimeDelta

fn clone(&self) -> TimeDelta

impl Copy for TimeDelta

impl Debug for TimeDelta

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

impl Default for TimeDelta

fn default() -> TimeDelta

impl Display for TimeDelta

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

Format a TimeDelta using the ISO 8601 format

impl Div<i32> for TimeDelta

type Output = TimeDelta;
fn div(self, rhs: i32) -> TimeDelta

impl Eq for TimeDelta

impl Hash for TimeDelta

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

impl Mul<i32> for TimeDelta

type Output = TimeDelta;
fn mul(self, rhs: i32) -> TimeDelta

impl Neg for TimeDelta

type Output = TimeDelta;
fn neg(self) -> TimeDelta

impl Ord for TimeDelta

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

impl PartialEq for TimeDelta

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

impl PartialOrd for TimeDelta

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

impl StructuralPartialEq for TimeDelta

impl Sub for TimeDelta

type Output = TimeDelta;
fn sub(self, rhs: TimeDelta) -> TimeDelta

impl SubAssign for TimeDelta

fn sub_assign(&mut self, rhs: TimeDelta)

impl Sum for TimeDelta

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

impl<'a> Sum<&'a TimeDelta> for TimeDelta

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

Auto Trait Implementations

impl Freeze for TimeDelta

impl RefUnwindSafe for TimeDelta

impl Send for TimeDelta

impl Sync for TimeDelta

impl Unpin for TimeDelta

impl UnsafeUnpin for TimeDelta

impl UnwindSafe for TimeDelta

Blanket Implementations

impl<T> Any for TimeDelta where T: 'static + ?Sized,

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for TimeDelta where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for TimeDelta where T: ?Sized,

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

impl<T> CloneToUninit for TimeDelta where T: Clone,

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

impl<T> From<T> for TimeDelta

fn from(t: T) -> T

Returns the argument unchanged.

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

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T> ToString for TimeDelta where T: Display + ?Sized,

fn to_string(&self) -> String

impl<T, U> Into<U> for TimeDelta where U: From<T>,

fn into(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<U> for TimeDelta where U: Into<T>,

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

impl<T, U> TryInto<U> for TimeDelta where U: TryFrom<T>,

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