http-date
Parsing and formatting of HTTP date values as defined in RFC 9110 § 5.6.7.
The three HTTP date formats are supported, and the format a value was parsed from is preserved so that re-encoding reproduces the original representation:
| Format | Example | Notes |
|---|---|---|
| IMF-fixdate | Sun, 06 Nov 1994 08:49:37 GMT |
Preferred; the only format senders must generate |
| RFC 850 | Sunday, 06-Nov-94 08:49:37 GMT |
Obsolete; two-digit year (0–99) |
| asctime | Sun Nov 6 08:49:37 1994 |
Obsolete; space-padded day |
decode enforces the grammar strictly (case-sensitive day/month names, exact
fixed-width fields, no trailing data) and validates component ranges.
HttpDate is constructor-only, so every value is guaranteed well-formed and
encode cannot fail.
Usage
Parsing
use decode;
let date = decode.unwrap;
assert!;
All three formats are accepted:
use decode;
assert!;
assert!;
Formatting
use ;
let date = decode.unwrap;
assert_eq!;
Building from components
use ;
let date = imf_fixdate;
assert_eq!;
HttpDate::rfc850 returns a Result because RFC 850 can only represent the
years 0–99; HttpDate::imf_fixdate and HttpDate::asctime are infallible.
Chrono integration
Enabled with the chrono feature:
[]
= { = "0.2", = ["chrono"] }
Convert HttpDate to a UTC datetime and back:
use ;
use decode;
let date = decode.unwrap;
// HttpDate -> chrono::DateTime<Utc>
let chrono: = try_from.unwrap;
assert_eq!;
// chrono::DateTime<Utc> -> HttpDate (always IMF-fixdate)
let back = try_from.unwrap;
assert!;
Notes:
- RFC 850's two-digit year is interpreted with the POSIX century window
(
69–99→ 1900s,0–68→ 2000s). - Converting to chrono fails on impossible calendar dates (e.g. 31 February),
which
decodeaccepts but chrono does not. - Converting from chrono fails for years outside 0–9999, which cannot be represented in a four-digit HTTP date year.
Features
| Feature | Default | Description |
|---|---|---|
chrono |
off | Conversions between HttpDate and chrono::DateTime<Utc> |
Development
The project uses a Nix flake with two dev shells, managed via direnv:
Common tasks are wrapped in a justfile:
Fuzzing uses cargo-fuzz with two
targets: roundtrip (decode never panics and encode/decode is stable) and
chrono_roundtrip (chrono conversion fixed point):
License
Licensed under the Apache License, Version 2.0 (http://www.apache.org/licenses/LICENSE-2.0).