Struct ixdtf::parsers::IsoDurationParser
source · pub struct IsoDurationParser<'a> { /* private fields */ }
Expand description
A parser for ISO8601 Duration strings.
✨ Enabled with the duration
Cargo feature.
§Example
use ixdtf::parsers::{IsoDurationParser, records::{Sign, DurationParseRecord, TimeDurationRecord}};
let duration_str = "P1Y2M1DT2H10M30S";
let result = IsoDurationParser::from_str(duration_str).parse().unwrap();
let date_duration = result.date.unwrap();
let (hours, minutes, seconds, fraction) = match result.time {
// Hours variant is defined as { hours: u32, fraction: u64 }
Some(TimeDurationRecord::Hours{ hours, fraction }) => (hours, 0, 0, fraction),
// Minutes variant is defined as { hours: u32, minutes: u32, fraction: u64 }
Some(TimeDurationRecord::Minutes{ hours, minutes, fraction }) => (hours, minutes, 0, fraction),
// Seconds variant is defined as { hours: u32, minutes: u32, seconds: u32, fraction: u32 }
Some(TimeDurationRecord::Seconds{ hours, minutes, seconds, fraction }) => (hours, minutes, seconds, fraction as u64),
None => (0,0,0,0),
};
assert_eq!(result.sign, Sign::Positive);
assert_eq!(date_duration.years, 1);
assert_eq!(date_duration.months, 2);
assert_eq!(date_duration.weeks, 0);
assert_eq!(date_duration.days, 1);//
assert_eq!(hours, 2);
assert_eq!(minutes, 10);
assert_eq!(seconds, 30);
assert_eq!(fraction, 0);
Implementations§
source§impl<'a> IsoDurationParser<'a>
impl<'a> IsoDurationParser<'a>
sourcepub fn from_utf8(source: &'a [u8]) -> Self
pub fn from_utf8(source: &'a [u8]) -> Self
Creates a new IsoDurationParser
from a slice of utf-8 bytes.
sourcepub fn parse(&mut self) -> ParserResult<DurationParseRecord>
pub fn parse(&mut self) -> ParserResult<DurationParseRecord>
Parse the contents of this IsoDurationParser
into a DurationParseRecord
.
§Examples
§Parsing a date duration
let date_duration = "P1Y2M3W1D";
let result = IsoDurationParser::from_str(date_duration).parse().unwrap();
let date_duration = result.date.unwrap();
assert!(result.time.is_none());
assert_eq!(date_duration.years, 1);
assert_eq!(date_duration.months, 2);
assert_eq!(date_duration.weeks, 3);
assert_eq!(date_duration.days, 1);
§Parsing a time duration
let time_duration = "PT2H10M30S";
let result = IsoDurationParser::from_str(time_duration).parse().unwrap();
let (hours, minutes, seconds, fraction) = match result.time {
// Hours variant is defined as { hours: u32, fraction: u64 }
Some(TimeDurationRecord::Hours{ hours, fraction }) => (hours, 0, 0, fraction),
// Minutes variant is defined as { hours: u32, minutes: u32, fraction: u64 }
Some(TimeDurationRecord::Minutes{ hours, minutes, fraction }) => (hours, minutes, 0, fraction),
// Seconds variant is defined as { hours: u32, minutes: u32, seconds: u32, fraction: u32 }
Some(TimeDurationRecord::Seconds{ hours, minutes, seconds, fraction }) => (hours, minutes, seconds, fraction as u64),
None => (0,0,0,0),
};
assert!(result.date.is_none());
assert_eq!(hours, 2);
assert_eq!(minutes, 10);
assert_eq!(seconds, 30);
assert_eq!(fraction, 0);
Trait Implementations§
Auto Trait Implementations§
impl<'a> Freeze for IsoDurationParser<'a>
impl<'a> RefUnwindSafe for IsoDurationParser<'a>
impl<'a> Send for IsoDurationParser<'a>
impl<'a> Sync for IsoDurationParser<'a>
impl<'a> Unpin for IsoDurationParser<'a>
impl<'a> UnwindSafe for IsoDurationParser<'a>
Blanket Implementations§
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more