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 GMT

  • Sunday, 06-Nov-94 08:49:37 GMT, where the years 70 to 99 are 1970 to 1999 and the years 00 to 69 are 2000 to 2069

  • Sun 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#

silk
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#

silk
pub struct HttpDate

Functions that write and read the date and time format of HTTP header fields.

Implementation HttpDate#

silk
impl HttpDate

Associated function write#

silk
pub fn write(instant: Instant, output: &mut [u8], at: usize) -> usize

Writes 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#

silk
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#

  • UnexpectedEnd or UnexpectedCharacter if text is not in one of the three forms.

  • OutOfRange if the date or the time does not exist, such as 30 Feb or hour 24.

Associated function parseBytes#

silk
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#

  • UnexpectedEnd or UnexpectedCharacter if bytes is not in one of the three forms.

  • OutOfRange if the date or the time does not exist, such as 30 Feb or hour 24.

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.

View the source of this page