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.
- 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.
- 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
sourceimpl Duration
impl Duration
sourcepub fn from_parts(centuries: i16, nanoseconds: u64) -> Duration
pub fn from_parts(centuries: i16, nanoseconds: u64) -> Duration
Create a normalized duration from its parts
sourcepub const fn to_parts(&self) -> (i16, u64)
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.
sourcepub fn from_total_nanoseconds(nanos: i128) -> Duration
pub fn from_total_nanoseconds(nanos: i128) -> Duration
Converts the total nanoseconds as i128 into this Duration (saving 48 bits)
sourcepub fn total_nanoseconds(&self) -> i128
pub fn total_nanoseconds(&self) -> i128
Returns the total nanoseconds in a signed 128 bit integer
sourcepub fn try_truncated_nanoseconds(&self) -> Result<i64, Errors>
pub fn try_truncated_nanoseconds(&self) -> Result<i64, Errors>
Returns the truncated nanoseconds in a signed 64 bit integer, if the duration fits.
sourcepub fn truncated_nanoseconds(&self) -> i64
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
sourcepub fn from_truncated_nanoseconds(nanos: i64) -> Duration
pub fn from_truncated_nanoseconds(nanos: i64) -> Duration
Create a new duration from the truncated nanoseconds (+/- 2927.1 years of duration)
sourcepub fn from_f64(value: f64, unit: Unit) -> Duration
pub fn from_f64(value: f64, unit: Unit) -> Duration
Creates a new duration from the provided unit
sourcepub fn in_seconds(&self) -> f64
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.
sourcepub fn in_unit(&self, unit: Unit) -> f64
pub fn in_unit(&self, unit: Unit) -> f64
Returns the value of this duration in the requested unit.
sourcepub fn new(centuries: i16, nanoseconds: u64) -> Duration
pub fn new(centuries: i16, nanoseconds: u64) -> Duration
Builds a new duration from the number of centuries and the number of nanoseconds
sourcepub fn decompose(&self) -> (i8, u64, u64, u64, u64, u64, u64, u64)
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
sourcepub fn compose(
sign: i8,
days: u64,
hours: u64,
minutes: u64,
seconds: u64,
milliseconds: u64,
microseconds: u64,
nanoseconds: u64
) -> Duration
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
sourcepub fn floor(&self, duration: Duration) -> Duration
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());
sourcepub fn ceil(&self, duration: Duration) -> Duration
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());
sourcepub fn round(&self, duration: Duration) -> Duration
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());
sourcepub const MAX: Duration = Self{centuries: i16::MAX, nanoseconds: NANOSECONDS_PER_CENTURY,}
pub const MAX: Duration = Self{centuries: i16::MAX, nanoseconds: NANOSECONDS_PER_CENTURY,}
Maximum duration that can be represented
sourcepub const MIN: Duration = Self{centuries: i16::MIN, nanoseconds: NANOSECONDS_PER_CENTURY,}
pub const MIN: Duration = Self{centuries: i16::MIN, nanoseconds: NANOSECONDS_PER_CENTURY,}
Minimum duration that can be represented
sourcepub const EPSILON: Duration = Self{centuries: 0, nanoseconds: 1,}
pub const EPSILON: Duration = Self{centuries: 0, nanoseconds: 1,}
Smallest duration that can be represented
sourcepub const MIN_POSITIVE: Duration = Self::EPSILON
pub const MIN_POSITIVE: Duration = Self::EPSILON
Minimum positive duration is one nanoseconds
sourcepub const MIN_NEGATIVE: Duration = Self{centuries: -1, nanoseconds: NANOSECONDS_PER_CENTURY - 1,}
pub const MIN_NEGATIVE: Duration = Self{centuries: -1, nanoseconds: NANOSECONDS_PER_CENTURY - 1,}
Minimum negative duration is minus one nanosecond
Trait Implementations
sourceimpl AddAssign<Duration> for Duration
impl AddAssign<Duration> for Duration
sourcefn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
Performs the +=
operation. Read more
sourceimpl AddAssign<Duration> for Epoch
impl AddAssign<Duration> for Epoch
sourcefn add_assign(&mut self, duration: Duration)
fn add_assign(&mut self, duration: Duration)
Performs the +=
operation. Read more
sourceimpl AddAssign<Unit> for Duration
impl AddAssign<Unit> for Duration
sourcefn add_assign(&mut self, rhs: Unit)
fn add_assign(&mut self, rhs: Unit)
Performs the +=
operation. Read more
sourceimpl Ord for Duration
impl Ord for Duration
sourceimpl PartialOrd<Duration> for Duration
impl PartialOrd<Duration> for Duration
sourcefn partial_cmp(&self, other: &Duration) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
sourceimpl PartialOrd<Unit> for Duration
impl PartialOrd<Unit> for Duration
sourcefn partial_cmp(&self, unit: &Unit) -> Option<Ordering>
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 · sourcefn lt(&self, other: &Rhs) -> bool
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 · sourcefn le(&self, other: &Rhs) -> bool
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
sourceimpl SubAssign<Duration> for Duration
impl SubAssign<Duration> for Duration
sourcefn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
Performs the -=
operation. Read more
sourceimpl SubAssign<Duration> for Epoch
impl SubAssign<Duration> for Epoch
sourcefn sub_assign(&mut self, duration: Duration)
fn sub_assign(&mut self, duration: Duration)
Performs the -=
operation. Read more
sourceimpl SubAssign<Unit> for Duration
impl SubAssign<Unit> for Duration
sourcefn sub_assign(&mut self, rhs: Unit)
fn sub_assign(&mut self, rhs: Unit)
Performs the -=
operation. Read more
impl Copy for Duration
impl Eq for Duration
impl StructuralEq for Duration
Auto Trait Implementations
impl RefUnwindSafe for Duration
impl Send for Duration
impl Sync for Duration
impl Unpin for Duration
impl UnwindSafe for Duration
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SP where
SS: SubsetOf<SP>,
fn to_subset(&self) -> Option<SS>
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
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
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
fn from_subset(element: &SS) -> SP
The inclusion map: converts self
to the equivalent element of its superset.