iso8601-duration-serde
A Rust library for serializing and deserializing time::Duration using the ISO 8601 format.
Features
This library provides a simple way to serialize and deserialize time::Duration types using the widely supported ISO 8601 duration format.
Examples:
P1Drepresents 1 dayPT1Hrepresents 1 hourPT30Mrepresents 30 minutesPT45Srepresents 45 secondsP2DT3H30M15Srepresents 2 days, 3 hours, 30 minutes, and 15 seconds
Example
use ;
use Duration;
// Serialization
let example = Example ;
let json = to_string?;
// json will be: {"duration":"P1DT2H30M"}
// Deserialization
let json = r#"{"duration":"PT45S"}"#;
let example: Example = from_str?;
// example.duration will be Duration::seconds(45)
Limitations
- Year and month durations are not supported as they are not fixed-length in
time::Duration - Attempting to deserialize a duration with years or months will result in an error
Dependencies
- time - Time handling library
- serde - Serialization framework
- iso8601-duration - ISO 8601 duration parsing
License
This project is licensed under the MIT License - see the LICENSE file for details.