Skip to content

Commit

Permalink
Improve resoution of #23
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Dec 28, 2024
1 parent 0ab9f0b commit 2b857b3
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 27 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
77 changes: 51 additions & 26 deletions lib/cldr/calendar/duration.ex
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 2b857b3

Please sign in to comment.