http_date v0.1.0 HTTPDate
Link to this section Summary
Functions
Format as an HTTP-date (RFC 7231).
Parse an HTTP-date (RFC 7231).
Parse an HTTP-date (RFC 7231).
Link to this section Types
Link to this section Functions
Format as an HTTP-date (RFC 7231).
By default it produces IMF-fixdate formatted dates, this can be changed by setting
the :format
option to the desired format, see format/0
.
iex> HTTPDate.format(HTTPDate.parse!("Sun, 06 Nov 1994 08:49:37 GMT"))
"Sun, 06 Nov 1994 08:49:37 GMT"
iex> HTTPDate.format(HTTPDate.parse!("Sunday, 06-Nov-94 08:49:37 GMT", base_year: 1900), format: :rfc850)
"Sunday, 06-Nov-94 08:49:37 GMT"
iex> HTTPDate.format(HTTPDate.parse!("Sun Nov 6 08:49:37 1994"), format: :asctime)
"Sun Nov 6 08:49:37 1994"
parse(date, opts \\ [])
parse(String.t(), Keyword.t()) :: {:ok, DateTime.t()} | {:error, :invalid | :unknown_format | {format(), component()}}
Parse an HTTP-date (RFC 7231).
This supports both the preferred format:
Sun, 06 Nov 1994 08:49:37 GMT ; IMF-fixdate
As well as the obsolete formats:
Sunday, 06-Nov-94 08:49:37 GMT ; obsolete RFC 850 format
Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format
By default the parsed date will be validated to ensure that it is correct. If this
validation is not desired it can be disabled by passing false
to the :validate
option.
By default it uses Calendar.ISO
, however this can be changed by passing another
calendar to the :calendar
option.
As the obsolete RFC 850 format only allows for the last 2 year digits, it is
assumed these digits belong to the current year. If a specific base year is
desired, pass the year to the :base_year
option.
iex> HTTPDate.parse("Sun, 06 Nov 1994 08:49:37 GMT")
{ :ok, elem(DateTime.from_iso8601("1994-11-06 08:49:37Z"), 1) }
iex> HTTPDate.parse("Sunday, 06-Nov-94 08:49:37 GMT", base_year: 1900)
{ :ok, elem(DateTime.from_iso8601("1994-11-06 08:49:37Z"), 1) }
iex> HTTPDate.parse("Sun Nov 6 08:49:37 1994")
{ :ok, elem(DateTime.from_iso8601("1994-11-06 08:49:37Z"), 1) }
iex> HTTPDate.parse("Mon, 06 Nov 1994 08:49:37 GMT")
{ :error, :invalid }
iex> HTTPDate.parse("Mon, 06 Nov 1994 08:49:37 GMT", validate: false)
{ :ok, elem(DateTime.from_iso8601("1994-11-06 08:49:37Z"), 1) }
parse!(date, opts \\ [])
parse!(String.t(), Keyword.t()) :: DateTime.t() | no_return()
Parse an HTTP-date (RFC 7231).
Raises an HTTPDate.ParseError
if the date could not be parsed.
For more details see HTTPDate.parse/2
.
iex> HTTPDate.parse!("Sun, 06 Nov 1994 08:49:37 GMT")
elem(DateTime.from_iso8601("1994-11-06 08:49:37Z"), 1)