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§
source§impl 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 ZERO: Duration = Self{ centuries: 0, nanoseconds: 0,}
pub const ZERO: Duration = Self{ centuries: 0, nanoseconds: 0,}
A duration of exactly zero nanoseconds
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§
source§impl AddAssign<Duration> for Duration
impl AddAssign<Duration> for Duration
source§fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
+=
operation. Read moresource§impl AddAssign<Duration> for Epoch
impl AddAssign<Duration> for Epoch
source§fn add_assign(&mut self, duration: Duration)
fn add_assign(&mut self, duration: Duration)
+=
operation. Read moresource§impl AddAssign<Unit> for Duration
impl AddAssign<Unit> for Duration
source§fn add_assign(&mut self, rhs: Unit)
fn add_assign(&mut self, rhs: Unit)
+=
operation. Read moresource§impl<'de> Deserialize<'de> for Duration
impl<'de> Deserialize<'de> for Duration
source§fn deserialize<D>(
deserializer: D
) -> Result<Duration, <D as Deserializer<'de>>::Error>where
D: Deserializer<'de>,
fn deserialize<D>( deserializer: D ) -> Result<Duration, <D as Deserializer<'de>>::Error>where D: Deserializer<'de>,
source§impl FromStr for Duration
impl FromStr for Duration
source§fn from_str(s: &str) -> Result<Duration, <Duration as FromStr>::Err>
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);
source§impl Ord for Duration
impl Ord for Duration
source§impl PartialEq<Duration> for Duration
impl PartialEq<Duration> for Duration
source§impl PartialOrd<Duration> for Duration
impl PartialOrd<Duration> for Duration
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl PartialOrd<Unit> for Duration
impl PartialOrd<Unit> for Duration
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read moresource§impl SubAssign<Duration> for Duration
impl SubAssign<Duration> for Duration
source§fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
-=
operation. Read moresource§impl SubAssign<Duration> for Epoch
impl SubAssign<Duration> for Epoch
source§fn sub_assign(&mut self, duration: Duration)
fn sub_assign(&mut self, duration: Duration)
-=
operation. Read moresource§impl SubAssign<Unit> for Duration
impl SubAssign<Unit> for Duration
source§fn sub_assign(&mut self, rhs: Unit)
fn sub_assign(&mut self, rhs: Unit)
-=
operation. Read moreimpl 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§
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<T> Pointable for T
impl<T> Pointable for T
§impl<T> Printing<T> for Twhere
T: Display,
impl<T> Printing<T> for Twhere T: Display,
§fn to_str(self) -> String
fn to_str(self) -> String
printing.rs
. §fn to_plainstr(self) -> String
fn to_plainstr(self) -> String
printing.rs
.§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere SS: SubsetOf<SP>,
§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self
from the equivalent element of its
superset. Read more§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
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
self.to_subset
but without any property checks. Always succeeds.§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self
to the equivalent element of its superset.