Struct Duration

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

The duration which is returned by the parser

The Duration of this library implements conversions to a std::time::Duration and if the feature is activated into a time::Duration respectively chrono::Duration. This crates duration is a superset of the aforementioned durations, so converting to fundu’s duration with From or Into is lossless. Converting from crate::time::Duration to the other durations can overflow the other duration’s value range, but TryFrom is implement for all of these durations. Note that fundu’s duration also implements SaturatingInto for the above durations which performs the conversion saturating at the maximum or minimum of these durations.

§Examples

Basic conversions from Duration to std::time::Duration.

use std::time::Duration as StdDuration;

use fundu_core::error::TryFromDurationError;
use fundu_core::time::{Duration, SaturatingInto};

let result: Result<StdDuration, TryFromDurationError> = Duration::positive(1, 2).try_into();
assert_eq!(result, Ok(StdDuration::new(1, 2)));

let result: Result<StdDuration, TryFromDurationError> = Duration::negative(1, 2).try_into();
assert_eq!(result, Err(TryFromDurationError::NegativeDuration));

let duration: StdDuration = Duration::negative(1, 2).saturating_into();
assert_eq!(duration, StdDuration::ZERO);

let duration: StdDuration = Duration::MAX.saturating_into();
assert_eq!(duration, StdDuration::MAX);

Implementations§

Source§

impl Duration

Source

pub const ZERO: Self

A duration of zero time

§Examples
use fundu_core::time::Duration;

let duration = Duration::ZERO;
assert!(duration.is_zero());
Source

pub const MIN: Self

The minimum duration

§Examples
use fundu_core::time::Duration;

let duration = Duration::MIN;
assert_eq!(Duration::negative(u64::MAX, 999_999_999), duration);
Source

pub const MAX: Self

The maximum duration

§Examples
use fundu_core::time::Duration;

let duration = Duration::MAX;
assert_eq!(Duration::positive(u64::MAX, 999_999_999), duration);
Source

pub const fn from_std(is_negative: bool, inner: Duration) -> Self

Creates a new Duration from a std::time::Duration which can be negative or positive

§Examples
use fundu_core::time::Duration;

let duration = Duration::from_std(false, std::time::Duration::new(1, 0));
assert_eq!(Duration::positive(1, 0), duration);

let duration = Duration::from_std(true, std::time::Duration::new(1, 0));
assert_eq!(Duration::negative(1, 0), duration);
Source

pub const fn positive(secs: u64, nanos: u32) -> Self

Creates a new positive Duration

§Panics

This constructor will panic if creating a std::time::Duration with the same parameters would panic

§Examples
use fundu_core::time::Duration;

let duration = Duration::positive(1, 0);
assert!(duration.is_positive());

let duration = Duration::positive(0, 0);
assert!(duration.is_positive());
Source

pub const fn negative(secs: u64, nanos: u32) -> Self

Creates a new negative Duration

§Panics

This constructor will panic if creating a std::time::Duration with the same parameters would panic

§Examples
use fundu_core::time::Duration;

let duration = Duration::negative(1, 0);
assert!(duration.is_negative());
Source

pub const fn as_weeks(&self) -> i64

Return the number of whole weeks in the Duration

§Examples
use fundu_core::time::Duration;

assert_eq!(Duration::positive(86400 * 7, 0).as_weeks(), 1);
assert_eq!(Duration::negative(86400 * 7, 0).as_weeks(), -1);
assert_eq!(Duration::positive(1, 0).as_weeks(), 0);
assert_eq!(Duration::positive(1_500_000, 0).as_weeks(), 2);
Source

pub const fn as_days(&self) -> i64

Return the number of whole days in the Duration

§Examples
use fundu_core::time::Duration;

assert_eq!(Duration::positive(86400, 0).as_days(), 1);
assert_eq!(Duration::negative(86400, 0).as_days(), -1);
assert_eq!(Duration::positive(1, 0).as_days(), 0);
assert_eq!(Duration::positive(1_500_000, 0).as_days(), 17);
Source

pub const fn as_hours(&self) -> i64

Return the number of whole hours in the Duration

§Examples
use fundu_core::time::Duration;

assert_eq!(Duration::positive(3600, 0).as_hours(), 1);
assert_eq!(Duration::negative(3600, 0).as_hours(), -1);
assert_eq!(Duration::positive(1, 0).as_hours(), 0);
assert_eq!(Duration::positive(1_500_000, 0).as_hours(), 416);
Source

pub const fn as_minutes(&self) -> i64

Return the number of whole minutes in the Duration

§Examples
use fundu_core::time::Duration;

assert_eq!(Duration::positive(60, 0).as_minutes(), 1);
assert_eq!(Duration::negative(60, 0).as_minutes(), -1);
assert_eq!(Duration::positive(1, 0).as_minutes(), 0);
assert_eq!(Duration::positive(1_500_000, 0).as_minutes(), 25_000);
Source

pub const fn as_seconds(&self) -> i128

Return the number of whole seconds in the Duration

§Examples
use fundu_core::time::Duration;

assert_eq!(Duration::positive(1, 0).as_seconds(), 1);
assert_eq!(Duration::negative(1, 0).as_seconds(), -1);
assert_eq!(Duration::positive(0, 1_000_000).as_seconds(), 0);
assert_eq!(Duration::positive(1_500_000, 0).as_seconds(), 1_500_000);
Source

pub const fn as_millis(&self) -> i128

Return the total number of whole milliseconds in the Duration

§Examples
use fundu_core::time::Duration;

assert_eq!(Duration::positive(1, 0).as_millis(), 1_000);
assert_eq!(Duration::negative(1, 0).as_millis(), -1_000);
assert_eq!(Duration::positive(0, 1_000_000).as_millis(), 1);
assert_eq!(Duration::positive(12, 3_000_000).as_millis(), 12_003);
Source

pub const fn as_micros(&self) -> i128

Return the total number of whole microseconds in the Duration

§Examples
use fundu_core::time::Duration;

assert_eq!(Duration::positive(1, 0).as_micros(), 1_000_000);
assert_eq!(Duration::negative(1, 0).as_micros(), -1_000_000);
assert_eq!(Duration::positive(0, 1_000).as_micros(), 1);
assert_eq!(Duration::positive(12, 3_000).as_micros(), 12_000_003);
Source

pub const fn as_nanos(&self) -> i128

Return the total number of nanoseconds in the Duration

§Examples
use fundu_core::time::Duration;

assert_eq!(Duration::positive(1, 0).as_nanos(), 1_000_000_000);
assert_eq!(Duration::negative(1, 0).as_nanos(), -1_000_000_000);
assert_eq!(Duration::positive(0, 1).as_nanos(), 1);
assert_eq!(Duration::positive(12, 3).as_nanos(), 12_000_000_003);
Source

pub const fn subsec_millis(&self) -> i32

Return the fractional part of the Duration in whole milliseconds

§Examples
use fundu_core::time::Duration;

assert_eq!(Duration::positive(0, 123_456_789).subsec_millis(), 123);
assert_eq!(Duration::negative(0, 123_456_789).subsec_millis(), -123);
assert_eq!(Duration::positive(1, 0).subsec_millis(), 0);
assert_eq!(Duration::positive(0, 1).subsec_millis(), 0);
Source

pub const fn subsec_micros(&self) -> i32

Return the fractional part of the Duration in whole microseconds

§Examples
use fundu_core::time::Duration;

assert_eq!(Duration::positive(0, 123_456_789).subsec_micros(), 123_456);
assert_eq!(Duration::negative(0, 123_456_789).subsec_micros(), -123_456);
assert_eq!(Duration::positive(1, 0).subsec_micros(), 0);
assert_eq!(Duration::positive(0, 1).subsec_micros(), 0);
Source

pub const fn subsec_nanos(&self) -> i32

Return the fractional part of the Duration in nanoseconds

§Examples
use fundu_core::time::Duration;

assert_eq!(Duration::positive(0, 123_456_789).subsec_nanos(), 123_456_789);
assert_eq!(Duration::negative(0, 123_456_789).subsec_nanos(), -123_456_789);
assert_eq!(Duration::positive(1, 0).subsec_nanos(), 0);
assert_eq!(Duration::positive(0, 1).subsec_nanos(), 1);
Source

pub fn extract_weeks(&mut self) -> i64

Return the number of whole weeks in this Duration and reduce it by that amount

§Examples
use fundu_core::time::Duration;

let mut duration = Duration::positive(86400 * 7, 0);
assert_eq!(duration.extract_weeks(), 1);
assert_eq!(duration, Duration::ZERO);

let mut duration = Duration::positive(123, 456);
assert_eq!(duration.extract_weeks(), 0);
assert_eq!(duration, Duration::positive(123, 456));
Source

pub fn extract_days(&mut self) -> i64

Return the number of whole days in this Duration and reduce it by that amount

§Examples
use fundu_core::time::Duration;

let mut duration = Duration::positive(86400, 0);
assert_eq!(duration.extract_days(), 1);
assert_eq!(duration, Duration::ZERO);

let mut duration = Duration::positive(123, 456);
assert_eq!(duration.extract_days(), 0);
assert_eq!(duration, Duration::positive(123, 456));
Source

pub fn extract_hours(&mut self) -> i64

Return the number of whole hours in this Duration and reduce it by that amount

§Examples
use fundu_core::time::Duration;

let mut duration = Duration::positive(3600, 0);
assert_eq!(duration.extract_hours(), 1);
assert_eq!(duration, Duration::ZERO);

let mut duration = Duration::positive(123, 456);
assert_eq!(duration.extract_hours(), 0);
assert_eq!(duration, Duration::positive(123, 456));
Source

pub fn extract_minutes(&mut self) -> i64

Return the number of whole minutes in this Duration and reduce it by that amount

§Examples
use fundu_core::time::Duration;

let mut duration = Duration::positive(60, 0);
assert_eq!(duration.extract_minutes(), 1);
assert_eq!(duration, Duration::ZERO);

let mut duration = Duration::positive(12, 456);
assert_eq!(duration.extract_minutes(), 0);
assert_eq!(duration, Duration::positive(12, 456));
Source

pub fn extract_seconds(&mut self) -> i128

Return the number of seconds in this Duration and reduce it by that amount

§Examples
use fundu_core::time::Duration;

let mut duration = Duration::positive(1, 0);
assert_eq!(duration.extract_seconds(), 1);
assert_eq!(duration, Duration::ZERO);

let mut duration = Duration::positive(12, 456);
assert_eq!(duration.extract_seconds(), 12);
assert_eq!(duration, Duration::positive(0, 456));
Source

pub const fn is_negative(&self) -> bool

Returns true if the Duration is negative

§Examples
use fundu_core::time::Duration;

let duration = Duration::MIN;
assert!(duration.is_negative());

let duration = Duration::negative(0, 1);
assert!(duration.is_negative());
Source

pub const fn is_positive(&self) -> bool

Returns true if the Duration is positive

§Examples
use fundu_core::time::Duration;

let duration = Duration::ZERO;
assert!(duration.is_positive());

let duration = Duration::positive(0, 1);
assert!(duration.is_positive());
Source

pub const fn is_zero(&self) -> bool

Returns true if the Duration is zero

§Examples
use fundu_core::time::Duration;

let duration = Duration::ZERO;
assert!(duration.is_zero());

let duration = Duration::positive(0, 0);
assert!(duration.is_zero());

let duration = Duration::negative(0, 0);
assert!(duration.is_zero());
Source

pub const fn abs(&self) -> Self

Returns the absolute value of the duration

This operation is lossless.

§Examples
use fundu_core::time::Duration;

let duration = Duration::MIN;
assert_eq!(duration.abs(), Duration::MAX);

let duration = Duration::negative(1, 0);
assert_eq!(duration.abs(), Duration::positive(1, 0));

let duration = Duration::positive(1, 0);
assert_eq!(duration.abs(), Duration::positive(1, 0));
Source

pub fn checked_add(&self, other: Self) -> Option<Self>

Sums this duration with the other duration, returning None if an overflow occurred

§Examples
use fundu_core::time::Duration;

assert_eq!(
    Duration::positive(1, 0).checked_add(Duration::positive(1, 0)),
    Some(Duration::positive(2, 0))
);
assert_eq!(
    Duration::positive(u64::MAX, 0).checked_add(Duration::positive(1, 0)),
    None
);
assert_eq!(
    Duration::negative(u64::MAX, 0).checked_add(Duration::negative(1, 0)),
    None
);
Source

pub fn checked_sub(&self, other: Self) -> Option<Self>

Subtracts this duration with the other duration, returning None if an overflow occurred

§Examples
use fundu_core::time::Duration;

assert_eq!(
    Duration::positive(1, 0).checked_sub(Duration::positive(1, 0)),
    Some(Duration::ZERO)
);
assert_eq!(
    Duration::negative(u64::MAX, 0).checked_sub(Duration::positive(1, 0)),
    None
);
Source

pub fn saturating_add(&self, other: Self) -> Self

Saturating Duration addition. Computes self + other, returning Duration::MAX or Duration::MIN if an overflow occurred.

§Examples
use fundu_core::time::Duration;

assert_eq!(
    Duration::positive(1, 0).saturating_add(Duration::positive(0, 1)),
    Duration::positive(1, 1)
);
assert_eq!(
    Duration::positive(u64::MAX, 0).saturating_add(Duration::positive(1, 0)),
    Duration::MAX
);
Source

pub fn saturating_sub(&self, other: Self) -> Self

Saturating Duration subtraction. Computes self - other, returning Duration::MAX or Duration::MIN if an overflow occurred.

§Examples
use fundu_core::time::Duration;

assert_eq!(
    Duration::positive(1, 0).saturating_sub(Duration::positive(1, 0)),
    Duration::ZERO
);
assert_eq!(
    Duration::negative(u64::MAX, 0).saturating_sub(Duration::positive(1, 0)),
    Duration::MIN
);

Trait Implementations§

Source§

impl Add for Duration

Source§

type Output = Duration

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Self) -> Self::Output

Performs the + operation. Read more
Source§

impl AddAssign for Duration

Source§

fn add_assign(&mut self, rhs: Self)

Performs the += operation. Read more
Source§

impl Clone for Duration

Source§

fn clone(&self) -> Duration

Returns a duplicate 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

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

impl Default for Duration

Source§

fn default() -> Duration

Returns the “default value” for a type. Read more
Source§

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

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::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

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

impl From<Duration> for Duration

Convert a std::time::Duration into a Duration

Source§

fn from(duration: Duration) -> Self

Converts to this type from the input type.
Source§

impl From<Duration> for Duration

Available on crate feature time only.

Convert a time::Duration into a Duration

Source§

fn from(duration: Duration) -> Self

Converts to this type from the input type.
Source§

impl From<TimeDelta> for Duration

Available on crate feature chrono only.

Convert a chrono::Duration into a Duration

Source§

fn from(duration: Duration) -> Self

Converts to this type from the input type.
Source§

impl Hash for Duration

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Neg for Duration

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

fn neg(self) -> Self::Output

Performs the unary - operation. Read more
Source§

impl Ord for Duration

Source§

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

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

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

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

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

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

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

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

impl PartialEq for Duration

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Duration

Source§

fn partial_cmp(&self, other: &Self) -> 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

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

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

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl SaturatingInto<Duration> for Duration

Source§

fn saturating_into(self) -> Duration

Performs the saturating conversion
Source§

impl SaturatingInto<Duration> for Duration

Available on crate feature time only.
Source§

fn saturating_into(self) -> Duration

Performs the saturating conversion
Source§

impl SaturatingInto<TimeDelta> for Duration

Available on crate feature chrono only.
Source§

fn saturating_into(self) -> Duration

Performs the saturating conversion
Source§

impl Serialize for Duration

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub for Duration

Source§

type Output = Duration

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Self) -> Self::Output

Performs the - operation. Read more
Source§

impl SubAssign for Duration

Source§

fn sub_assign(&mut self, rhs: Self)

Performs the -= operation. Read more
Source§

impl TryFrom<&Duration> for Duration

Available on crate feature time only.

Convert a Duration into a time::Duration

Source§

type Error = TryFromDurationError

The type returned in the event of a conversion error.
Source§

fn try_from(duration: &Duration) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<&Duration> for Duration

Available on crate feature chrono only.

Convert a Duration into a chrono::Duration

Source§

type Error = TryFromDurationError

The type returned in the event of a conversion error.
Source§

fn try_from(duration: &Duration) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Duration> for Duration

Convert a Duration into a std::time::Duration

Source§

type Error = TryFromDurationError

The type returned in the event of a conversion error.
Source§

fn try_from(duration: Duration) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Duration> for Duration

Available on crate feature time only.

Convert a Duration into a time::Duration

Source§

type Error = TryFromDurationError

The type returned in the event of a conversion error.
Source§

fn try_from(duration: Duration) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl TryFrom<Duration> for Duration

Available on crate feature chrono only.

Convert a Duration into a chrono::Duration

Source§

type Error = TryFromDurationError

The type returned in the event of a conversion error.
Source§

fn try_from(duration: Duration) -> Result<Self, Self::Error>

Performs the conversion.
Source§

impl Copy for Duration

Source§

impl Eq for Duration

Auto Trait Implementations§

Blanket Implementations§

Source§

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

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

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

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

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

Source§

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

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

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

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.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

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

Source§

fn to_string(&self) -> String

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

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

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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

Source§

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

The type returned in the event of a conversion error.
Source§

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

Performs the conversion.
Source§

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