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
impl Duration
impl Duration
pub fn from_parts(centuries: i16, nanoseconds: u64) -> Duration
pub fn from_parts(centuries: i16, nanoseconds: u64) -> Duration
Create a normalized duration from its parts
pub 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.
pub 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)
pub fn total_nanoseconds(&self) -> i128
pub fn total_nanoseconds(&self) -> i128
Returns the total nanoseconds in a signed 128 bit integer
pub 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.
pub 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
pub 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)
pub 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.
pub 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
pub 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
pub 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
pub 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());
pub 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());
pub 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());
pub const ZERO: Duration = Self{centuries: 0, nanoseconds: 0,}
pub const ZERO: Duration = Self{centuries: 0, nanoseconds: 0,}
A duration of exactly zero nanoseconds
pub 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
pub 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
pub const EPSILON: Duration = Self{centuries: 0, nanoseconds: 1,}
pub const EPSILON: Duration = Self{centuries: 0, nanoseconds: 1,}
Smallest duration that can be represented
pub const MIN_POSITIVE: Duration = Self::EPSILON
pub const MIN_POSITIVE: Duration = Self::EPSILON
Minimum positive duration is one nanoseconds
pub 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
impl AddAssign<Duration> for Duration
impl AddAssign<Duration> for Duration
fn add_assign(&mut self, rhs: Duration)
fn add_assign(&mut self, rhs: Duration)
Performs the +=
operation. Read more
impl AddAssign<Duration> for Epoch
impl AddAssign<Duration> for Epoch
fn add_assign(&mut self, duration: Duration)
fn add_assign(&mut self, duration: Duration)
Performs the +=
operation. Read more
impl AddAssign<Unit> for Duration
impl AddAssign<Unit> for Duration
fn add_assign(&mut self, rhs: Unit)
fn add_assign(&mut self, rhs: Unit)
Performs the +=
operation. Read more
impl<'de> Deserialize<'de> for Duration
impl<'de> Deserialize<'de> for Duration
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>,
Deserialize this value from the given Serde deserializer. Read more
impl FromStr for Duration
impl FromStr for Duration
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);
impl Ord for Duration
impl Ord for Duration
1.21.0 · sourcefn max(self, other: Self) -> Self
fn max(self, other: Self) -> Self
Compares and returns the maximum of two values. Read more
1.21.0 · sourcefn min(self, other: Self) -> Self
fn min(self, other: Self) -> Self
Compares and returns the minimum of two values. Read more
1.50.0 · sourcefn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
fn clamp(self, min: Self, max: Self) -> Self where
Self: PartialOrd<Self>,
Restrict a value to a certain interval. Read more
impl PartialOrd<Duration> for Duration
impl PartialOrd<Duration> for Duration
fn 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
impl PartialOrd<Unit> for Duration
impl PartialOrd<Unit> for Duration
fn 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
impl SubAssign<Duration> for Duration
impl SubAssign<Duration> for Duration
fn sub_assign(&mut self, rhs: Duration)
fn sub_assign(&mut self, rhs: Duration)
Performs the -=
operation. Read more
impl SubAssign<Duration> for Epoch
impl SubAssign<Duration> for Epoch
fn sub_assign(&mut self, duration: Duration)
fn sub_assign(&mut self, duration: Duration)
Performs the -=
operation. Read more
impl SubAssign<Unit> for Duration
impl SubAssign<Unit> for Duration
fn 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<T> Pointable for T
impl<T> Pointable for T
impl<T> Printing<T> for T where
T: Display,
impl<T> Printing<T> for T where
T: Display,
fn to_str(self) -> String
fn to_str(self) -> String
Method to serialize generic items, slices, and slices of Vecs.
Adds square brackets around Vecs (prettier lists).
Implementation code is in printing.rs
. Read more
fn to_plainstr(self) -> String
fn to_plainstr(self) -> String
Method to serialize generic items, slices, and slices of Vecs.
Implementation code is in printing.rs
. Read more
fn wvec(self, f: &mut File) -> Result<(), Error>
fn wvec(self, f: &mut File) -> Result<(), Error>
Method to write vector(s) to file f (space separated, without brackets). Passes up io errors Read more
fn pvec(self)
fn pvec(self)
Method to print vector(s) to stdout (space separated,without brackets).
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.