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,Wfor weeks is also allowed before theTseparator - Note:Wis 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 durationD days, HH:MM:SS- time prefixed byX days, case-insensitive, spacessand,are all optionalD d, HH:MM:SS- time prefixed byX 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 daysM- 30 daysW- 7 daysD- 1 dayH- 3600 secondsM- 60 secondsS- 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: boolThe positive or negative sign of the duration
day: u32The number of days
second: u32The number of seconds, range 0 to 86399
microsecond: u32The number of microseconds, range 0 to 999999
Implementations§
Source§impl Duration
impl Duration
Sourcepub fn new(
positive: bool,
day: u32,
second: u32,
microsecond: u32,
) -> Result<Self, ParseError>
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 durationday- the number of days in theDuration, max allowed value is999_999_999to match python’stimedeltasecond- the number of seconds in theDurationmicrosecond- the number of microseconds in theDuration
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,
}
);Sourcepub fn parse_str(str: &str) -> Result<Self, ParseError>
pub fn parse_str(str: &str) -> Result<Self, ParseError>
Sourcepub fn parse_bytes(bytes: &[u8]) -> Result<Self, ParseError>
pub fn parse_bytes(bytes: &[u8]) -> Result<Self, ParseError>
Sourcepub fn parse_bytes_with_config(
bytes: &[u8],
config: &TimeConfig,
) -> Result<Self, ParseError>
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 parseconfig- TheTimeConfigto 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");Sourcepub fn signed_total_seconds(&self) -> i64
pub fn signed_total_seconds(&self) -> i64
Total number of seconds in the duration (days + seconds) with sign based on self.positive
Sourcepub fn signed_total_ms(&self) -> i64
pub fn signed_total_ms(&self) -> i64
Total number of milliseconds in the duration (days + seconds) with sign.
Sourcepub fn signed_microseconds(&self) -> i32
pub fn signed_microseconds(&self) -> i32
Microseconds in the duration with sign based on self.positive
Trait Implementations§
Source§impl PartialOrd for Duration
impl PartialOrd for Duration
Source§fn partial_cmp(&self, other: &Self) -> Option<Ordering>
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"));