elixir-cldr / cldr_units

Unit formatting (volume, area, length, ...) functions for the Common Locale Data Repository (CLDR)
Other
16 stars 13 forks source link

:duration category -> conversion of :year to :weeks/:days not working #17

Closed DamienFriot closed 3 years ago

DamienFriot commented 3 years ago

Hi,

While :week and :day are compatible units, this is not the case for :year (but it is compatible with month).

Is it on purpose?

Thanks in advance for your answer.

kipcole9 commented 3 years ago

Yes, its by design in CLDR. Convertability is based upon units that have a common base unit. And not all time elements have the same base unit. For example:

iex> Cldr.Unit.base_unit :day
{:ok, :second}
iex> Cldr.Unit.base_unit :week
{:ok, :second}
iex> Cldr.Unit.base_unit :hour 
{:ok, :second}
iex> Cldr.Unit.base_unit :minute
{:ok, :second}
iex> Cldr.Unit.base_unit :second
{:ok, :second}

iex> Cldr.Unit.base_unit :year
{:ok, :year}
iex> Cldr.Unit.base_unit :month
{:ok, :year}

You can see that :day: and :week are defined in terms of :second. :year and :month are defined in terms of :year.

The reason for that is, as I'm sure you understand, that years and months are variable in length so the conversion would need to have context that isn't available. Even as it is, the conversions with this library assume a year has 12 months - which isn't true for all calendars.

My ex_cldr_dates_times is a lot more aware of durations so if you let me know what the use case is I may have another way to help you achieve it.

DamienFriot commented 3 years ago

Thanks for your answer. I will look at ex_cldr_dates_times and come back to you if needed but I think this will ok. Sorry for bothering you with that.

kipcole9 commented 3 years ago

Will close this issue now since the code is working as expected - even if not as hoped for.