New function `DateTime.from_iso8601!`

suggestion
#1

I would like to suggest the addition of a new function DateTime.from_iso8601!.

  @doc """
  Parses the extended "Date and time of day" format described by
  [ISO 8601:2004](https://en.wikipedia.org/wiki/ISO_8601).

  Raises if the format is invalid.

  ## Examples

      iex> DateTime.from_iso8601!("2019-02-06T10:00:00Z")
      #DateTime<2019-02-06 10:00:00Z>

      iex> DateTime.from_iso8601!("2019-02-06T10:00:00")
      ** (ArgumentError) cannot parse "2019-02-06T10:00:00" as datetime, reason: :missing_offset

      iex> DateTime.from_iso8601!("2019-02-06T10:00:70Z")
      ** (ArgumentError) cannot parse "2019-02-06T10:00:70Z" as datetime, reason: :invalid_time

      iex> DateTime.from_iso8601!("2019-02-29T10:00:00Z")
      ** (ArgumentError) cannot parse "2019-02-29T10:00:00Z" as datetime, reason: :invalid_date

      iex> DateTime.from_iso8601!("foo")
      ** (ArgumentError) cannot parse "foo" as datetime, reason: :invalid_format
  """
  @spec from_iso8601!(String.t(), Calendar.calendar()) :: DateTime.t() | no_return
  def from_iso8601!(string, calendar \\ Calendar.ISO) do
    case DateTime.from_iso8601(string, calendar) do
      {:ok, value, _} ->
        value

      {:error, reason} ->
        raise ArgumentError,
              "cannot parse #{inspect(string)} as datetime, reason: #{inspect(reason)}"
    end
  end

What do you think?

#2

This has been proposed before, see: https://github.com/elixir-lang/elixir/pull/6514

1 Like
#3

Ok, thank you.