From 2b857b3263d2b9377638b4ff06ecd4ff18747d40 Mon Sep 17 00:00:00 2001 From: Kip Cole Date: Sun, 29 Dec 2024 08:24:14 +1100 Subject: [PATCH] Improve resoution of #23 --- CHANGELOG.md | 4 +- lib/cldr/calendar/duration.ex | 77 +++++++++++++++++++++++------------ 2 files changed, 54 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 683681a..dc82714 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,12 +4,14 @@ ## Cldr.Calendars v1.26.4 -This is the changelog for Cldr Calendars v1.26.4 released on _______. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_calendars/tags) +This is the changelog for Cldr Calendars v1.26.4 released on December 29th, 2024. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_calendars/tags) ### Bug Fixes * Fix day of week calculations when the config `%{day_of_week: :first}` is set. +* Improve the implementation of the fix for #23 to respect the conditional compilation based upon whether `ex_cldr_units` is configured or not. + ## Cldr.Calendars v1.26.3 This is the changelog for Cldr Calendars v1.26.3 released on December 29th, 2024. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_calendars/tags) diff --git a/lib/cldr/calendar/duration.ex b/lib/cldr/calendar/duration.ex index 29b0308..aaa1386 100644 --- a/lib/cldr/calendar/duration.ex +++ b/lib/cldr/calendar/duration.ex @@ -116,6 +116,37 @@ defmodule Cldr.Calendar.Duration do end |> Cldr.Unit.to_string(options) end + + @doc """ + Formats a duration as a string or raises + an exception on error. + + ## Arguments + + * `duration` is a duration of type `t:Cldr.Calendar.Duration.t/0` returned + by `Cldr.Calendar.Duration.new/2`. + + * `options` is a Keyword list of options. + + ## Options + + See `Cldr.Calendar.Duration.to_string/2`. + + ## Returns + + * A formatted string or + + * raises an exception. + + """ + + @spec to_string!(t(), Keyword.t()) :: String.t() | no_return + def to_string!(%__MODULE__{} = duration, options \\ []) do + case to_string(duration, options) do + {:ok, string} -> string + {:error, {exception, reason}} -> raise exception, reason + end + end else @doc """ Returns a string formatted representation of @@ -167,46 +198,40 @@ defmodule Cldr.Calendar.Duration do {:ok, formatted} end - end - defp maybe_extract_microseconds(:microsecond, {microseconds, _precision}), do: microseconds - defp maybe_extract_microseconds(_any, value), do: value - - @doc """ - Formats a duration as a string or raises - an exception on error. - - ## Arguments + @doc """ + Formats a duration as a string or raises + an exception on error. - * `duration` is a duration of type `t:Cldr.Calendar.Duration.t/0` returned - by `Cldr.Calendar.Duration.new/2` + ## Arguments - * `options` is a Keyword list of options + * `duration` is a duration of type `t:Cldr.Calendar.Duration.t/0` returned + by `Cldr.Calendar.Duration.new/2`. - ## Options + * `options` is a Keyword list of options. - See `Cldr.Calendar.Duration.to_string/2` + ## Options - ## Returns + See `Cldr.Calendar.Duration.to_string/2`. - * A formatted string or + ## Returns - * raises an exception + * A formatted string or - """ + * raises an exception. - # For now there are no exception returns for `to_string/2` - # so the code here is a reminder that perhaps that should - # change. + """ - @spec to_string!(t(), Keyword.t()) :: String.t() | no_return - def to_string!(%__MODULE__{} = duration, options \\ []) do - case to_string(duration, options) do - {:ok, string} -> string - # {:error, {exception, reason}} -> raise exception, reason + @spec to_string!(t(), Keyword.t()) :: String.t() | no_return + def to_string!(%__MODULE__{} = duration, options \\ []) do + {:ok, string} = to_string(duration, options) + string end end + defp maybe_extract_microseconds(:microsecond, {microseconds, _precision}), do: microseconds + defp maybe_extract_microseconds(_any, value), do: value + @doc """ Calculates the calendar difference between two dates returning a `Duration` struct.