Skip to content

Commit fc8dcc4

Browse files
committed
Improve docs for specifying formats
1 parent 0aee17d commit fc8dcc4

File tree

8 files changed

+71
-26
lines changed

8 files changed

+71
-26
lines changed

CHANGELOG.md

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
## Cldr_Dates_Times v2.19.0
66

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

99
### Bug Fixes
1010

@@ -16,10 +16,16 @@ This is the changelog for Cldr_Dates_Times v2.19.0 released on _____, 2024. For
1616

1717
* Adds support for partial dates in `Cldr.Date.to_string/2`. Partial dates are maps with one or more of the fields `:year`, `:month`, `:day` and `:calendar`.
1818

19-
* Adds support for deriving the "best match" format for a date based upon the users requested format or deriving from the available date fields.
19+
* Adds support for partial time in `Cldr.Time.to_string/2`. Partial times are maps with one or more of the fields `:hour`, `:minute`, and `:second`.
20+
21+
* Adds support for partial datetimes in `Cldr.DateTime.to_string/2`. Partial datetimes are maps with one or more of the fields `:year`, `:month`, `:day`, `:hour`, `:minute`, `:second` and `:calendar`.
22+
23+
* Adds support for deriving the "best match" format for a date, time or datetime based upon the users requested format or deriving from the available date, time or datetime fields.
2024

2125
* Adds support for formatting using format IDs (atoms that are keys to locale-independent formats) in `Cldr.Date.to_string/2` and `Cldr.Time.to_string/2`. They have been previously supported in `Cldr.DateTime.to_string/2` only.
2226

27+
* Add `MyApp.Cldr.DateTime.Format.common_date_time_format_names/0`.
28+
2329
* Improve the error message if `:hour` is outside the valid range.
2430

2531
## Cldr_Dates_Times v2.18.1

README.md

+14-8
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ A `backend` module is required that is used to host the functions that manage CL
5252
default_backend: MyApp.Cldr
5353
```
5454

55-
## Usage Introduction
55+
## Introduction
5656

5757
`ex_cldr_dates_times` is an addon library application for [ex_cldr](https://hex.pm/packages/ex_cldr) that provides localisation and formatting for dates, times and date_times.
5858

@@ -83,11 +83,11 @@ iex> h MyApp.Cldr.DateTime.to_string
8383
iex> h MyApp.Cldr.DateTime.Relative.to_string
8484
```
8585

86-
## Date, Time and DateTime Localization Formatting
86+
## Date, Time and DateTime Localization Formats
8787

88-
Dates, Times and DateTimes can be formatted using:
88+
Dates, Times and DateTimes can be formatted using standard formats, format strings or format IDs.
8989

90-
* The format types defined for each locale. These format types provide cross-locale standardisation and therefore should be preferred where possible. The format types, implemented for `MyApp.Cldr.Date.to_string/2`, `MyApp.Cldr.Time.to_string/2`,`MyApp.Cldr.DateTime.to_string/2` are `:short`, `:medium`, `:long` and `:full`. The default is `:medium`. For example, assuming a configured backend called `MyApp.Cldr`:
90+
* Standard formats provide cross-locale standardisation and therefore should be preferred where possible. The format types, implemented for `MyApp.Cldr.Date.to_string/2`, `MyApp.Cldr.Time.to_string/2`,`MyApp.Cldr.DateTime.to_string/2` are `:short`, `:medium`, `:long` and `:full`. The default is `:medium`. For example, assuming a configured backend called `MyApp.Cldr`:
9191

9292
```elixir
9393
iex> MyApp.Cldr.DateTime.to_string ~U[2020-05-30 03:52:56Z], format: :short
@@ -103,17 +103,17 @@ Dates, Times and DateTimes can be formatted using:
103103
{:ok, "30 mai 2020 à 03:52:56 UTC"}
104104
```
105105

106-
* A user specified format string. A format string uses one or more formatting symbols to define what date and time elements should be places in the format. A simple example to format the time into hours and minutes:
106+
* Format strings use one or more formatting symbols to define what date and time elements should be places in the format. A simple example to format the time into hours and minutes:
107107

108108
```elixir
109109
iex> MyApp.Cldr.DateTime.to_string ~U[2020-05-30 03:52:56Z], format: "hh:mm"
110110
{:ok, "03:52"}
111111
```
112112

113-
* For `DateTime`s there is also a set of predefined format name. These format names are returned by `MyApp.Cldr.DateTime.Format.date_time_available_formats/0` (assuming your backend is `MyApp.Cldr`). The set of common format names across all locales configured in `ex_cldr` can be returned by `Cldr.DateTime.Format.common_date_time_format_names`. These format names can be used with the `:format` parameter to `Cldr.DateTime.to_string/2` module only.
113+
* Format IDs are an atom that is a key into a map of formats defined by CLDR. These format IDs are returned by `MyApp.Cldr.DateTime.Format.date_time_available_formats/2` (assuming your backend is `MyApp.Cldr`). The set of common format names across all locales configured in `ex_cldr` can be returned by `MyApp.Cldr.DateTime.Format.common_date_time_format_names/0`.
114114

115115
```elixir
116-
iex> MyApp.Cldr.DateTime.Format.date_time_available_formats
116+
iex> MyApp.Cldr.DateTime.Format.date_time_available_formats()
117117
%{mmmm_w_count_one: "'week' W 'of' MMMM", gy_mmm: "MMM y G", md: "M/d",
118118
mmm_md: "MMMM d", e_hms: "E HH:mm:ss", ed: "d E", y_mmm: "MMM y",
119119
e_hm: "E HH:mm", mmm_ed: "E, MMM d", y_mmm_ed: "E, MMM d, y",
@@ -128,14 +128,20 @@ Dates, Times and DateTimes can be formatted using:
128128

129129
# These format types can be invoked for any locale - meaning
130130
# these format names are defined for all configured locales.
131-
iex> Cldr.DateTime.Format.common_date_time_format_names(MyApp.Cldr)
131+
iex> MyApp.Cldr.DateTime.Format.common_date_time_format_names()
132132
[:gy_mmm, :md, :mmm_md, :e_hms, :ed, :y_mmm, :e_hm, :mmm_ed, :y_mmm_ed,
133133
:gy_mm_md, :mmm, :y_md, :gy, :hms, :hm, :y_mmmm, :m, :gy_mmm_ed, :y_qqq, :e,
134134
:y_qqqq, :hmsv, :mmmm_w_count_other, :ehm, :y_m_ed, :h, :hmv, :yw_count_other,
135135
:mm_md, :y_m, :m_ed, :ms, :d, :y_mm_md, :y, :ehms]
136136

137137
iex> Cldr.DateTime.to_string ~U[2020-05-30 03:52:56Z], MyApp.Cldr, format: :gy_mmm_ed
138138
{:ok, "Sat, May 30, 2020 AD"}
139+
140+
iex(2)> Cldr.Time.to_string ~U[2020-05-30 03:52:56Z], MyApp.Cldr, format: :hm
141+
{:ok, "3:52 AM"}
142+
143+
iex(3)> Cldr.Date.to_string ~U[2020-05-30 03:52:56Z], MyApp.Cldr, format: :yMd
144+
{:ok, "5/30/2020"}
139145
```
140146

141147
## Format strings

lib/cldr/backend/date_time.ex

+12-6
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ defmodule Cldr.DateAndTime.Backend do
2424
2525
* `:format` is one of `:short`, `:medium`, `:long`, `:full` or a format string or
2626
any of the keys in the map returned by `Cldr.DateTime.date_time_formats/3`.
27-
The default is `:medium`.
27+
The default is `:medium`. See [here](README.md#date-time-and-datetime-localization-formats)
28+
for more information about specifiying formats.
2829
2930
* `:date_format` is any one of `:short`, `:medium`, `:long`, `:full`. If defined,
3031
this option is used to format the date part of the date time. This option is
@@ -110,7 +111,8 @@ defmodule Cldr.DateAndTime.Backend do
110111
111112
* `:format` is one of `:short`, `:medium`, `:long`, `:full` or a format string or
112113
any of the keys in the map returned by `Cldr.DateTime.date_time_formats/3`.
113-
The default is `:medium`.
114+
The default is `:medium`. See [here](README.md#date-time-and-datetime-localization-formats)
115+
for more information about specifiying formats.
114116
115117
* `:date_format` is any one of `:short`, `:medium`, `:long`, `:full`. If defined,
116118
this option is used to format the date part of the date time. This option is
@@ -192,7 +194,8 @@ defmodule Cldr.DateAndTime.Backend do
192194
dates having `:year`, `:month`, `:day` and `:calendar` fields). The
193195
default for partial dates is to derive a candidate format id from the date and
194196
find the best match from the formats returned by
195-
`Cldr.Date.available_formats/3`.
197+
`Cldr.Date.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats)
198+
for more information about specifiying formats.
196199
197200
* `:locale:` any locale returned by `Cldr.known_locale_names/1`.
198201
The default is `Cldr.get_locale/0`.
@@ -276,7 +279,8 @@ defmodule Cldr.DateAndTime.Backend do
276279
dates having `:year`, `:month`, `:day` and `:calendar` fields). The
277280
default for partial dates is to derive a candidate format id from the date and
278281
find the best match from the formats returned by
279-
`Cldr.Date.available_formats/3`.
282+
`Cldr.Date.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats)
283+
for more information about specifiying formats.
280284
281285
* `:locale:` any locale returned by `Cldr.known_locale_names/1`.
282286
The default is `Cldr.get_locale/0`.
@@ -361,7 +365,8 @@ defmodule Cldr.DateAndTime.Backend do
361365
times having `:hour`, `:minute` and `:second` fields). The
362366
default for partial times is to derive a candidate format from the time and
363367
find the best match from the formats returned by
364-
`Cldr.Time.available_formats/2`.
368+
`Cldr.Time.available_formats/2`. See [here](README.md#date-time-and-datetime-localization-formats)
369+
for more information about specifiying formats.
365370
366371
* `:locale` any locale returned by `Cldr.known_locale_names/1`. The default is
367372
`Cldr.get_locale/0`.
@@ -438,7 +443,8 @@ defmodule Cldr.DateAndTime.Backend do
438443
times having `:hour`, `:minute` and `:second` fields). The
439444
default for partial times is to derive a candidate format from the time and
440445
find the best match from the formats returned by
441-
`Cldr.Time.available_formats/2`.
446+
`Cldr.Time.available_formats/2`. See [here](README.md#date-time-and-datetime-localization-formats)
447+
for more information about specifiying formats.
442448
443449
* `:locale` any locale returned by `Cldr.known_locale_names/1`. The default is
444450
`Cldr.get_locale/0`.

lib/cldr/backend/format.ex

+21
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,27 @@ defmodule Cldr.DateTime.Format.Backend do
618618
end
619619
end
620620

621+
@doc """
622+
Returns a list of the date_time format IDs that are
623+
available in all known locales.
624+
625+
The format IDs returned by `common_date_time_format_names/0`
626+
are guaranteed to be available in all known locales,
627+
628+
## Example:
629+
630+
iex> #{inspect(__MODULE__)}.common_date_time_format_names()
631+
[:Bh, :Bhm, :Bhms, :E, :EBhm, :EBhms, :EHm, :EHms, :Ed, :Ehm, :Ehms, :Gy,
632+
:GyMMM, :GyMMMEd, :GyMMMd, :GyMd, :H, :Hm, :Hms, :Hmsv, :Hmv, :M, :MEd, :MMM,
633+
:MMMEd, :MMMMW, :MMMMd, :MMMd, :Md, :d, :h, :hm, :hms, :hmsv, :hmv, :ms, :y,
634+
:yM, :yMEd, :yMMM, :yMMMEd, :yMMMM, :yMMMd, :yMd, :yQQQ, :yQQQQ, :yw]
635+
636+
"""
637+
@spec common_date_time_format_names() :: [Format.format_id()]
638+
def common_date_time_format_names do
639+
Cldr.DateTime.Format.common_date_time_format_names(unquote(backend))
640+
end
641+
621642
@doc """
622643
Returns the fallback format for a given
623644
locale and calendar type

lib/cldr/date.ex

+7-5
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,13 @@ defmodule Cldr.Date do
5858
5959
### Options
6060
61-
* `:format` is one of `:short`, `:medium`, `:long`, `:full`, or a format id
61+
* `:format` is one of `:short`, `:medium`, `:long`, `:full`, or a format ID
6262
or a format string. The default is `:medium` for full dates (that is,
6363
dates having `:year`, `:month`, `:day` and `:calendar` fields). The
64-
default for partial dates is to derive a candidate format id from the date and
64+
default for partial dates is to derive a candidate format ID from the date and
6565
find the best match from the formats returned by
66-
`Cldr.Date.available_formats/3`.
66+
`Cldr.Date.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats)
67+
for more information about specifiying formats.
6768
6869
* `:locale:` any locale returned by `Cldr.known_locale_names/1`.
6970
The default is `Cldr.get_locale/0`.
@@ -182,12 +183,13 @@ defmodule Cldr.Date do
182183
183184
### Options
184185
185-
* `:format` is one of `:short`, `:medium`, `:long`, `:full`, or a format id
186+
* `:format` is one of `:short`, `:medium`, `:long`, `:full`, or a format ID
186187
or a format string. The default is `:medium` for full dates (that is,
187188
dates having `:year`, `:month`, `:day` and `:calendar` fields). The
188189
default for partial dates is to derive a candidate format from the date and
189190
find the best match from the formats returned by
190-
`Cldr.Date.available_formats/3`.
191+
`Cldr.Date.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats)
192+
for more information about specifiying formats.
191193
192194
* `:locale` is any valid locale name returned by `Cldr.known_locale_names/0`
193195
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`.

lib/cldr/date_time.ex

+4-2
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,8 @@ defmodule Cldr.DateTime do
103103
104104
* `:format` is one of `:short`, `:medium`, `:long`, `:full` or a format string or
105105
any of the keys in the map returned by `Cldr.DateTime.Format.date_time_formats/3`.
106-
The default is `:medium`.
106+
The default is `:medium`. See [here](README.md#date-time-and-datetime-localization-formats)
107+
for more information about specifiying formats.
107108
108109
* `:date_format` is any one of `:short`, `:medium`, `:long`, `:full`. If defined,
109110
this option is used to format the date part of the date time. This option is
@@ -250,7 +251,8 @@ defmodule Cldr.DateTime do
250251
251252
* `:format` is one of `:short`, `:medium`, `:long`, `:full` or a format string or
252253
any of the keys returned by `Cldr.DateTime.Format.date_time_formats/3`.
253-
The default is `:medium`.
254+
The default is `:medium`. See [here](README.md#date-time-and-datetime-localization-formats)
255+
for more information about specifiying formats.
254256
255257
* `:style` is either `:at` or `:default`. When set to `:at` the datetime may
256258
be formatted with a localised string representing `<date> at <time>` if such

lib/cldr/format/date_time_format.ex

+1-1
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ defmodule Cldr.DateTime.Format do
498498
:yM, :yMEd, :yMMM, :yMMMEd, :yMMMM, :yMMMd, :yMd, :yQQQ, :yQQQQ, :yw]
499499
500500
"""
501-
@spec common_date_time_format_names(backend :: Cldr.backend()) :: [atom]
501+
@spec common_date_time_format_names(backend :: Cldr.backend()) :: [format_id()]
502502
def common_date_time_format_names(backend \\ Cldr.Date.default_backend()) do
503503
datetime_module = Module.concat(backend, DateTime.Format)
504504

lib/cldr/time.ex

+4-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ defmodule Cldr.Time do
7878
times having `:hour`, `:minute` and `:second` fields). The
7979
default for partial times is to derive a candidate format from the time and
8080
find the best match from the formats returned by
81-
`Cldr.Time.available_formats/3`.
81+
`Cldr.Time.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats)
82+
for more information about specifiying formats.
8283
8384
* `:locale` any locale returned by `Cldr.known_locale_names/1`. The default is
8485
`Cldr.get_locale/0`.
@@ -194,7 +195,8 @@ defmodule Cldr.Time do
194195
times having `:hour`, `:minute` and `:second` fields). The
195196
default for partial times is to derive a candidate format from the time and
196197
find the best match from the formats returned by
197-
`Cldr.Time.available_formats/3`.
198+
`Cldr.Time.available_formats/3`. See [here](README.md#date-time-and-datetime-localization-formats)
199+
for more information about specifiying formats.
198200
199201
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
200202
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`.

0 commit comments

Comments
 (0)