Skip to main content

decode

Function decode 

Source
pub fn decode(buf: &str) -> Result<HttpDate, Error>
Expand description

Parses an HTTP date from its textual representation.

Accepts any of the three date formats defined in RFC 9110 § 5.6.7:

FormatExample
IMF-fixdateSun, 06 Nov 1994 08:49:37 GMT
RFC 850Sunday, 06-Nov-94 08:49:37 GMT
asctimeSun Nov 6 08:49:37 1994

§Errors

Returns a Error if buf is not a well-formed HTTP date, for example when the day name or month is unknown, a required separator is missing, the input is truncated, there is trailing data after the date, or a component is out of range (day 1–31, hour 0–23, minute/second 0–59).

§Examples

An IMF-fixdate (the format HTTP servers must emit):

use http_date::{decode, HttpDate};

let date = decode("Sun, 06 Nov 1994 08:49:37 GMT")
    .expect("valid IMF-fixdate");
assert!(date.is_imf_fixdate());

An RFC 850 date:

use http_date::{decode, HttpDate};

let date = decode("Sunday, 06-Nov-94 08:49:37 GMT")
    .expect("valid RFC 850 date");
assert!(date.is_rfc850());

An asctime date:

use http_date::{decode, HttpDate};

let date = decode("Sun Nov  6 08:49:37 1994")
    .expect("valid asctime date");
assert!(date.is_asctime());

Malformed input is rejected:

use http_date::decode;

assert!(decode("not a date").is_err());