silk.time.text_http.
The date and time format of HTTP header fields such as Date, Last-Modified, and Expires.
When to use#
Use HttpDate.write for a header field that a program sends and HttpDate.parse for one
that it receives. For timestamps in other protocols and in files, use Instant.parse and
Instant.writeUtc, which read and write RFC 3339.
Details#
HttpDate.write writes the one form that RFC 9110 allows a sender, which is always 29 bytes:
Sun, 06 Nov 1994 08:49:37 GMT.
HttpDate.parse reads the three forms that RFC 9110 requires of a recipient:
Sun, 06 Nov 1994 08:49:37 GMTSunday, 06-Nov-94 08:49:37 GMT, where the years70to99are 1970 to 1999 and the years00to69are 2000 to 2069Sun Nov 6 08:49:37 1994
Names are case-sensitive. The name of the day must be a real day name, and the parser does not
check that it is the day of the date. Second 60 reads as 59.
Examples#
Write and read a header value#
import silk.collections.slice { Slice }
import silk.core.result { Result }
import silk.time.instant { Instant }
import silk.time.text { TimeParseError }
import silk.time.text_http { HttpDate }
pub fn main() -> i32 {
let mut text: [u8; 29] = [0; 29]
let length = HttpDate.write(Instant.fromUnixSeconds(784111777), &mut text, 0)
// `text` is now `Sun, 06 Nov 1994 08:49:37 GMT`.
match HttpDate.parseBytes(Slice.view<u8>(&text, 0, length)) {
Result<Instant, TimeParseError>.Success { value } => {
if value.unixSeconds() == 784111777 {
return 0
}
return 1
}
Result<Instant, TimeParseError>.Failure { error: _ } => {
return 2
}
}
}Import as HttpDate with import silk.time.text_http { HttpDate }.
Public declarations: 1.
HttpDate#
pub struct HttpDateFunctions that write and read the date and time format of HTTP header fields.
Implementation HttpDate#
impl HttpDateAssociated function write#
pub fn write(instant: Instant, output: &mut [u8], at: usize) -> usizeWrites instant as Sun, 06 Nov 1994 08:49:37 GMT at output[at] and returns at + 29.
Details#
The format has whole seconds, so a fraction of a second is dropped. The year of instant in
UTC must be from 0 to 9999, or the function traps.
Associated function parse#
pub fn parse(text: string) -> Result<Instant, TimeParseError>Reads an instant from the value of an HTTP date header field, in any of the three forms of RFC 9110.
Errors#
UnexpectedEndorUnexpectedCharacteriftextis not in one of the three forms.OutOfRangeif the date or the time does not exist, such as30 Febor hour24.
Associated function parseBytes#
pub fn parseBytes(bytes: &[u8]) -> Result<Instant, TimeParseError>Reads an instant from the bytes of the value of an HTTP date header field, in any of the three forms of RFC 9110.
Errors#
UnexpectedEndorUnexpectedCharacterifbytesis not in one of the three forms.OutOfRangeif the date or the time does not exist, such as30 Febor hour24.
Category: Time.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2, x86_64-pc-windows-msvc, wasm64-unknown-unknown.