[go: up one dir, main page]

Struct Duration

Source
pub struct Duration {
    pub positive: bool,
    pub day: u32,
    pub second: u32,
    pub microsecond: u32,
}
Expand description

A Duration

Allowed values:

  • PnYnMnDTnHnMnS - ISO 8601 duration format, see wikipedia for more details, W for weeks is also allowed before the T separator - Note: W is allowed combined with other quantities which is a slight deviation from the ISO 8601 standard.
  • HH:MM:SS - any of the above time formats are allowed to represent a duration
  • D days, HH:MM:SS - time prefixed by X days, case-insensitive, spaces s and , are all optional
  • D d, HH:MM:SS - time prefixed by X d, case-insensitive, spaces and , are optional

All duration formats can be prefixed with + or - to indicate positive and negative durations respectively.

Duration stores durations in days, seconds and microseconds (all ints), therefore durations like years need be scaled when creating a Duration. The following scaling factors are used:

  • Y - 365 days
  • M - 30 days
  • W - 7 days
  • D - 1 day
  • H - 3600 seconds
  • M - 60 seconds
  • S - 1 second

Fractions of quantities are permitted by ISO 8601 in the final quantity included, e.g. P1.5Y or P1Y1.5M. Wen fractions of quantities are found day, second and microsecond are calculated to most accurately represent the fraction. For example P1.123W is represented as

Duration {
   positive: true,
   day: 7,
   second: 74390,
   microsecond: 400_000
}

Fields§

§positive: bool

The positive or negative sign of the duration

§day: u32

The number of days

§second: u32

The number of seconds, range 0 to 86399

§microsecond: u32

The number of microseconds, range 0 to 999999

Implementations§

Source§

impl Duration

Source

pub fn new( positive: bool, day: u32, second: u32, microsecond: u32, ) -> Result<Self, ParseError>

Create a duration from raw values.

§Arguments
  • positive - the positive or negative sign of the duration
  • day - the number of days in the Duration, max allowed value is 999_999_999 to match python’s timedelta
  • second - the number of seconds in the Duration
  • microsecond - the number of microseconds in the Duration

second and microsecond are normalised to be in the ranges 0 to 86_400 and 0 to 999_999 respectively.

Due to the limit on days, the maximum duration which can be represented is P2739726Y9DT86399.999999S, that is 1 microsecond short of 2,739,726 years and 10 days, positive or negative.

§Examples
use speedate::Duration;

let d = Duration::new(false, 1, 86500, 1_000_123).unwrap();
assert_eq!(
    d,
    Duration {
        positive: false,
        day: 2,
        second: 101,
        microsecond: 123,
    }
);
Source

pub fn parse_str(str: &str) -> Result<Self, ParseError>

Parse a duration from a string

§Arguments
  • str - The string to parse
§Examples
use speedate::Duration;

let d = Duration::parse_str("P1YT2.1S").unwrap();
assert_eq!(
    d,
    Duration {
        positive: true,
        day: 365,
        second: 2,
        microsecond: 100_000
    }
);
assert_eq!(d.to_string(), "P1YT2.1S");
Source

pub fn parse_bytes(bytes: &[u8]) -> Result<Self, ParseError>

Parse a duration from bytes

§Arguments
  • bytes - The bytes to parse
§Examples
use speedate::Duration;

let d = Duration::parse_bytes(b"P1Y").unwrap();
assert_eq!(
    d,
    Duration {
        positive: true,
        day: 365,
        second: 0,
        microsecond: 0
    }
);
assert_eq!(d.to_string(), "P1Y");
Source

pub fn parse_bytes_with_config( bytes: &[u8], config: &TimeConfig, ) -> Result<Self, ParseError>

Same as Duration::parse_bytes but with a TimeConfig component.

§Arguments
  • bytes - The bytes to parse
  • config - The TimeConfig to use
§Examples
use speedate::{Duration, TimeConfigBuilder};

let d = Duration::parse_bytes_with_config(b"P1Y", &TimeConfigBuilder::new().build()).unwrap();
assert_eq!(
    d,
    Duration {
        positive: true,
        day: 365,
        second: 0,
        microsecond: 0
    }
);
assert_eq!(d.to_string(), "P1Y");
Source

pub fn signed_total_seconds(&self) -> i64

Total number of seconds in the duration (days + seconds) with sign based on self.positive

Source

pub fn signed_total_ms(&self) -> i64

Total number of milliseconds in the duration (days + seconds) with sign.

Source

pub fn signed_microseconds(&self) -> i32

Microseconds in the duration with sign based on self.positive

Trait Implementations§

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 Display for Duration

Source§

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

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

impl FromStr for Duration

Source§

type Err = ParseError

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

fn from_str(s: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl PartialEq for Duration

Source§

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

Compare two durations by inequality.

Duration supports equality (==, !=) and inequality (>, <, >= & <=) comparisons.

§Example
use speedate::Duration;

let duration = |s| Duration::parse_str(s).unwrap();

let d1 = duration("P3DT4H5M6.7S");
let d2 = duration("P4DT1H");

assert!(d2 > d1);

positive is included in in comparisons, thus +P1D is greater than -P2D, similarly -P2D is less than -P1D.

assert!(duration("+P1D") > duration("-P2D"));
assert!(duration("-P2D") < duration("-P1D"));
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 Eq for Duration

Source§

impl StructuralPartialEq 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.