elixir-cldr / cldr_dates_times

Date & times formatting functions for the Common Locale Data Repository (CLDR) package https://github.com/elixir-cldr/cldr
Other
68 stars 13 forks source link

Open intervals #23

Closed woylie closed 3 years ago

woylie commented 3 years ago

As far as I can see, there is currently no way to format open intervals to strings. What I need to do is this:

iex> Cldr.Date.Interval.to_string(~D[2020-01-01], nil, MyApp.Cldr, locale: "en")
{:ok, "Jan 1, 2020 –"}

iex> Cldr.Date.Interval.to_string(~D[2020-01-01], nil, MyApp.Cldr, locale: "ja")
{:ok, "2020年1月1日~"}

iex> Cldr.Date.Interval.to_string(nil, ~D[2020-01-01], MyApp.Cldr, locale: "en")
{:ok, "– Jan 1, 2020"}

iex> Cldr.Date.Interval.to_string(nil, ~D[2020-01-01], MyApp.Cldr, locale: "ja")
{:ok, "~2020年1月1日"}

And the same for DateTime.Interval and Time.Interval.

Is there an easy to way to get to that output with the existing functions? Would it make sense to modify the to_string functions as described?

kipcole9 commented 3 years ago

Thanks for the report. Should be possible, I will take a look over the weekend. Is that is workable from your timeline?

I can definitely support open intervals as parameters but the return string may be something like " – Jan 1, 2020" since the interval formats from CLDR typically have a space (or non-breaking space) either side of the symbol. And I can't think of a heuristic that would consistently deal with that in all locales.

Would you be ok in that case?

woylie commented 3 years ago

Thanks for the quick reply. I can live with " – Jan 1, 2020", I would just pipe the end result into Strim.trim/1. This is not critical for us, I'm not in a hurry.

kipcole9 commented 3 years ago

Hmmmm, I think I can do String.trim_leading/1 and String.trim_trailing/1 appropriately without creating any issues so I'll go with that and let you know when I'm done.

kipcole9 commented 3 years ago

Sorry for the delay. I have just now published ex_cldr_dates_times version 2.9.0 with the following changelog entry:

Bug fixes

Enhancements

Examples

  iex> Cldr.Date.Interval.to_string ~D[2020-01-01], nil, MyApp.Cldr,
  ...> format: :short
  {:ok, "1/1/20 –"}

  iex> Cldr.Time.Interval.to_string ~U[2020-01-01 00:00:00.0Z], nil, MyApp.Cldr,
  ...> format: :long, style: :flex
  {:ok, "12:00:00 AM UTC –"}

  iex> Cldr.DateTime.Interval.to_string ~U[2020-01-01 00:00:00.0Z], nil, MyApp.Cldr
  {:ok, "Jan 1, 2020, 12:00:00 AM –"}
woylie commented 3 years ago

Great, thank you very much!