[Proposal] Add ability to parse ISO8601 "basic" format

107 views
Skip to first unread message

Stefan Chrobot

unread,
Aug 6, 2019, 5:50:14 AM8/6/19
to elixir-lang-core
Hi!

Date (and friends) can be formatted using either "basic" or "extended" ISO8601, but parsing is only implemented for the "extended" format.

So one can do:

iex> Date.to_iso8601(~D[2019-07-01]) |> Date.from_iso8601()
{:ok, ~D[2019-07-01]}

but this won't "work":

iex> Date.to_iso8601(~D[2019-07-01], :basic) |> Date.from_iso8601()
{:error, :invalid_format}

Is there any reason for that? Would you accept a PR that adds this feature?



Best,

Stefan

José Valim

unread,
Aug 6, 2019, 5:54:36 AM8/6/19
to elixir-l...@googlegroups.com
I am looking forward to the discussion. The only constraint I propose is that the parsing of the basic format would have to be explicit (i.e. you would have to pass the :basic atom to the function).


José Valim
Skype: jv.ptec
Founder and Director of R&D


--
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/172d294d-3a56-42e1-873f-deea0cc0880c%40googlegroups.com.

Wojtek Mach

unread,
Aug 6, 2019, 6:07:07 AM8/6/19
to elixir-l...@googlegroups.com
I don't recall any examples at the moment but I remember seeing APIs that return data in the basic format so the proposal sounds good to me.

I agree that the format should be specified explicitly. Given a Date.from_iso8601/2 already exists, the second argument is the calendar (Calendar.ISO by default), the format needs to be the 3rd argument which is a bit unfortunate.

Allen Madsen

unread,
Aug 6, 2019, 8:39:07 AM8/6/19
to elixir-l...@googlegroups.com
This might be avoided in the Elixir codebase, not sure. But one clause of the function could specifically check for :basic as the second argument. So, the second argument is :basic or calendar.

def from_iso8601(date, format) when format in [:basic, :extended] do
  from_iso8601(date, format, Calendar.ISO)
end
def from_iso8601(date, calendar) do
  from_iso8601(date, :extended, calendar)
end

On Tue, Aug 6, 2019 at 6:07 AM Wojtek Mach <woj...@wojtekmach.pl> wrote:
I don't recall any examples at the moment but I remember seeing APIs that return data in the basic format so the proposal sounds good to me.

I agree that the format should be specified explicitly. Given a Date.from_iso8601/2 already exists, the second argument is the calendar (Calendar.ISO by default), the format needs to be the 3rd argument which is a bit unfortunate.

--
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.

Stefan Chrobot

unread,
Aug 16, 2019, 9:52:40 AM8/16/19
to elixir-l...@googlegroups.com
We could use format as the second argument and make it backwards-compatible using Allen's idea, but it requires at least 4 heads:

  def from_iso8601(date) do
    # :extended + Calendar.ISO
  end


  def from_iso8601(date, format) when format in [:basic, :extended] do
    # format + Calendar.ISO

  end

  def from_iso8601(date, calendar) do
    # :extended + calendar
  end

  def from_iso8601(date, format, calendar) do
    # format + calendar
  end

What do you think? Is it worth it?

Best,

Stefan

Chris

unread,
Aug 27, 2019, 1:55:05 PM8/27/19
to elixir-l...@googlegroups.com
What's the harm in just supporting the basic format? I mean, it's in the spec.

Chulki Lee

unread,
Aug 27, 2019, 6:43:12 PM8/27/19
to elixir-lang-core
What about taking both opts (list) and calendar (module)? We may add optional runtime warning when calendar is passed if needed.

The downside is: (probably) it cannot give compile time / dialyzer warning - as we can do with introducing new functions.

Wojtek Mach

unread,
Sep 7, 2019, 9:10:39 AM9/7/19
to elixir-l...@googlegroups.com
Another idea is to deprecate `Date.from_iso8601(string, calendar)` in favour of `Date.from_iso8601(string, calendar: calendar)`, and then `:format` becomes just another option.

Fernando Tapia Rico

unread,
Sep 8, 2019, 2:22:19 PM9/8/19
to elixir-lang-core
One downside of passing a list of options as a second argument is that it deviates from the other `from_iso8601/2` of other calendar types :)

Wojtek Mach

unread,
Sep 8, 2019, 2:27:29 PM9/8/19
to elixir-l...@googlegroups.com
I believe the proposal is about supporting :basic format in all calendar types. All from_iso8601 functions have the same shape right now and if we make a change in one place we should make it remaining too.

Wojtek Mach

unread,
Sep 8, 2019, 2:41:01 PM9/8/19
to elixir-l...@googlegroups.com
But yes, I only showed Date.from_iso8601(string, calendar: calendar), I meant to say we’d make this change everywhere. Which also means there would be deprecations everywhere. Thanks for follow-up, this needed clarification.

Fernando Tapia Rico

unread,
Sep 8, 2019, 5:01:36 PM9/8/19
to elixir-lang-core
My bad, thanks for the clarification Wojtek!

Looking at other functions of the calendar types, it seems the calendar is always passed as the last argument. 

For reference, here are some functions with an input value, an option, and a calendar as arguments: 

  • `DateTime.from_unix(integer, unit \\ :second, calendar \\ Calendar.ISO)`
  • `NaiveDateTime.from_erl(tuple, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)` 
  • `Time.from_erl(tuple, microsecond \\ {0, 0}, calendar \\ Calendar.ISO)`

I think Stefan's proposal is the closest one to the format of the current function signatures. 

Kelvin Raffael Stinghen

unread,
Sep 10, 2019, 12:18:46 PM9/10/19
to elixir-l...@googlegroups.com
Another idea is to deprecate `Date.from_iso8601(string, calendar)` in favour of `Date.from_iso8601(string, calendar: calendar)`, and then `:format` becomes just another option.

I’m +1 for this one. For optional parameters it is always good what actually that’s optional that it receives. I would even deprecate every `Date.something(date, calendar)` call in favor of that.

Best,
Kelvin Stinghen
kelvin....@me.com

Dmitry Belyaev

unread,
Sep 11, 2019, 11:23:05 PM9/11/19
to elixir-l...@googlegroups.com, Kelvin Raffael Stinghen
Getting a value from a keyword definitely costs more then having that value supplied directly as a positional argument. Of course the cost is very small, but why waste when we have a way which doesn't waste.

I suggest a separate function `from_iso8601_short`.


On 9 September 2019 10:07:39 am AEST, Kelvin Raffael Stinghen <kelvin....@gmail.com> wrote:
Another idea is to deprecate `Date.from_iso8601(string, calendar)` in favour of `Date.from_iso8601(string, calendar: calendar)`, and then `:format` becomes just another option.

I’m +1 for this one. For optional parameters it is always good what actually that’s optional that it receives. I would even deprecate every `Date.something(date, calendar)` call in favor of that.

Best,
Kelvin Stinghen
kelvin....@me.com

On Sep 7, 2019, at 10:10, Wojtek Mach <woj...@wojtekmach.pl> wrote:

Another idea is to deprecate `Date.from_iso8601(string, calendar)` in favour of `Date.from_iso8601(string, calendar: calendar)`, and then `:format` becomes just another option.


--
Kind regards,
Dmitry Belyaev

Chris

unread,
Sep 12, 2019, 12:55:20 AM9/12/19
to elixir-l...@googlegroups.com
Again, what's the problem with just silently supporting the basic form, exactly?

--
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.
Reply all
Reply to author
Forward
0 new messages