Skip to content

Commit b6c6bad

Browse files
committed
Reinstate Cldr.DateTime.Format.date_formats/3. Closes #51
1 parent 6fc4fa0 commit b6c6bad

8 files changed

+234
-11
lines changed

CHANGELOG.md

+8
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@
22

33
**Note that `ex_cldr_dates_times` version 2.18.0 and later are supported on Elixir 1.12 and later only.**
44

5+
## Cldr_Dates_Times v2.19.2
6+
7+
This is the changelog for Cldr_Dates_Times v2.19.2 released on July 9th, 2024. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_cldr_dates_times/tags)
8+
9+
### Bug Fixes
10+
11+
* Reinstate `Cldr.DateTime.Format.date_formats/3` which was incorrectly removed in version 2.19. Version 2.19 made efforts to improve the symmetry of `Cldr.Date`, `Cldr.Time` and `Cldr.DateTime`. Part of that work is to have each of those modules contain the functions `formats/3` and `available_formats/3`. It was not intended at this time that the equivalent functions be removed from `Cldr.DateTime.Format`. Thanks to @tjchambers for the report. Closes #51.
12+
513
## Cldr_Dates_Times v2.19.1
614

715
This is the changelog for Cldr_Dates_Times v2.19.1 released on July 8th, 2024. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_cldr_dates_times/tags)

lib/cldr/date.ex

+1-2
Original file line numberDiff line numberDiff line change
@@ -348,8 +348,7 @@ defmodule Cldr.Date do
348348
calendar \\ Cldr.Calendar.default_cldr_calendar(),
349349
backend \\ Cldr.Date.default_backend()
350350
) do
351-
backend = Module.concat(backend, DateTime.Format)
352-
backend.date_formats(locale, calendar)
351+
Cldr.DateTime.Format.date_formats(locale, calendar, backend)
353352
end
354353

355354
@doc """

lib/cldr/date_time.ex

+153-6
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ defmodule Cldr.DateTime do
2121

2222
alias Cldr.DateTime.Format
2323
alias Cldr.LanguageTag
24+
alias Cldr.Locale
2425

2526
@format_types [:short, :medium, :long, :full]
2627
@default_format_type :medium
@@ -101,9 +102,12 @@ defmodule Cldr.DateTime do
101102
102103
## Options
103104
104-
* `:format` is one of `:short`, `:medium`, `:long`, `:full` or a format string or
105-
any of the keys in the map returned by `Cldr.DateTime.Format.date_time_formats/3`.
106-
The default is `:medium`. See [here](README.md#date-time-and-datetime-localization-formats)
105+
* `:format` is one of `:short`, `:medium`, `:long`, `:full`, or a format ID
106+
or a format string. The default is `:medium` for full datetimes (that is,
107+
dates having `:year`, `:month`, `:day`, `:hour`, `:minutes`, `:second` and
108+
`:calendar` fields). The default for partial datetimes is to derive a candidate
109+
format ID from the date and find the best match from the formats returned by
110+
`Cldr.DateTime.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats)
107111
for more information about specifying formats.
108112
109113
* `:date_format` is any one of `:short`, `:medium`, `:long`, `:full`. If defined,
@@ -249,9 +253,12 @@ defmodule Cldr.DateTime do
249253
250254
## Options
251255
252-
* `:format` is one of `:short`, `:medium`, `:long`, `:full` or a format string or
253-
any of the keys returned by `Cldr.DateTime.Format.date_time_formats/3`.
254-
The default is `:medium`. See [here](README.md#date-time-and-datetime-localization-formats)
256+
* `:format` is one of `:short`, `:medium`, `:long`, `:full`, or a format ID
257+
or a format string. The default is `:medium` for full datetimes (that is,
258+
dates having `:year`, `:month`, `:day`, `:hour`, `:minutes`, `:second` and
259+
`:calendar` fields). The default for partial datetimes is to derive a candidate
260+
format ID from the date and find the best match from the formats returned by
261+
`Cldr.DateTime.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats)
255262
for more information about specifying formats.
256263
257264
* `:style` is either `:at` or `:default`. When set to `:at` the datetime may
@@ -321,6 +328,146 @@ defmodule Cldr.DateTime do
321328
end
322329
end
323330

331+
@doc """
332+
Returns a map of the standard datetime formats for a given locale and calendar.
333+
334+
## Arguments
335+
336+
* `locale` is any locale returned by `Cldr.known_locale_names/0`
337+
or a `t:Cldr.LanguageTag.t/0`. The default is `Cldr.get_locale/0`.
338+
339+
* `calendar` is any calendar returned by `Cldr.DateTime.Format.calendars_for/1`
340+
The default is `:gregorian`.
341+
342+
* `backend` is any module that includes `use Cldr` and therefore
343+
is a `Cldr` backend module. The default is `Cldr.default_backend/0`.
344+
345+
## Examples:
346+
347+
iex> Cldr.DateTime.Format.date_time_formats(:en)
348+
{:ok, %Cldr.DateTime.Formats{
349+
full: "{1}, {0}",
350+
long: "{1}, {0}",
351+
medium: "{1}, {0}",
352+
short: "{1}, {0}"
353+
}}
354+
355+
iex> Cldr.DateTime.Format.date_time_formats(:en, :buddhist, MyApp.Cldr)
356+
{:ok, %Cldr.DateTime.Formats{
357+
full: "{1}, {0}",
358+
long: "{1}, {0}",
359+
medium: "{1}, {0}",
360+
short: "{1}, {0}"
361+
}}
362+
363+
"""
364+
@spec formats(
365+
Locale.locale_reference(),
366+
Cldr.Calendar.calendar(),
367+
Cldr.backend()
368+
) ::
369+
{:ok, map()} | {:error, {atom, String.t()}}
370+
371+
def formats(
372+
locale \\ Cldr.get_locale(),
373+
calendar \\ Cldr.Calendar.default_cldr_calendar(),
374+
backend \\ Cldr.Date.default_backend()
375+
) do
376+
Cldr.DateTime.Format.date_time_formats(locale, calendar, backend)
377+
end
378+
379+
@doc """
380+
Returns a map of the available datetime formats for a
381+
given locale and calendar.
382+
383+
## Arguments
384+
385+
* `locale` is any locale returned by `Cldr.known_locale_names/0`
386+
or a `t:Cldr.LanguageTag.t/0`. The default is `Cldr.get_locale/0`.
387+
388+
* `calendar` is any calendar returned by `Cldr.DateTime.Format.calendars_for/1`
389+
The default is `:gregorian`.
390+
391+
* `backend` is any module that includes `use Cldr` and therefore
392+
is a `Cldr` backend module. The default is `Cldr.default_backend/0`.
393+
394+
## Examples:
395+
396+
iex> Cldr.DateTime.available_formats(:en)
397+
{:ok,
398+
%{
399+
yw: %{
400+
other: "'week' w 'of' Y",
401+
one: "'week' w 'of' Y",
402+
pluralize: :week_of_year
403+
},
404+
GyMMMEd: "E, MMM d, y G",
405+
Hms: "HH:mm:ss",
406+
MMMMW: %{
407+
other: "'week' W 'of' MMMM",
408+
one: "'week' W 'of' MMMM",
409+
pluralize: :week_of_month
410+
},
411+
E: "ccc",
412+
MMMd: "MMM d",
413+
yMEd: "E, M/d/y",
414+
yQQQ: "QQQ y",
415+
Ehm: %{unicode: "E h:mm a", ascii: "E h:mm a"},
416+
M: "L",
417+
hm: %{unicode: "h:mm a", ascii: "h:mm a"},
418+
yM: "M/y",
419+
GyMMMd: "MMM d, y G",
420+
GyMd: "M/d/y G",
421+
Gy: "y G",
422+
Hm: "HH:mm",
423+
EBhms: "E h:mm:ss B",
424+
d: "d",
425+
hms: %{unicode: "h:mm:ss a", ascii: "h:mm:ss a"},
426+
Ed: "d E",
427+
Ehms: %{unicode: "E h:mm:ss a", ascii: "E h:mm:ss a"},
428+
EHms: "E HH:mm:ss",
429+
Bh: "h B",
430+
h: %{unicode: "h a", ascii: "h a"},
431+
Bhms: "h:mm:ss B",
432+
Hmv: "HH:mm v",
433+
hmv: %{unicode: "h:mm a v", ascii: "h:mm a v"},
434+
yMd: "M/d/y",
435+
ms: "mm:ss",
436+
MMM: "LLL",
437+
y: "y",
438+
Bhm: "h:mm B",
439+
yMMM: "MMM y",
440+
yQQQQ: "QQQQ y",
441+
yMMMEd: "E, MMM d, y",
442+
yMMMM: "MMMM y",
443+
EBhm: "E h:mm B",
444+
Hmsv: "HH:mm:ss v",
445+
yMMMd: "MMM d, y",
446+
MEd: "E, M/d",
447+
EHm: "E HH:mm",
448+
GyMMM: "MMM y G",
449+
hmsv: %{unicode: "h:mm:ss a v", ascii: "h:mm:ss a v"},
450+
H: "HH",
451+
Md: "M/d",
452+
MMMEd: "E, MMM d",
453+
MMMMd: "MMMM d"
454+
}}
455+
456+
"""
457+
@spec available_formats(
458+
Locale.locale_reference(),
459+
Cldr.Calendar.calendar(),
460+
Cldr.backend()
461+
) :: {:ok, map()} | {:error, {atom, String.t()}}
462+
463+
def available_formats(
464+
locale \\ Cldr.get_locale(),
465+
calendar \\ Cldr.Calendar.default_cldr_calendar(),
466+
backend \\ Cldr.Date.default_backend()
467+
) do
468+
Format.date_time_available_formats(locale, calendar, backend)
469+
end
470+
324471
defp normalize_options(datetime, backend, []) do
325472
{locale, _backend} = Cldr.locale_and_backend_from(nil, backend)
326473
number_system = Cldr.Number.System.number_system_from_locale(locale, backend)

lib/cldr/format/date_time_format.ex

+50
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,56 @@ defmodule Cldr.DateTime.Format do
180180
backend.hour_format(locale)
181181
end
182182

183+
@doc """
184+
Returns a map of the standard date formats for a given
185+
locale and calendar.
186+
187+
### Arguments
188+
189+
* `locale` is any locale returned by `Cldr.known_locale_names/0`
190+
or a `t:Cldr.LanguageTag.t/0`. The default is `Cldr.get_locale/0`.
191+
192+
* `calendar` is any calendar returned by `Cldr.DateTime.Format.calendars_for/1`
193+
The default is `:gregorian`.
194+
195+
* `backend` is any module that includes `use Cldr` and therefore
196+
is a `Cldr` backend module. The default is `Cldr.default_backend/0`.
197+
198+
### Examples:
199+
200+
iex> Cldr.DateTime.Format.date_formats(:en, :gregorian, MyApp.Cldr)
201+
{:ok, %Cldr.Date.Formats{
202+
full: "EEEE, MMMM d, y",
203+
long: "MMMM d, y",
204+
medium: "MMM d, y",
205+
short: "M/d/yy"
206+
}}
207+
208+
iex> Cldr.DateTime.Format.date_formats(:en, :buddhist, MyApp.Cldr)
209+
{:ok, %Cldr.Date.Formats{
210+
full: "EEEE, MMMM d, y G",
211+
long: "MMMM d, y G",
212+
medium: "MMM d, y G",
213+
short: "M/d/y GGGGG"
214+
}}
215+
216+
"""
217+
@spec date_formats(
218+
Locale.locale_reference(),
219+
Cldr.Calendar.calendar(),
220+
Cldr.backend()
221+
) ::
222+
{:ok, Cldr.DateTime.Format.standard_formats()} | {:error, {atom, String.t()}}
223+
224+
def date_formats(
225+
locale \\ Cldr.get_locale(),
226+
calendar \\ Cldr.Calendar.default_cldr_calendar(),
227+
backend \\ Cldr.Date.default_backend()
228+
) do
229+
backend = Module.concat(backend, DateTime.Format)
230+
backend.date_formats(locale, calendar)
231+
end
232+
183233
@doc """
184234
Returns a map of the standard time formats for a given locale and calendar.
185235

lib/cldr/time.ex

+1-2
Original file line numberDiff line numberDiff line change
@@ -412,8 +412,7 @@ defmodule Cldr.Time do
412412
calendar \\ Cldr.Calendar.default_cldr_calendar(),
413413
backend \\ Cldr.Date.default_backend()
414414
) do
415-
backend = Module.concat(backend, DateTime.Format)
416-
backend.time_formats(locale, calendar)
415+
Cldr.DateTime.Format.time_formats(locale, calendar, backend)
417416
end
418417

419418
@doc """

mix.exs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
defmodule Cldr.DatesTimes.Mixfile do
22
use Mix.Project
33

4-
@version "2.19.1"
4+
@version "2.19.2"
55

66
def project do
77
[

mix/for_dialyzer.ex

+10
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ defmodule Cldr.DatesTimes.Dialyzer do
1515
{:ok, %{medium: _format_dt}} = MyApp.Cldr.DateTime.Format.time_formats(:en)
1616
end
1717

18+
def formats do
19+
{:ok, _} = Cldr.Date.formats()
20+
{:ok, _} = Cldr.Time.formats()
21+
{:ok, _} = Cldr.DateTime.formats()
22+
23+
{:ok, _} = Cldr.Date.available_formats()
24+
{:ok, _} = Cldr.Time.available_formats()
25+
{:ok, _} = Cldr.DateTime.available_formats()
26+
end
27+
1828
def format do
1929
_ = Cldr.DateTime.Format.calendars_for(:en, MyApp.Cldr)
2030
_ = Cldr.DateTime.Format.calendars_for("en", MyApp.Cldr)

test/cldr_dates_times_test.exs

+10
Original file line numberDiff line numberDiff line change
@@ -161,4 +161,14 @@ defmodule Cldr.DatesTimes.Test do
161161
assert {:ok, "July 7, 2024, 9:36:00 PM UTC"} =
162162
Cldr.DateTime.to_string(datetime, format: :long, style: :default)
163163
end
164+
165+
test "Symmetry of the format/3 and available_format/3 functions for Date, Time and DateTime" do
166+
assert {:ok, _} = Cldr.Date.formats()
167+
assert {:ok, _} = Cldr.Time.formats()
168+
assert {:ok, _} = Cldr.DateTime.formats()
169+
170+
assert {:ok, _} = Cldr.Date.available_formats()
171+
assert {:ok, _} = Cldr.Time.available_formats()
172+
assert {:ok, _} = Cldr.DateTime.available_formats()
173+
end
164174
end

0 commit comments

Comments
 (0)