Skip to main content

Crate http_date

Crate http_date 

Source
Expand description

Parsing and formatting of HTTP date values as defined in RFC 9110 § 5.6.7.

An HTTP date can appear in one of three formats — IMF-fixdate, RFC 850, or asctime. Use decode to parse a value into a HttpDate:

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());

Values can also be constructed directly from components:

use http_date::{Date, DateTime, DayName, HttpDate, Time, encode};

fn datetime(year: u16) -> DateTime {
    DateTime {
        dayname: DayName::Sun,
        date: Date::new(year, 11, 6).expect("valid date"),
        time: Time::new(8, 49, 37).expect("valid time"),
    }
}

let imf = HttpDate::imf_fixdate(datetime(1994));
assert!(imf.is_imf_fixdate());

// RFC 850 uses a two-digit year; construction rejects anything larger.
let rfc850 = HttpDate::rfc850(datetime(94)).expect("year is 0-99");
assert!(rfc850.is_rfc850());

let asctime = HttpDate::asctime(datetime(1994));
assert!(asctime.is_asctime());

assert_eq!(encode(&imf), "Sun, 06 Nov 1994 08:49:37 GMT");
assert_eq!(encode(&rfc850), "Sunday, 06-Nov-94 08:49:37 GMT");
assert_eq!(encode(&asctime), "Sun Nov  6 08:49:37 1994");

The decoded components are available on the DateTime inside the returned HttpDate.

Structs§

Date
The calendar date portion of an HTTP date.
DateTime
A fully decoded HTTP date: weekday, calendar date, and time of day.
Error
An error returned when an HTTP date cannot be parsed.
HttpDate
An HTTP date in one of the three formats defined in RFC 9110 § 5.6.7.
Time
The time-of-day portion of an HTTP date.

Enums§

DayName
The day of the week as used in HTTP date values.

Functions§

decode
Parses an HTTP date from its textual representation.
encode
Formats an HTTP date as a string in its original textual format.