Skip to content

Commit

Permalink
Fix time intervals formatting when the interval crosses midnight. Closes
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Oct 22, 2023
1 parent 8e29db7 commit a045363
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

**Note that `ex_cldr_dates_times` version 2.14.0 and later are supported on Elixir 1.11 and later only.**

## Cldr_Dates_Times v2.15.1

This is the changelog for Cldr_Dates_Times v2.15.1 released on October 22nd, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_cldr_dates_times/tags)

### Bug Fixes

* Fix formatting time intervals when the `to` time is not greater than the `from` time. This allows time intervals that cross midnight to be formatted correctly. Thanks to @larshei for the report. Closes #42.

## Cldr_Dates_Times v2.15.0

This is the changelog for Cldr_Dates_Times v2.15.0 released on October 17th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_cldr_dates_times/tags)
Expand Down
23 changes: 12 additions & 11 deletions lib/cldr/interval/time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,7 @@ defmodule Cldr.Time.Interval do
|> Keyword.put(:locale, locale)
|> Keyword.put(:number_system, number_system)

with {:ok, _} <- from_less_than_or_equal_to(from, to),
{:ok, backend} <- Cldr.validate_backend(backend),
with {:ok, backend} <- Cldr.validate_backend(backend),
{:ok, locale} <- Cldr.validate_locale(locale, backend),
{:ok, _} <- Cldr.Number.validate_number_system(locale, number_system, backend),
{:ok, calendar} <- Cldr.Calendar.validate_calendar(from.calendar),
Expand Down Expand Up @@ -313,8 +312,7 @@ defmodule Cldr.Time.Interval do
`Calendar.time` type.
* `to` is any map that conforms to the
`Calendar.time` type. `to` must occur
on or after `from`.
`Calendar.time` type.
* `backend` is any module that includes `use Cldr` and
is therefore `Cldr` backend module
Expand Down Expand Up @@ -368,6 +366,9 @@ defmodule Cldr.Time.Interval do
iex> Cldr.Time.Interval.to_string! ~T[10:00:00], ~T[10:03:00], MyApp.Cldr, format: :long
"10:00 – 10:03 AM"
iex> Cldr.Time.Interval.to_string ~T[23:00:00.0Z], ~T[01:01:00.0Z], MyApp.Cldr
{:ok, "11:00 PM – 1:01 AM"}
iex> Cldr.Time.Interval.to_string! ~T[10:00:00], ~T[10:03:00], MyApp.Cldr,
...> format: :long, style: :flex
"10:00 – 10:03 in the morning"
Expand Down Expand Up @@ -427,12 +428,12 @@ defmodule Cldr.Time.Interval do
Cldr.Date.Interval.greatest_difference(from, to)
end

defp from_less_than_or_equal_to(from, to) do
case Time.compare(from, to) do
comp when comp in [:eq, :lt] -> {:ok, comp}
_other -> {:error, Cldr.Date.Interval.datetime_order_error(from, to)}
end
end
# defp from_less_than_or_equal_to(from, to) do
# case Time.compare(from, to) do
# comp when comp in [:eq, :lt] -> {:ok, comp}
# _other -> {:error, Cldr.Date.Interval.datetime_order_error(from, to)}
# end
# end

defp resolve_format(from, to, formats, locale, options) do
format = Keyword.get(options, :format, @default_format)
Expand All @@ -450,7 +451,7 @@ defmodule Cldr.Time.Interval do
end

defp greatest_difference_format(%{hour: from}, %{hour: to}, format, :H)
when from < 12 and to >= 12 do
when (from < 12 and to >= 12) or (from >= 12 and to < 12) do
case Map.get(format, :b) || Map.get(format, :a) || Map.get(format, :H) || Map.get(format, :h) do
nil -> {:error, format_error(format, format)}
success -> {:ok, success}
Expand Down
2 changes: 1 addition & 1 deletion mix.exs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
defmodule Cldr.DatesTimes.Mixfile do
use Mix.Project

@version "2.15.0"
@version "2.15.1"

def project do
[
Expand Down

0 comments on commit a045363

Please sign in to comment.