Packages
ex_cldr_dates_times
0.2.1
2.25.6
2.25.5
2.25.4
2.25.3
2.25.2
2.25.1
2.25.0
2.24.2
2.24.1
2.24.0
2.23.0
2.22.0
2.21.0
2.20.3
2.20.2
2.20.0
2.19.2
2.19.1
2.19.0
retired
2.18.1
2.18.0
2.17.1
2.17.0
2.16.0
2.15.0
2.14.3
2.14.2
2.14.1
2.14.0
2.13.3
2.13.2
2.13.1
2.13.0
2.12.0
2.11.0
2.10.2
2.10.1
2.10.0
2.10.0-rc.3
2.10.0-rc.2
2.10.0-rc.1
2.10.0-rc.0
2.9.4
2.9.3
2.9.2
2.9.1
2.9.0
2.8.0
2.7.2
2.7.1
retired
2.7.0
2.7.0-rc.0
2.6.4
2.6.3
2.6.2
2.6.1
retired
2.6.0
2.6.0-rc.0
2.5.4
2.5.3
2.5.2
2.5.1
2.5.0
2.4.0
2.4.0-rc.0
2.3.0
2.2.4
2.2.3
2.2.2
2.2.1
2.2.0
2.1.0
2.0.2
2.0.1
2.0.0
1.4.0
1.3.1
1.3.0
1.2.1
1.2.0
1.0.1
1.0.0
1.0.0-rc.1
1.0.0-rc.0
retired
0.3.3
0.3.2
retired
0.3.1
retired
0.3.0
0.2.2
0.2.1
0.2.0
0.1.2
0.1.1
0.1.0
Date, Time and DateTime localization, internationalization and formatting functions using the Common Locale Data Repository (CLDR).
Current section
Files
Jump to
Current section
Files
lib/cldr/datetime/datetime_compiler.ex
defmodule Cldr.DateTime.Compiler do
@moduledoc """
Tokenizes and parses `Date`, `Time` and `DateTime` format strings.
During compilation, each of the date, time and datetime format
strings defined in CLDR are compiled into a list of
function bodies that are then grafted onto the function head
`Cldr.DateTime.Formatter.format/3`. As a result these compiled
formats execute with good performance.
For formats not defined in CLDR (ie a user defined format),
the tokenizing and parsing is performed, then list of function
bodies is created and then `Cldr.DateTime.Formatter.format/3`
recurses over the list, invoking each function and
collecting the results. This process is significantly slower
than that of the precompiled formats.
User defined formats can also be precompiled by configuring
them under the key `:precompile_datetime_formats`. For example:
config :ex_cldr,
precompile_datetime_formats: ["yy/dd", "hhh:mmm:sss"]
"""
alias Cldr.DateTime.Formatter
@doc """
Scan a number format definition and return
the tokens of a date/time/datetime format
string.
This function is designed to produce output
that is fed into `Cldr.DateTime.Compiler.compile/1`.
## Example
iex> Cldr.DateTime.Compiler.tokenize "yyyy/MM/dd"
{:ok,
[{:year, 1, 4}, {:literal, 1, "/"}, {:month, 1, 2}, {:literal, 1, "/"},
{:day_of_month, 1, 2}], 1}
"""
def tokenize(definition) when is_binary(definition) do
definition
|> String.to_charlist
|> :datetime_format_lexer.string()
end
def tokenize(%{number_system: _numbers, format: value}) do
tokenize(value)
end
@doc """
Parse a number format definition
* `format_string` is a string defining how a date/time/datetime
is to be formatted. See `Cldr.DateTime.Formatter` for the list
of supported format symbols.
Returns is a list of function bodies which are grafted onto
a function head in `Cldr.DateTime.Formatter` at compile time
to produce a series of functions that process a given format
string efficiently.
"""
@spec compile(String.t) :: {:ok, List.t} | {:error, String.t}
def compile(format_string)
def compile("") do
{:error, "empty format string cannot be compiled"}
end
def compile(nil) do
{:error, "no format string or token list provided"}
end
def compile(definition) when is_binary(definition) do
{:ok, tokens, _end_line} = tokenize(definition)
transforms = Enum.map(tokens, fn {fun, _line, count} ->
quote do
Formatter.unquote(fun)(var!(date), unquote(count), var!(locale), var!(options))
end
end)
{:ok, transforms}
end
def compile(%{number_system: _number_system, format: value}) do
compile(value)
end
def compile(arg) do
raise ArgumentError, message: "No idea how to compile format: #{inspect arg}"
end
end