Skip to content

Commit

Permalink
Mix format
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Jul 26, 2024
1 parent 1a05fd1 commit 0e9bc19
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 21 deletions.
1 change: 1 addition & 0 deletions lib/cldr/format/date_time_format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ defmodule Cldr.DateTime.Format do
def format_list(config) do
locale_names = Cldr.Locale.Loader.known_locale_names(config)
backend = config.backend

((known_formats(&all_date_formats(&1, backend), locale_names) ++
known_formats(&all_time_formats(&1, backend), locale_names) ++
known_formats(&all_date_time_formats(&1, backend), locale_names) ++
Expand Down
2 changes: 1 addition & 1 deletion lib/cldr/interval/date.ex
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ defmodule Cldr.Date.Interval do
def to_string(unquote(date()) = from, unquote(date()) = to, backend, options) do
{locale, backend} = Cldr.locale_and_backend_from(options[:locale], backend)
formatter = Module.concat(backend, DateTime.Formatter)
options = normalize_options(locale, backend, options)
options = normalize_options(locale, backend, options)

number_system = options.number_system
prefer = options.prefer
Expand Down
14 changes: 12 additions & 2 deletions lib/cldr/interval/date_time.ex
Original file line number Diff line number Diff line change
Expand Up @@ -386,7 +386,8 @@ defmodule Cldr.DateTime.Interval do
{:ok, locale} <- Cldr.validate_locale(locale, backend),
{:ok, calendar} <- Cldr.Calendar.validate_calendar(from.calendar),
{:ok, _} <- Cldr.Number.validate_number_system(locale, number_system, backend),
{:ok, format, date_format, time_format} <- validate_format(format, date_format, time_format, options),
{:ok, format, date_format, time_format} <-
validate_format(format, date_format, time_format, options),
{:ok, greatest_difference} <- greatest_difference(from, to) do
options = adjust_options(options, locale, format, date_format, time_format)
format_date_time(from, to, locale, backend, calendar, greatest_difference, options)
Expand Down Expand Up @@ -755,7 +756,16 @@ defmodule Cldr.DateTime.Interval do
to_options = Map.put(options, :format, to_format)
final_format = if is_atom(format), do: format, else: [from_format, to_format]

do_format_date_time(from, to, backend, final_format, difference, from_options, to_options, fallback)
do_format_date_time(
from,
to,
backend,
final_format,
difference,
from_options,
to_options,
fallback
)
end

# The difference is only in the time part
Expand Down
4 changes: 2 additions & 2 deletions test/cldr_dates_times_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ defmodule Cldr.DatesTimes.Test do
datetime = ~U[2024-07-07 21:36:00.440105Z]

assert {:ok, "July 7, 2024 at 9:36:00 PM UTC"} =
Cldr.DateTime.to_string(datetime, format: :long, style: :at)
Cldr.DateTime.to_string(datetime, format: :long, style: :at)

assert {:ok, "July 7, 2024, 9:36:00 PM UTC"} =
Cldr.DateTime.to_string(datetime, format: :long, style: :default)
Cldr.DateTime.to_string(datetime, format: :long, style: :default)
end

test "Symmetry of the format/3 and available_format/3 functions for Date, Time and DateTime" do
Expand Down
52 changes: 36 additions & 16 deletions test/variant_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,48 +13,68 @@ defmodule Cldr.DateTime.VariantTest do
datetime = ~U[2024-07-07 21:36:00.440105Z]

assert {:ok, "Sun 9:36 PM"} =
Cldr.DateTime.to_string(datetime, format: :Ehm, prefer: :unicode, locale: :en)
Cldr.DateTime.to_string(datetime, format: :Ehm, prefer: :unicode, locale: :en)

assert {:ok, "Sun 9:36 PM"} =
Cldr.DateTime.to_string(datetime, format: :Ehm, prefer: :ascii, locale: :en)
Cldr.DateTime.to_string(datetime, format: :Ehm, prefer: :ascii, locale: :en)

assert {:ok, "Sun 9:36 PM"} =
Cldr.DateTime.to_string(datetime, format: :Ehm, prefer: [:unicode], locale: :en)
Cldr.DateTime.to_string(datetime, format: :Ehm, prefer: [:unicode], locale: :en)

assert {:ok, "Sun 9:36 PM"} =
Cldr.DateTime.to_string(datetime, format: :Ehm, prefer: [:ascii], locale: :en)
Cldr.DateTime.to_string(datetime, format: :Ehm, prefer: [:ascii], locale: :en)
end

test "Date interval variant" do
assert {:ok, "1/1/2024 – 1/2/2024"} =
Cldr.Date.Interval.to_string ~D[2024-01-01], ~D[2024-02-01], format: :yMd, prefer: :variant, locale: "en-CA"
Cldr.Date.Interval.to_string(~D[2024-01-01], ~D[2024-02-01],
format: :yMd,
prefer: :variant,
locale: "en-CA"
)

assert {:ok, "1/1/2024–2/1/2024"} =
Cldr.Date.Interval.to_string ~D[2024-01-01], ~D[2024-02-01], format: :yMd, prefer: :default, locale: "en-CA"
Cldr.Date.Interval.to_string(~D[2024-01-01], ~D[2024-02-01],
format: :yMd,
prefer: :default,
locale: "en-CA"
)

assert {:ok, "1/1/2024 – 1/2/2024"} =
Cldr.Date.Interval.to_string ~D[2024-01-01], ~D[2024-02-01], format: :yMd, prefer: [:variant], locale: "en-CA"
Cldr.Date.Interval.to_string(~D[2024-01-01], ~D[2024-02-01],
format: :yMd,
prefer: [:variant],
locale: "en-CA"
)

assert {:ok, "1/1/2024–2/1/2024"} =
Cldr.Date.Interval.to_string ~D[2024-01-01], ~D[2024-02-01], format: :yMd, prefer: [:default], locale: "en-CA"
Cldr.Date.Interval.to_string(~D[2024-01-01], ~D[2024-02-01],
format: :yMd,
prefer: [:default],
locale: "en-CA"
)

assert {:ok, "1/1/2024–2/1/2024"} =
Cldr.Date.Interval.to_string ~D[2024-01-01], ~D[2024-02-01], format: :yMd, locale: "en-CA"
Cldr.Date.Interval.to_string(~D[2024-01-01], ~D[2024-02-01],
format: :yMd,
locale: "en-CA"
)
end

test "Date variant" do
assert {:ok, "1/3/2024"} =
Cldr.Date.to_string ~D[2024-03-01], format: :yMd, prefer: :variant, locale: "en-CA"
Cldr.Date.to_string(~D[2024-03-01], format: :yMd, prefer: :variant, locale: "en-CA")

assert {:ok, "2024-03-01"} =
Cldr.Date.to_string ~D[2024-03-01], format: :yMd, prefer: :default, locale: "en-CA"
Cldr.Date.to_string(~D[2024-03-01], format: :yMd, prefer: :default, locale: "en-CA")

assert {:ok, "1/3/2024"} =
Cldr.Date.to_string ~D[2024-03-01], format: :yMd, prefer: [:variant], locale: "en-CA"
Cldr.Date.to_string(~D[2024-03-01], format: :yMd, prefer: [:variant], locale: "en-CA")

assert {:ok, "2024-03-01"} =
Cldr.Date.to_string ~D[2024-03-01], format: :yMd, prefer: [:default], locale: "en-CA"
Cldr.Date.to_string(~D[2024-03-01], format: :yMd, prefer: [:default], locale: "en-CA")

assert {:ok, "2024-03-01"} =
Cldr.Date.to_string ~D[2024-03-01], format: :yMd, locale: "en-CA"
Cldr.Date.to_string(~D[2024-03-01], format: :yMd, locale: "en-CA")
end

end
end

0 comments on commit 0e9bc19

Please sign in to comment.