httpdate

A tiny, zero-dependency Rust crate for parsing and formatting the timestamps found in HTTP header fields.

Library
Cargo
v1.0.3
59stars
Apache License 2.0

Repository Health

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

Technical Analysis

AI-assessed by reading the actual repository — architecture, code quality, innovation, and documentation.How we score it →
70/100Good
Architecture70
Code Quality80
Innovation55
Learning Curve75

httpdate provides the two functions almost every Rust HTTP stack eventually needs: parse_http_date and fmt_http_date, converting between std::time::SystemTime and the timestamp formats used in headers like Date, Last-Modified, and If-Modified-Since. It supports the preferred IMF-fixdate format as well as the legacy RFC 850 and ANSI C asctime formats for parsing, and always formats using IMF-fixdate on output, matching the HTTP specification’s recommendations.

The crate exposes an HttpDate type that packs a timestamp into 8 bytes (versus 16 for SystemTime), with Display/FromStr implementations that avoid intermediate allocation. It has zero runtime dependencies, forbids unsafe code, and is widely used as a transitive dependency across the Rust HTTP ecosystem.

What You Get

  • parse_http_date(&str) -> Result<SystemTime, Error> accepting IMF-fixdate, RFC 850, and ascdate formats with two-digit years mapped to 1970-2069
  • fmt_http_date(SystemTime) -> String producing spec-compliant IMF-fixdate output for response headers
  • An HttpDate type implementing Display/FromStr and conversions to/from SystemTime, at half the memory footprint
  • Zero runtime dependencies and #![forbid(unsafe_code)], keeping the crate auditable and lightweight to embed
  • Fuzz-testing setup (cargo-fuzz) and criterion benchmarks included in the repo

Common Use Cases

  • Formatting Date and Last-Modified response headers in a Rust HTTP server
  • Parsing If-Modified-Since/If-Unmodified-Since request headers for conditional-GET caching logic
  • Any Rust HTTP client or server crate (e.g. hyper-adjacent stacks) that needs spec-correct HTTP timestamp handling without pulling in a full datetime library
  • Cookie expiration timestamp formatting, since cookie Expires attributes use the same IMF-fixdate format

Under The Hood

Architecture: The crate is deliberately minimal — src/lib.rs (160 lines) exposes two free functions and re-exports the HttpDate type defined in src/date.rs (420 lines), which implements the actual calendar math, parsing state machine, and Display formatting. HttpDate stores a timestamp in a compact 8-byte representation and converts to/from std::time::SystemTime via TryFrom/Into, so the crate never allocates during formatting except for the final String returned by fmt_http_date. Parsing dispatches across three date grammars (IMF-fixdate, obsolete RFC 850, and ANSI C asctime) using straightforward byte-slice matching rather than a general-purpose parser combinator, keeping the dependency-free footprint intact.

Tech Stack: Pure Rust, edition 2021, MSRV 1.56, with #![forbid(unsafe_code)] and zero runtime dependencies — the only dependency declared anywhere is criterion for dev-time benchmarking. A cargo fuzz harness (fuzz_target_1) is set up for parser robustness testing, and CI runs via GitHub Actions.

Code Quality: Test coverage lives inline alongside the parsing/formatting logic in date.rs, exercising round-trips between SystemTime and each supported date grammar, plus edge cases like two-digit year mapping. The error type is a single opaque Error(()) with a fixed message, a pragmatic choice for a crate with exactly one failure mode (unparseable date string) rather than a proliferation of error variants. Code is small enough (580 lines total) to review end-to-end in minutes, and the forbid(unsafe_code) attribute backs up that simplicity with a compiler-enforced guarantee.

API Design: Two top-level functions cover the 95% use case with no configuration or builder pattern required, and the HttpDate type is available for callers who want to hold a parsed timestamp without immediately converting back to SystemTime. Rustdoc on both public functions states supported formats and the two-digit-year mapping rule directly, so there’s little need to read source to use the crate correctly. The tradeoff is that the API doesn’t expose which of the three date grammars matched, which is rarely needed but not recoverable if a caller wanted it.

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