Skip to content

Commit

Permalink
Add MyApp.Cldr.DateTime.date_time_at_formats/2
Browse files Browse the repository at this point in the history
  • Loading branch information
kipcole9 committed Oct 13, 2023
1 parent 56c41aa commit a3edd42
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 16 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.0

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

### Enhancements

* Add support for "at" style formatting for `DateTime` structs. This style is documented in [TR35](https://unicode.org/reports/tr35/tr35-dates.html#Date_Time_Combination_Examples) and was introduced in [CLDR 43](https://cldr.unicode.org/index/downloads/cldr-43). Thanks to @jueberschlag for the report and motivation to get this done.

## Cldr_Dates_Times v2.14.3

This is the changelog for Cldr_Dates_Times v2.14.3 released on October 10th, 2023. For older changelogs please consult the release tag on [GitHub](https://github.com/elixir-cldr/cldr_cldr_dates_times/tags)
Expand Down
98 changes: 83 additions & 15 deletions lib/cldr/backend/format.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ defmodule Cldr.DateTime.Format.Backend do
## Arguments
* `locale` is any valid locale name returned by `Cldr.known_locale_names/0`
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`
or a `Cldr.LanguageTag` struct. The default is `Cldr.get_locale/0`.
## Example
Expand Down Expand Up @@ -70,10 +70,10 @@ defmodule Cldr.DateTime.Format.Backend do
## Arguments
* `locale` is any locale returned by `Cldr.known_locale_names/0`
* `locale` is any locale returned by `Cldr.known_locale_names/0`.
* `calendar` is any calendar returned by `Cldr.DateTime.Format.calendars_for/1`
The default is `:gregorian`
The default is `:gregorian`.
## Examples:
Expand Down Expand Up @@ -117,10 +117,10 @@ defmodule Cldr.DateTime.Format.Backend do
## Arguments
* `locale` is any locale returned by `Cldr.known_locale_names/0`
* `locale` is any locale returned by `Cldr.known_locale_names/0`.
* `calendar` is any calendar returned by `Cldr.DateTime.Format.calendars_for/1`
The default is `:gregorian`
The default is `:gregorian`.
## Examples:
Expand Down Expand Up @@ -164,10 +164,10 @@ defmodule Cldr.DateTime.Format.Backend do
## Arguments
* `locale` is any locale returned by `Cldr.known_locale_names/0`
* `locale` is any locale returned by `Cldr.known_locale_names/0`.
* `calendar` is any calendar returned by `Cldr.DateTime.Format.calendars_for/1`
The default is `:gregorian`
The default is `:gregorian`.
## Examples:
Expand Down Expand Up @@ -206,6 +206,58 @@ defmodule Cldr.DateTime.Format.Backend do
end
end

@doc """
Returns a map of the standard datetime "at" formats for a given
locale and calendar.
An "at" format is one where the datetime is formatted with the
date part separated from the time part by a localized version
of "at".
## Arguments
* `locale` is any locale returned by `Cldr.known_locale_names/0`.
* `calendar` is any calendar returned by `Cldr.DateTime.Format.calendars_for/1`
The default is `:gregorian`,
## Examples:
iex> #{inspect(__MODULE__)}.date_time_at_formats(:en)
{:ok, %Cldr.DateTime.Styles{
full: "{1} 'at' {0}",
long: "{1} 'at' {0}",
medium: "{1}, {0}",
short: "{1}, {0}"}
}
iex> #{inspect(__MODULE__)}.date_time_at_formats(:en, :buddhist)
{:ok, %Cldr.DateTime.Styles{
full: "{1} 'at' {0}",
long: "{1} 'at' {0}",
medium: "{1}, {0}",
short: "{1}, {0}"}
}
"""
@spec date_time_at_formats(Locale.locale_reference(), calendar) ::
{:ok, map()} | {:error, {module(), String.t()}}

def date_time_at_formats(
locale \\ unquote(backend).get_locale(),
calendar \\ Cldr.Calendar.default_cldr_calendar()
)

def date_time_at_formats(%LanguageTag{cldr_locale_name: cldr_locale_name}, cldr_calendar) do
date_time_at_formats(cldr_locale_name, cldr_calendar)
end

def date_time_at_formats(locale_name, calendar) when is_binary(locale_name) do
with {:ok, locale} <- unquote(backend).validate_locale(locale_name) do
date_time_at_formats(locale, calendar)
end
end

@doc """
Returns a map of the available non-standard datetime formats for a
given locale and calendar.
Expand Down Expand Up @@ -482,33 +534,45 @@ defmodule Cldr.DateTime.Format.Backend do
|> Map.get(:dates)
|> get_in([:calendars, calendar])

formats = struct(Cldr.Date.Styles, Map.get(calendar_data, :date_formats))
date_formats = struct(Cldr.Date.Styles, Map.get(calendar_data, :date_formats))

def date_formats(unquote(locale), unquote(calendar)) do
{:ok, unquote(Macro.escape(formats))}
{:ok, unquote(Macro.escape(date_formats))}
end

formats = struct(Cldr.Time.Styles, Map.get(calendar_data, :time_formats))
time_formats = struct(Cldr.Time.Styles, Map.get(calendar_data, :time_formats))

def time_formats(unquote(locale), unquote(calendar)) do
{:ok, unquote(Macro.escape(formats))}
{:ok, unquote(Macro.escape(time_formats))}
end

formats =
date_time_formats =
struct(
Cldr.DateTime.Styles,
Map.get(calendar_data, :date_time_formats)
|> Map.take(@standard_formats)
)

def date_time_formats(unquote(locale), unquote(calendar)) do
{:ok, unquote(Macro.escape(formats))}
{:ok, unquote(Macro.escape(date_time_formats))}
end

date_time_at_formats =
struct(
Cldr.DateTime.Styles,
Map.get(calendar_data, :date_time_formats_at_time)
|> Map.get(:standard)
|> Map.take(@standard_formats)
)

def date_time_at_formats(unquote(locale), unquote(calendar)) do
{:ok, unquote(Macro.escape(date_time_at_formats))}
end

formats = get_in(calendar_data, [:date_time_formats, :available_formats])
available_formats = get_in(calendar_data, [:date_time_formats, :available_formats])

def date_time_available_formats(unquote(locale), unquote(calendar)) do
{:ok, unquote(Macro.escape(formats))}
{:ok, unquote(Macro.escape(available_formats))}
end

formats = get_in(calendar_data, [:date_time_formats, :interval_formats])
Expand Down Expand Up @@ -552,6 +616,9 @@ defmodule Cldr.DateTime.Format.Backend do
def date_time_formats(unquote(locale), calendar),
do: {:error, Cldr.Calendar.calendar_error(calendar)}

def date_time_at_formats(unquote(locale), calendar),
do: {:error, Cldr.Calendar.calendar_error(calendar)}

def date_time_available_formats(unquote(locale), calendar),
do: {:error, Cldr.Calendar.calendar_error(calendar)}

Expand All @@ -569,6 +636,7 @@ defmodule Cldr.DateTime.Format.Backend do
def date_formats(locale, _calendar), do: {:error, Locale.locale_error(locale)}
def time_formats(locale, _calendar), do: {:error, Locale.locale_error(locale)}
def date_time_formats(locale, _calendar), do: {:error, Locale.locale_error(locale)}
def date_time_at_formats(locale, _calendar), do: {:error, Locale.locale_error(locale)}

def date_time_available_formats(locale, _calendar),
do: {:error, Locale.locale_error(locale)}
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.14.3"
@version "2.15.0-dev"

def project do
[
Expand Down

0 comments on commit a3edd42

Please sign in to comment.