pub struct HttpDate { /* private fields */ }Expand description
A point in time with one-second resolution, from year 1 to year 9999,
UTC; the value of HTTP-date fields (Date, Last-Modified, Expires).
§Examples
use parse_net::HttpDate;
let d = HttpDate::from_unix(0)?;
assert_eq!(&d.format(), b"Thu, 01 Jan 1970 00:00:00 GMT");
assert!(HttpDate::from_unix(HttpDate::MAX.unix() + 1).is_err());Implementations§
Source§impl HttpDate
impl HttpDate
Sourcepub const MIN: Self
pub const MIN: Self
0001-01-01T00:00:00Z.
§Examples
use parse_net::HttpDate;
assert_eq!(&HttpDate::MIN.format(), b"Mon, 01 Jan 0001 00:00:00 GMT");
assert!(HttpDate::from_unix(HttpDate::MIN.unix() - 1).is_err());Sourcepub const MAX: Self
pub const MAX: Self
9999-12-31T23:59:59Z.
§Examples
let c = parse_net::HttpDate::MAX.civil();
assert_eq!((c.year, c.month, c.day, c.hour, c.minute, c.second), (9999, 12, 31, 23, 59, 59));Sourcepub const fn from_unix(secs: i64) -> Result<Self, ParseError>
pub const fn from_unix(secs: i64) -> Result<Self, ParseError>
The date secs seconds after the Unix epoch (negative before 1970).
§Errors
ErrorKind::OutOfRange outside years 1 to 9999.
§Examples
use parse_net::HttpDate;
assert_eq!(HttpDate::from_unix(1_792_540_800)?.civil().weekday, 3); // 2026-10-21, a Wednesday
assert!(HttpDate::from_unix(i64::MAX).is_err());Sourcepub const fn unix(self) -> i64
pub const fn unix(self) -> i64
Seconds since the Unix epoch.
§Examples
let d = parse_net::HttpDate::parse_imf_fixdate(b"Thu, 01 Jan 1970 00:01:00 GMT")?;
assert_eq!(d.unix(), 60);Sourcepub fn civil(self) -> Civil
pub fn civil(self) -> Civil
The broken-down UTC time.
§Examples
let c = parse_net::HttpDate::from_unix(946_684_800)?.civil();
assert_eq!((c.year, c.month, c.day, c.weekday), (2000, 1, 1, 6)); // a SaturdaySourcepub fn format(self) -> [u8; 29]
pub fn format(self) -> [u8; 29]
The IMF-fixdate text, Sun, 06 Nov 1994 08:49:37 GMT.
§Examples
let d = parse_net::HttpDate::from_unix(1_792_540_800)?;
assert_eq!(&d.format(), b"Wed, 21 Oct 2026 00:00:00 GMT");Sourcepub fn write_to(self, out: &mut impl Sink) -> Result<(), WriteError>
pub fn write_to(self, out: &mut impl Sink) -> Result<(), WriteError>
Writes format into out.
§Errors
§Examples
use parse_net::{HttpDate, SliceSink};
let mut buf = [0u8; 32];
let mut out = SliceSink::new(&mut buf);
HttpDate::MIN.write_to(&mut out)?;
assert_eq!(out.written(), b"Mon, 01 Jan 0001 00:00:00 GMT");
assert!(HttpDate::MAX.write_to(&mut SliceSink::new(&mut [0u8; 8])).is_err());Sourcepub fn parse_imf_fixdate(value: &[u8]) -> Result<Self, ParseError>
pub fn parse_imf_fixdate(value: &[u8]) -> Result<Self, ParseError>
Parses exactly an IMF-fixdate (29 bytes after OWS), the only format senders may generate. Needs no clock.
§Errors
ErrorKind::InvalidDate, ErrorKind::WeekdayMismatch,
ErrorKind::OutOfRange (year 0000), ErrorKind::TrailingData.
§Examples
use parse_net::HttpDate;
assert_eq!(HttpDate::parse_imf_fixdate(b" Wed, 21 Oct 2026 07:28:00 GMT ")?.civil().hour, 7);
assert!(HttpDate::parse_imf_fixdate(b"Thu, 21 Oct 2026 07:28:00 GMT").is_err()); // wrong weekday
assert!(HttpDate::parse_imf_fixdate(b"Wednesday, 21-Oct-26 07:28:00 GMT").is_err());Sourcepub fn parse_at(value: &[u8], now: i64) -> Result<Self, ParseError>
pub fn parse_at(value: &[u8], now: i64) -> Result<Self, ParseError>
Parses an HTTP-date in any of its three formats.
The RFC 850 two-digit year is resolved against now (Unix seconds):
a date more than 50 years after now is taken as the most recent past
year with the same last two digits, and a date 50 years or more before
now moves forward a century, so the year lands in the hundred years
around now. Names are case-sensitive, only GMT is accepted, and
the weekday must agree with the date.
§Errors
ErrorKind::InvalidDate, ErrorKind::WeekdayMismatch,
ErrorKind::OutOfRange, ErrorKind::TrailingData.
§Examples
use parse_net::HttpDate;
let now = 1_792_540_800; // 2026-10-21
let d = HttpDate::parse_at(b"Sunday, 06-Nov-94 08:49:37 GMT", now)?;
assert_eq!(d.civil().year, 1994);
assert_eq!(d, HttpDate::parse_at(b"Sun Nov 6 08:49:37 1994", now)?);Sourcepub fn parse_at_with(
value: &[u8],
now: i64,
limits: &Limits,
) -> Result<Self, ParseError>
pub fn parse_at_with( value: &[u8], now: i64, limits: &Limits, ) -> Result<Self, ParseError>
As parse_at, with Limits::allow_leap_second
taken from limits.
§Errors
As parse_at.
§Examples
use parse_net::{HttpDate, Limits};
let leap = b"Sat, 31 Dec 2016 23:59:60 GMT";
assert!(HttpDate::parse_at(leap, 0).is_err());
assert!(HttpDate::parse_at_with(leap, 0, &Limits::DEFAULT.allow_leap_second(true)).is_ok());Sourcepub fn parse_detailed(
value: &[u8],
now: i64,
) -> Result<(Self, DateFormat), ParseError>
pub fn parse_detailed( value: &[u8], now: i64, ) -> Result<(Self, DateFormat), ParseError>
Sourcepub fn now() -> Self
Available on crate feature std only.
pub fn now() -> Self
std only.The current time from the system clock, clamped to the supported range.
§Examples
use parse_net::HttpDate;
assert!(HttpDate::now() > HttpDate::from_unix(1_700_000_000)?); // later than 2023-11-14Sourcepub fn from_system_time(t: SystemTime) -> Result<Self, ParseError>
Available on crate feature std only.
pub fn from_system_time(t: SystemTime) -> Result<Self, ParseError>
std only.The date of a SystemTime, truncated to whole seconds toward the
past.
§Errors
ErrorKind::OutOfRange outside years 1 to 9999.
§Examples
use std::time::{Duration, UNIX_EPOCH};
let t = UNIX_EPOCH + Duration::from_millis(1_500);
assert_eq!(parse_net::HttpDate::from_system_time(t)?.unix(), 1);Sourcepub fn to_system_time(self) -> Option<SystemTime>
Available on crate feature std only.
pub fn to_system_time(self) -> Option<SystemTime>
std only.The date as a SystemTime, or None when the platform’s clock type
cannot represent it (Windows cannot go before the year 1601).
§Examples
use std::time::{Duration, UNIX_EPOCH};
let d = parse_net::HttpDate::from_unix(86_400)?;
assert_eq!(d.to_system_time(), Some(UNIX_EPOCH + Duration::from_secs(86_400)));