View Source AnyHttp.Utils (Any HTTP v0.6.0)

Provides some functions which can be used with AnyHttp library.

Summary

Functions

Converts a RFC1123 value into a NaiveDateTime.

Converts a NaiveDateTime into a valid RFC1123 value (mostly used in HTTP headers).

Functions

Link to this function

from_rfc1123_date!(value)

View Source
@spec from_rfc1123_date!(binary() | charlist()) :: NaiveDateTime.t() | no_return()

Converts a RFC1123 value into a NaiveDateTime.

Raises a ArgumentError exception if the value is not a valid datetime.

Examples

iex> AnyHttp.Utils.from_rfc1123_date!("Thu, 07 Dec 2023 18:06:28 GMT")
~N[2023-12-07 18:06:28]

iex> AnyHttp.Utils.from_rfc1123_date!(~c"Thu, 07 Dec 2023 18:06:28 GMT")
~N[2023-12-07 18:06:28]

iex> AnyHttp.Utils.from_rfc1123_date!(~c"Thu, 99 Dec 2023 18:06:28 GMT")
** (ArgumentError) cannot convert {{2023, 12, 99}, {18, 6, 28}} to naive datetime, reason: :invalid_date
Link to this function

to_rfc1123_date(naive_datetime, format \\ :binary)

View Source
@spec to_rfc1123_date(NaiveDateTime.t(), :binary) :: binary()
@spec to_rfc1123_date(NaiveDateTime.t(), :charlist) :: charlist()

Converts a NaiveDateTime into a valid RFC1123 value (mostly used in HTTP headers).

It accepts a second argument to define if the return value should be a string or a charlist.

Note: The implementation relies on :httpd_util from Erlang but Erlang converts the value into local datetime which makes the to => from => to inconsistent. This implementation to keep the same timezone.

Examples

iex> AnyHttp.Utils.to_rfc1123_date(~N[2023-12-07 18:06:28.313171])
"Thu, 07 Dec 2023 18:06:28 GMT"

iex> AnyHttp.Utils.to_rfc1123_date(~N[2023-12-07 18:06:28.313171], :binary)
"Thu, 07 Dec 2023 18:06:28 GMT"

iex> AnyHttp.Utils.to_rfc1123_date(~N[2023-12-07 18:06:28.313171], :charlist)
~c"Thu, 07 Dec 2023 18:06:28 GMT"