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
TimeDeltawith given number of seconds and nanoseconds.Errors
Returns
Nonewhen the duration is out of bounds, or ifnanos≥ 1,000,000,000.const fn weeks(weeks: i64) -> TimeDeltaMakes a new
TimeDeltawith 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
TimeDeltawith the given number of weeks.Equivalent to
TimeDelta::try_seconds(weeks * 7 * 24 * 60 * 60)with overflow checks.Errors
Returns
Nonewhen theTimeDeltawould be out of bounds.const fn days(days: i64) -> TimeDeltaMakes a new
TimeDeltawith the given number of days.Equivalent to
TimeDelta::seconds(days * 24 * 60 * 60)with overflow checks.Panics
Panics when the
TimeDeltawould be out of bounds.const fn try_days(days: i64) -> Option<TimeDelta>Makes a new
TimeDeltawith the given number of days.Equivalent to
TimeDelta::try_seconds(days * 24 * 60 * 60)with overflow checks.Errors
Returns
Nonewhen theTimeDeltawould be out of bounds.const fn hours(hours: i64) -> TimeDeltaMakes a new
TimeDeltawith the given number of hours.Equivalent to
TimeDelta::seconds(hours * 60 * 60)with overflow checks.Panics
Panics when the
TimeDeltawould be out of bounds.const fn try_hours(hours: i64) -> Option<TimeDelta>Makes a new
TimeDeltawith the given number of hours.Equivalent to
TimeDelta::try_seconds(hours * 60 * 60)with overflow checks.Errors
Returns
Nonewhen theTimeDeltawould be out of bounds.const fn minutes(minutes: i64) -> TimeDeltaMakes a new
TimeDeltawith the given number of minutes.Equivalent to
TimeDelta::seconds(minutes * 60)with overflow checks.Panics
Panics when the
TimeDeltawould be out of bounds.const fn try_minutes(minutes: i64) -> Option<TimeDelta>Makes a new
TimeDeltawith the given number of minutes.Equivalent to
TimeDelta::try_seconds(minutes * 60)with overflow checks.Errors
Returns
Nonewhen theTimeDeltawould be out of bounds.const fn seconds(seconds: i64) -> TimeDeltaMakes a new
TimeDeltawith the given number of seconds.Panics
Panics when
secondsis more thani64::MAX / 1_000or less than-i64::MAX / 1_000(in this context, this is the same asi64::MIN / 1_000due to rounding).const fn try_seconds(seconds: i64) -> Option<TimeDelta>Makes a new
TimeDeltawith the given number of seconds.Errors
Returns
Nonewhensecondsis more thani64::MAX / 1_000or less than-i64::MAX / 1_000(in this context, this is the same asi64::MIN / 1_000due to rounding).const fn milliseconds(milliseconds: i64) -> TimeDeltaMakes a new
TimeDeltawith the given number of milliseconds.Panics
Panics when the
TimeDeltawould be out of bounds, i.e. whenmillisecondsis more thani64::MAXor less than-i64::MAX. Notably, this is not the same asi64::MIN.const fn try_milliseconds(milliseconds: i64) -> Option<TimeDelta>Makes a new
TimeDeltawith the given number of milliseconds.Errors
Returns
NonetheTimeDeltawould be out of bounds, i.e. whenmillisecondsis more thani64::MAXor less than-i64::MAX. Notably, this is not the same asi64::MIN.const fn microseconds(microseconds: i64) -> TimeDeltaMakes a new
TimeDeltawith 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) -> TimeDeltaMakes a new
TimeDeltawith 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) -> i64Returns the total number of whole weeks in the
TimeDelta.const fn num_days(&self) -> i64Returns the total number of whole days in the
TimeDelta.const fn num_hours(&self) -> i64Returns the total number of whole hours in the
TimeDelta.const fn num_minutes(&self) -> i64Returns the total number of whole minutes in the
TimeDelta.const fn num_seconds(&self) -> i64Returns the total number of whole seconds in the
TimeDelta.fn as_seconds_f64(self) -> f64Returns the fractional number of seconds in the
TimeDelta.fn as_seconds_f32(self) -> f32Returns the fractional number of seconds in the
TimeDelta.const fn num_milliseconds(&self) -> i64Returns the total number of whole milliseconds in the
TimeDelta.const fn subsec_millis(&self) -> i32Returns the number of milliseconds in the fractional part of the duration.
This is the number of milliseconds such that
subsec_millis() + num_seconds() * 1_000is 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, orNoneon overflow (exceeding 2^63 microseconds in either direction).const fn subsec_micros(&self) -> i32Returns 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_000is 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, orNoneon overflow (exceeding 2^63 nanoseconds in either direction).const fn subsec_nanos(&self) -> i32Returns 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_000is the total number of nanoseconds in theTimeDelta.const fn checked_add(&self, rhs: &TimeDelta) -> Option<TimeDelta>Add two
TimeDeltas, returningNoneif overflow occurred.const fn checked_sub(&self, rhs: &TimeDelta) -> Option<TimeDelta>Subtract two
TimeDeltas, returningNoneif overflow occurred.const fn checked_mul(&self, rhs: i32) -> Option<TimeDelta>Multiply a
TimeDeltawith a i32, returningNoneif overflow occurred.const fn checked_div(&self, rhs: i32) -> Option<TimeDelta>Divide a
TimeDeltawith a i32, returningNoneif dividing by 0.const fn abs(&self) -> TimeDeltaReturns the
TimeDeltaas an absolute (non-negative) value.const fn min_value() -> TimeDeltaThe minimum possible
TimeDelta:-i64::MAXmilliseconds.const fn max_value() -> TimeDeltaThe maximum possible
TimeDelta:i64::MAXmilliseconds.const fn zero() -> TimeDeltaA
TimeDeltawhere the stored seconds and nanoseconds are equal to zero.const fn is_zero(&self) -> boolReturns
trueif theTimeDeltaequalsTimeDelta::zero().const fn from_std(duration: Duration) -> Result<TimeDelta, OutOfRangeError>Creates a
TimeDeltaobject fromstd::time::DurationThis 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::Durationobject from aTimeDelta.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::MAXmilliseconds.const MAX: Self = MAX;The maximum possible
TimeDelta:i64::MAXmilliseconds.
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<'_>) -> ResultFormat a
TimeDeltausing 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) -> TReturns the argument unchanged.
impl<T> ToOwned for TimeDelta
where
T: Clone,
type Owned = T;fn to_owned(&self) -> Tfn 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) -> UCalls
U::from(self).That is, this conversion is whatever the implementation of
[From]<T> for Uchooses 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>