pub struct Duration { /* private fields */ }
Expand description

Defines generally usable durations for nanosecond precision valid for 32,768 centuries in either direction, and only on 80 bits / 10 octets.

Important conventions: Conventions had to be made to define the partial order of a duration.

  1. It was decided that the nanoseconds corresponds to the nanoseconds into the current century. In other words, a durationn with centuries = -1 and nanoseconds = 0 is a smaller duration than centuries = -1 and nanoseconds = 1. That difference is exactly 1 nanoseconds, where the former duration is “closer to zero” than the latter. As such, the largest negative duration that can be represented sets the centuries to i16::MAX and its nanoseconds to NANOSECONDS_PER_CENTURY.
  2. It was also decided that opposite durations are equal, e.g. -15 minutes == 15 minutes. If the direction of time matters, use the signum function.

Implementations§

source§

impl Duration

source

pub fn from_parts(centuries: i16, nanoseconds: u64) -> Duration

Create a normalized duration from its parts

source

pub const fn to_parts(&self) -> (i16, u64)

Returns the centuries and nanoseconds of this duration NOTE: These items are not public to prevent incorrect durations from being created by modifying the values of the structure directly.

source

pub fn from_total_nanoseconds(nanos: i128) -> Duration

Converts the total nanoseconds as i128 into this Duration (saving 48 bits)

source

pub fn total_nanoseconds(&self) -> i128

Returns the total nanoseconds in a signed 128 bit integer

source

pub fn try_truncated_nanoseconds(&self) -> Result<i64, Errors>

Returns the truncated nanoseconds in a signed 64 bit integer, if the duration fits.

source

pub fn truncated_nanoseconds(&self) -> i64

Returns the truncated nanoseconds in a signed 64 bit integer, if the duration fits. WARNING: This function will NOT fail and will return the i64::MIN or i64::MAX depending on the sign of the centuries if the Duration does not fit on aa i64

source

pub fn from_truncated_nanoseconds(nanos: i64) -> Duration

Create a new duration from the truncated nanoseconds (+/- 2927.1 years of duration)

source

pub fn from_f64(value: f64, unit: Unit) -> Duration

Creates a new duration from the provided unit

source

pub fn in_seconds(&self) -> f64

Returns this duration in seconds f64. For high fidelity comparisons, it is recommended to keep using the Duration structure.

source

pub fn in_unit(&self, unit: Unit) -> f64

Returns the value of this duration in the requested unit.

source

pub fn abs(&self) -> Duration

Returns the absolute value of this duration

source

pub fn new(centuries: i16, nanoseconds: u64) -> Duration

Builds a new duration from the number of centuries and the number of nanoseconds

source

pub const fn signum(&self) -> i8

Returns the sign of this duration

source

pub fn decompose(&self) -> (i8, u64, u64, u64, u64, u64, u64, u64)

Decomposes a Duration in its sign, days, hours, minutes, seconds, ms, us, ns

source

pub fn compose( sign: i8, days: u64, hours: u64, minutes: u64, seconds: u64, milliseconds: u64, microseconds: u64, nanoseconds: u64 ) -> Duration

Creates a new duration from its parts

source

pub fn floor(&self, duration: Duration) -> Duration

Floors this duration to the closest duration from the bottom

Example
use hifitime::{Duration, TimeUnits};

let two_hours_three_min = 2.hours() + 3.minutes();
assert_eq!(two_hours_three_min.floor(1.hours()), 2.hours());
assert_eq!(two_hours_three_min.floor(30.minutes()), 2.hours());
// This is zero because we floor by a duration longer than the current duration, rounding it down
assert_eq!(two_hours_three_min.floor(4.hours()), 0.hours());
assert_eq!(two_hours_three_min.floor(1.seconds()), two_hours_three_min);
assert_eq!(two_hours_three_min.floor(1.hours() + 1.minutes()), 2.hours() + 2.minutes());
assert_eq!(two_hours_three_min.floor(1.hours() + 5.minutes()), 1.hours() + 5.minutes());
source

pub fn ceil(&self, duration: Duration) -> Duration

Ceils this duration to the closest provided duration

This simply floors then adds the requested duration

Example
use hifitime::{Duration, TimeUnits};

let two_hours_three_min = 2.hours() + 3.minutes();
assert_eq!(two_hours_three_min.ceil(1.hours()), 3.hours());
assert_eq!(two_hours_three_min.ceil(30.minutes()), 2.hours() + 30.minutes());
assert_eq!(two_hours_three_min.ceil(4.hours()), 4.hours());
assert_eq!(two_hours_three_min.ceil(1.seconds()), two_hours_three_min + 1.seconds());
assert_eq!(two_hours_three_min.ceil(1.hours() + 5.minutes()), 2.hours() + 10.minutes());
source

pub fn round(&self, duration: Duration) -> Duration

Rounds this duration to the closest provided duration

This performs both a ceil and floor and returns the value which is the closest to current one.

Example
use hifitime::{Duration, TimeUnits};

let two_hours_three_min = 2.hours() + 3.minutes();
assert_eq!(two_hours_three_min.round(1.hours()), 2.hours());
assert_eq!(two_hours_three_min.round(30.minutes()), 2.hours());
assert_eq!(two_hours_three_min.round(4.hours()), 4.hours());
assert_eq!(two_hours_three_min.round(1.seconds()), two_hours_three_min);
assert_eq!(two_hours_three_min.round(1.hours() + 5.minutes()), 2.hours() + 10.minutes());
source

pub const ZERO: Duration = Self{ centuries: 0, nanoseconds: 0,}

A duration of exactly zero nanoseconds

source

pub const MAX: Duration = Self{ centuries: i16::MAX, nanoseconds: NANOSECONDS_PER_CENTURY,}

Maximum duration that can be represented

source

pub const MIN: Duration = Self{ centuries: i16::MIN, nanoseconds: NANOSECONDS_PER_CENTURY,}

Minimum duration that can be represented

source

pub const EPSILON: Duration = Self{ centuries: 0, nanoseconds: 1,}

Smallest duration that can be represented

source

pub const MIN_POSITIVE: Duration = Self::EPSILON

Minimum positive duration is one nanoseconds

source

pub const MIN_NEGATIVE: Duration = Self{ centuries: -1, nanoseconds: NANOSECONDS_PER_CENTURY - 1,}

Minimum negative duration is minus one nanosecond

Trait Implementations§

source§

impl Add<Duration> for Duration

§

type Output = Duration

The resulting type after applying the + operator.
source§

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

Performs the + operation. Read more
source§

impl Add<Duration> for Epoch

§

type Output = Epoch

The resulting type after applying the + operator.
source§

fn add(self, duration: Duration) -> Epoch

Performs the + operation. Read more
source§

impl Add<Unit> for Duration

§

type Output = Duration

The resulting type after applying the + operator.
source§

fn add(self, rhs: Unit) -> Duration

Performs the + operation. Read more
source§

impl AddAssign<Duration> for Duration

source§

fn add_assign(&mut self, rhs: Duration)

Performs the += operation. Read more
source§

impl AddAssign<Duration> for Epoch

source§

fn add_assign(&mut self, duration: Duration)

Performs the += operation. Read more
source§

impl AddAssign<Unit> for Duration

source§

fn add_assign(&mut self, rhs: Unit)

Performs the += operation. Read more
source§

impl Clone for Duration

source§

fn clone(&self) -> Duration

Returns a copy of the value. Read more
1.0.0 · source§

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

Performs copy-assignment from source. Read more
source§

impl Debug for Duration

source§

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

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

impl<'de> Deserialize<'de> for Duration

source§

fn deserialize<D>( deserializer: D ) -> Result<Duration, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for Duration

source§

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

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

impl Div<f64> for Duration

§

type Output = Duration

The resulting type after applying the / operator.
source§

fn div(self, q: f64) -> <Duration as Div<f64>>::Output

Performs the / operation. Read more
source§

impl Div<i64> for Duration

§

type Output = Duration

The resulting type after applying the / operator.
source§

fn div(self, q: i64) -> <Duration as Div<i64>>::Output

Performs the / operation. Read more
source§

impl FromStr for Duration

source§

fn from_str(s: &str) -> Result<Duration, <Duration as FromStr>::Err>

Attempts to convert a simple string to a Duration. Does not yet support complicated durations.

Identifiers:

  • d, days, day
  • h, hours, hour
  • min, mins, minute
  • s, second, seconds
  • ms, millisecond, milliseconds
  • us, microsecond, microseconds
  • ns, nanosecond, nanoseconds
Example
use hifitime::{Duration, Unit};
use std::str::FromStr;

assert_eq!(Duration::from_str("1 d").unwrap(), Unit::Day * 1);
assert_eq!(Duration::from_str("10.598 days").unwrap(), Unit::Day * 10.598);
assert_eq!(Duration::from_str("10.598 min").unwrap(), Unit::Minute * 10.598);
assert_eq!(Duration::from_str("10.598 us").unwrap(), Unit::Microsecond * 10.598);
assert_eq!(Duration::from_str("10.598 seconds").unwrap(), Unit::Second * 10.598);
assert_eq!(Duration::from_str("10.598 nanosecond").unwrap(), Unit::Nanosecond * 10.598);
§

type Err = Errors

The associated error which can be returned from parsing.
source§

impl LowerExp for Duration

source§

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

Formats the value using the given formatter.
source§

impl Mul<f64> for Duration

§

type Output = Duration

The resulting type after applying the * operator.
source§

fn mul(self, q: f64) -> <Duration as Mul<f64>>::Output

Performs the * operation. Read more
source§

impl Mul<i64> for Duration

§

type Output = Duration

The resulting type after applying the * operator.
source§

fn mul(self, q: i64) -> <Duration as Mul<i64>>::Output

Performs the * operation. Read more
source§

impl Neg for Duration

§

type Output = Duration

The resulting type after applying the - operator.
source§

fn neg(self) -> <Duration as Neg>::Output

Performs the unary - operation. Read more
source§

impl Ord for Duration

source§

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

This method returns an Ordering between self and other. Read more
1.21.0 · source§

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

Compares and returns the maximum of two values. Read more
1.21.0 · source§

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

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Selfwhere Self: Sized + PartialOrd<Self>,

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

impl PartialEq<Duration> for Duration

source§

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<Unit> for Duration

source§

fn eq(&self, unit: &Unit) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialOrd<Duration> for Duration

source§

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

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl PartialOrd<Unit> for Duration

source§

fn partial_cmp(&self, unit: &Unit) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
source§

impl Sub<Duration> for Duration

§

type Output = Duration

The resulting type after applying the - operator.
source§

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

Performs the - operation. Read more
source§

impl Sub<Duration> for Epoch

§

type Output = Epoch

The resulting type after applying the - operator.
source§

fn sub(self, duration: Duration) -> Epoch

Performs the - operation. Read more
source§

impl Sub<Unit> for Duration

§

type Output = Duration

The resulting type after applying the - operator.
source§

fn sub(self, rhs: Unit) -> Duration

Performs the - operation. Read more
source§

impl SubAssign<Duration> for Duration

source§

fn sub_assign(&mut self, rhs: Duration)

Performs the -= operation. Read more
source§

impl SubAssign<Duration> for Epoch

source§

fn sub_assign(&mut self, duration: Duration)

Performs the -= operation. Read more
source§

impl SubAssign<Unit> for Duration

source§

fn sub_assign(&mut self, rhs: Unit)

Performs the -= operation. Read more
source§

impl Copy for Duration

source§

impl Eq for Duration

source§

impl StructuralEq for Duration

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable · source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable · source§

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

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere 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

const: unstable · source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

const: unstable · 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.

§

impl<T> Pointable for T

§

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
§

type Init = T

The type for initializers.
§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
§

impl<T> Printing<T> for Twhere T: Display,

§

fn to_str(self) -> String

Method to serialize. Decorates Vecs with square brackets and tuples with round ones. Implementation code is in printing.rs.
§

fn to_plainstr(self) -> String

Method to serialize in minimal form (space separated, no brackets) Implementation code is in printing.rs.
§

fn rd(self) -> String

Printable in red
§

fn gr(self) -> String

Printable in green
§

fn bl(self) -> String

Printable in blue
§

fn yl(self) -> String

Printable in yellow
§

fn mg(self) -> String

Printable in magenta
§

fn cy(self) -> String

Printable in cyan
§

fn wvec(self, f: &mut File) -> Result<(), Error>

Method to write vector(s) to file f (space separated, without brackets). Passes up io errors
§

fn pvec(self)

Method to print vector(s) to stdout (space separated,without brackets).
source§

impl<T> Same<T> for T

§

type Output = T

Should always be Self
§

impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,

§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
§

fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable · source§

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

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

§

impl<T, Right> ClosedAdd<Right> for Twhere T: Add<Right, Output = T> + AddAssign<Right>,

§

impl<T> ClosedNeg for Twhere T: Neg<Output = T>,

§

impl<T, Right> ClosedSub<Right> for Twhere T: Sub<Right, Output = T> + SubAssign<Right>,

source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,

source§

impl<T> Scalar for Twhere T: 'static + Clone + PartialEq<T> + Debug,