humantime-serde

Serde (de)serialization support for human-readable durations and timestamps

Library
Cargo
v1.1.1
40stars
MIT OR Apache-2.0

Repository Health

Pre-computed score based on development activity, maintenance, community, maturity, and trend momentum.How we score it →
23/100Needs Attention
Development Activity0
Maintenance0
Community20
Maturity60
Momentum12

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
65/100Good
Architecture60
Code Quality75
Innovation45
Learning Curve80

humantime-serde bridges the humantime crate’s human-friendly duration and timestamp parsing ("15days", "2h 30m") with Serde, so std::time::Duration and SystemTime fields can be read from and written to config files, JSON, or any Serde-backed format using readable strings instead of raw nanosecond integers.

It’s applied via a #[serde(with = "humantime_serde")] attribute on a struct field, or through a Serde<T> wrapper type for use inside collections like Vec<Serde<SystemTime>>, and has become a near-standard companion crate wherever configuration structs need human-readable time values.

What You Get

  • A #[serde(with = "humantime_serde")] attribute helper for Duration and SystemTime fields on any Serde-derived struct
  • An option submodule providing the same support for Option<Duration>/Option<SystemTime> fields
  • A Serde<T> newtype wrapper for using humantime-formatted values inside collections like Vec<Serde<SystemTime>>
  • A re-exported re::humantime module so consumers can reach the underlying humantime crate without adding it as a direct dependency

Common Use Cases

  • Reading timeout, TTL, or interval fields from a YAML/TOML/JSON config file as "30s" or "5m" instead of raw integers
  • Serializing SystemTime fields to human-readable timestamps in structured logs or config dumps
  • Building CLI tools or daemons whose configuration structs need human-editable duration fields with zero custom parsing code
  • Composing with other Serde-derived structs in libraries that expose duration/time settings as part of their public config API

Under The Hood

Architecture — The crate is two small modules: src/lib.rs (~377 lines) implements top-level serialize/deserialize functions and the Serde<T> wrapper type using Deref/DerefMut to transparently expose the inner Duration/SystemTime, while src/option.rs (~47 lines) re-implements the same pair for Option<T> fields, since Serde’s with attribute needs distinct functions per Option-ness. Tech Stack — Minimal pure-Rust dependencies: humantime for the actual string parsing/formatting and serde (with the derive feature) for the (de)serialization traits; dev-dependencies add serde_json for doctests and version-sync to keep README version badges in sync with Cargo.toml. Code Quality#![forbid(unsafe_code)] is set at the crate root, the public API is documented with runnable doctests demonstrating both the attribute and wrapper-type usage patterns, and lib.rs carries six #[test] functions covering round-trip serialization; the crate has seen no commits since its 1.1.1 release in 2022, though its narrow scope means little churn is expected. API Design — The #[serde(with = "humantime_serde")] attribute pattern requires no boilerplate beyond one derive annotation per field, making it a drop-in addition to existing Serde structs; the separate option module for Option<T> fields is the one wrinkle a user must discover before mixing optional and required time fields.

Join founders buildingwith open source

Opinionated takes, migration guides, cost-saving tips, and insights from the open source ecosystem.

Subscribe on Substack
Join 750+ subscribers

Search