elixir-cldr / cldr_dates_times

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

Formatting native DateTime with AM/PM fragment #48

Closed sysashi closed 4 months ago

sysashi commented 4 months ago

Failing example:

iex> date = ~U[2024-07-01 13:00:00+00:00] |> DateTime.shift_zone("Europe/London")
iex> MyApp.Cldr.DateTime.Formatter.format(date, "MMM M @ h:mm z") # {:ok, "Jul 7 @ 2:00 BST"}
iex> MyApp.Cldr.DateTime.Formatter.format(date, "MMM M @ h:mm aaaaa z")
** (UndefinedFunctionError) function Calendar.ISO.cldr_calendar_type/0 is undefined or private
    (elixir 1.17.1) Calendar.ISO.cldr_calendar_type()
    (elixir 1.17.1) src/elixir_erl_pass.erl:715: :elixir_erl_pass.no_parens_remote/2
    (ex_cldr_calendars 1.23.1) lib/cldr/calendar.ex:2522: Cldr.Calendar.localize/6
    (ex_cldr_dates_times 2.18.0) lib/cldr/format/date_time_formatter.ex:1921: Cldr.DateTime.Formatter.period_am_pm/5
    (elixir 1.17.1) lib/enum.ex:1703: Enum."-map/2-lists^map/1-1-"/2
    (elixir 1.17.1) lib/enum.ex:1703: Enum."-map/2-lists^map/1-1-"/2

It seems like the underlying Cldr.Calendar.localize/6 function grabs calendar from passed date_time, which for native date time would be Calendar.ISO (DateTime.utc_now().calendar # => Calendar.ISO)

Question is: Should I convert datetime to cldr calendar or expect it to convert it for me automatically (Calendar.ISO -> Cldr.Calendar.ISO) or maybe there is something else entirely

kipcole9 commented 4 months ago

or expect it to convert it for me automatically (Calendar.ISO -> Cldr.Calendar.Gregorian)

Yes, you absolutely should expect ex_cldr_calendars and ex_cldr_dates_times to take care of that for you. It's definitely a bug. Sorry for the inconvenience - I'll get this fixed and a new release out within the next 24 hours.

kipcole9 commented 4 months ago

I have published ex_cldr_dates_times version 2.18.1 with the following changelog entry:

Bug Fixes

Thanks for the report!

kipcole9 commented 4 months ago

BTW, just noting what the docs say about MyApp.Cldr.DateTime.Formatter.format/4

NOTE This function is called by Cldr.Date.to_string/2, Cldr.Time.to_string/2 and Cldr.DateTime.to_string/2 which is the preferred API.

sysashi commented 4 months ago

Got it, thank you @kipcole9, completely missed that note.. :( for some reason I was always assuming using <my-app>.Cldr is better since I dont have to pass backend option. Will use the proper api

kipcole9 commented 4 months ago

Oh my bad. Totally ok and even more preferred to use MyApp.Cldr.{Date, Time, DateTime}.to_string/2 ! I'll make the note clearer.