Adds TryFrom<DayTimeDuration> to std::time::Duration

pull/559/head
Tpt 2 years ago
parent 001b6e07b7
commit 98ac089984
  1. 36
      lib/oxsdatatypes/src/duration.rs

@ -522,6 +522,34 @@ impl TryFrom<StdDuration> for DayTimeDuration {
}
}
impl TryFrom<DayTimeDuration> for StdDuration {
type Error = DecimalOverflowError;
#[inline]
fn try_from(value: DayTimeDuration) -> Result<Self, DecimalOverflowError> {
if value.seconds.is_negative() {
return Err(DecimalOverflowError);
}
let secs = value.seconds.floor();
let nanos = value
.seconds
.checked_sub(secs)
.ok_or(DecimalOverflowError)?
.checked_mul(1_000_000_000)
.ok_or(DecimalOverflowError)?
.floor();
Ok(StdDuration::new(
secs.as_i128()
.try_into()
.map_err(|_| DecimalOverflowError)?,
nanos
.as_i128()
.try_into()
.map_err(|_| DecimalOverflowError)?,
))
}
}
impl FromStr for DayTimeDuration {
type Err = XsdParseError;
@ -647,6 +675,14 @@ mod tests {
);
}
#[test]
fn to_std() -> Result<(), XsdParseError> {
let duration = StdDuration::try_from(DayTimeDuration::from_str("PT10.00000001S")?).unwrap();
assert_eq!(duration.as_secs(), 10);
assert_eq!(duration.subsec_nanos(), 10);
Ok(())
}
#[test]
fn equals() -> Result<(), XsdParseError> {
assert_eq!(

Loading…
Cancel
Save