Provide DateTime.from_iso8601!/2

69 views
Skip to first unread message

Mário Guimarães

unread,
Jun 17, 2019, 10:18:29 AM6/17/19
to elixir-lang-core
Hello,

all "from_x" functions in DateTime support their throw exception version, but the function "from_iso8601/2" which returns an error tuple.

Doesn't it make sense, because of convenience and completeness, to provide a bang version of this function?


Thanks
Mário

Mário Guimarães

unread,
Jun 17, 2019, 10:31:54 AM6/17/19
to elixir-lang-core
The proposed function could be implemented like this

  def from_iso8601!(string, calendar \\ Calendar.ISO) do
   
case DateTime.from_iso8601(string, calendar) do
     
{:ok, datetime, offset} ->
       
{datetime, offset}

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

Andrea Leopardi

unread,
Jun 19, 2019, 4:13:39 AM6/19/19
to elixir-lang-core
I can't see a reason why not to add this. I think a PR would be welcome!

Andrea Leopardi


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/1b2ea98e-24b0-4829-8943-82ca3add955c%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Fernando Tapia Rico

unread,
Jun 19, 2019, 4:40:31 AM6/19/19
to elixir-lang-core
This has been suggested in the past. For example https://github.com/elixir-lang/elixir/pull/6514

The main reason why there is not a bang variant is because the `offset` information would be dropped.

On Wednesday, June 19, 2019 at 10:13:39 AM UTC+2, Andrea Leopardi wrote:
> I can't see a reason why not to add this. I think a PR would be welcome!
>
>
>
>
>
> Andrea Leopardi
> an.le...@gmail.com
>
>
>
> On Mon, Jun 17, 2019 at 4:31 PM Mário Guimarães <mario.lui...@gmail.com> wrote:
>
> The proposed function could be implemented like this
>
>
>
>
>   def from_iso8601!(string, calendar \\ Calendar.ISO) do
>     case DateTime.from_iso8601(string, calendar) do
>       {:ok, datetime, offset} ->
>         {datetime, offset}
>
>       {:error, reason} ->
>         raise ArgumentError,
>               "cannot convert #{string} to datetime, reason: #{inspect(reason)}"
>     end
>   end
>
>
>
>
> segunda-feira, 17 de Junho de 2019 às 15:18:29 UTC+1, Mário Guimarães escreveu:
> Hello,
>
>
> all "from_x" functions in DateTime support their throw exception version, but the function "from_iso8601/2" which returns an error tuple.
>
>
> Doesn't it make sense, because of convenience and completeness, to provide a bang version of this function?
>
>
> See https://hexdocs.pm/elixir/DateTime.html#from_iso8601/2
>
>
> Thanks
> Mário
>
>
>
>
> --
>
> You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
>
> To unsubscribe from this group and stop receiving emails from it, send an email to elixir-l...@googlegroups.com.

Andrea Leopardi

unread,
Jun 19, 2019, 4:42:50 AM6/19/19
to elixir-lang-core
We don't drop the offset information in the suggested implementation. And pattern matching on {datetime, offset} instead of {:ok, datetime, offset} still has the benefit that it has to match otherwise a nice error is returned (which doesn't happen if you match on {:ok, datetime, offset} and get a MatchError).

Andrea Leopardi


To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/f8d56771-d618-4ece-be85-4f4f740fe928%40googlegroups.com.

Fernando Tapia Rico

unread,
Jun 19, 2019, 4:43:09 AM6/19/19
to elixir-lang-core
Oh! Sorry, I replied too soon, the example contains the offset.

I’m not sure if users would expect just the datetime in the bang version :(

Andrea Leopardi

unread,
Jun 19, 2019, 4:44:36 AM6/19/19
to elixir-lang-core
Well, it's documented, and if the non-bang variant returns the offset I think users would expect that?

Tbh to me the benefits (better error, mirroring all other functions) outweigh the downsides (returning a tuple instead of a datetime like other functions).

Andrea Leopardi


To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/dab80e48-9121-41dd-8d19-dc9658096af1%40googlegroups.com.

Chris

unread,
Aug 27, 2019, 2:02:01 PM8/27/19
to elixir-l...@googlegroups.com
This seems like a nice cleanup of a minor blemish.

Reply all
Reply to author
Forward
0 new messages