2 unstable releases

0.2.0 Mar 19, 2023
0.1.0 Oct 1, 2019

#249 in Date and time

Download history 3388/week @ 2023-12-06 3107/week @ 2023-12-13 1827/week @ 2023-12-20 1498/week @ 2023-12-27 2852/week @ 2024-01-03 2078/week @ 2024-01-10 2345/week @ 2024-01-17 1963/week @ 2024-01-24 2660/week @ 2024-01-31 5607/week @ 2024-02-07 5242/week @ 2024-02-14 5792/week @ 2024-02-21 4475/week @ 2024-02-28 2367/week @ 2024-03-06 2907/week @ 2024-03-13 2405/week @ 2024-03-20

13,431 downloads per month
Used in 6 crates (2 directly)

MIT license

19KB
365 lines

Build Status docs.rs crates.io

iso8601-duration

Parse ISO8601 duration format.

https://en.wikipedia.org/wiki/ISO_8601#Durations

Installation

iso8601-duration = "0.2.0"

Usage

use iso8601_duration::Duration;

 assert_eq!(
     "P3Y6M4DT12H30M5S".parse(),
     Ok(Duration::new(3., 6., 4., 12., 30., 5.))
 );
 assert_eq!("P23DT23H".parse::<Duration>().unwrap().num_hours(), Some(575.));
 assert_eq!("P0.5Y".parse::<Duration>().unwrap().num_years(), Some(0.5));
 assert_eq!("P0.5Y0.5M".parse::<Duration>().unwrap().num_months(), Some(6.5));
 assert_eq!("P12W".parse::<Duration>().unwrap().num_days(), Some(84.));

 assert!("PT".parse::<Duration>().is_err());
 assert!("P12WT12H30M5S".parse::<Duration>().is_err());
 assert!("P0.5S0.5M".parse::<Duration>().is_err());
 assert!("P0.5A".parse::<Duration>().is_err());

year and month

Duration can be converted to either std::time::Duration or chrono::Duration by calling to_std or to_chrono.

Both to_std and to_chrono will return None if the duration includes year and month. Because ISO8601 duration format allows the usage of year and month, and these durations are non-standard. Since months can have 28, 29 30, 31 days, and years can have either 365 or 366 days.

To perform a lossless conversion, a starting date must be specified:

// requires `chrono` feature

use iso8601_duration::Duration;
use chrono::DateTime;

let one_month: Duration = "P1M".parse().unwrap();
let date = DateTime::parse_from_rfc3339("2000-02-01T00:00:00Z").unwrap();
assert_eq!(
    one_month.to_chrono_at_datetime(date).num_days(),
    29 // 2000 is a leap year
);

License: MIT

Dependencies

~0.8–1.5MB
~27K SLoC