iso8601-duration-serde

Crates.ioiso8601-duration-serde
lib.rsiso8601-duration-serde
version0.1.0
created_at2025-10-19 10:25:47.316981+00
updated_at2025-10-19 10:25:47.316981+00
descriptionA Rust library for serializing and deserializing time::Duration using the ISO 8601 format.
homepage
repositoryhttps://github.com/windtail/iso8601-duration-serde
max_upload_size
id1890316
size16,252
Luo Jiejun (windtail)

documentation

README

iso8601-duration-serde

Crates.io License

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:

  • P1D represents 1 day
  • PT1H represents 1 hour
  • PT30M represents 30 minutes
  • PT45S represents 45 seconds
  • P2DT3H30M15S represents 2 days, 3 hours, 30 minutes, and 15 seconds

Example

use serde::{Serialize, Deserialize};
use time::Duration;

#[derive(Serialize, Deserialize)]
struct Example {
    #[serde(with = "iso8601_duration_serde")]
    duration: Duration,
}

// Serialization
let example = Example {
    duration: Duration::days(1) + Duration::hours(2) + Duration::minutes(30),
};

let json = serde_json::to_string(&example)?;
// json will be: {"duration":"P1DT2H30M"}

// Deserialization
let json = r#"{"duration":"PT45S"}"#;
let example: Example = serde_json::from_str(json)?;
// 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

License

This project is licensed under the MIT License - see the LICENSE file for details.

Commit count: 0

cargo fmt