Cldr.DateTime.Compiler (Cldr Dates & Times v2.10.2) View Source

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 format/3 in a backend module. 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 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"]

Link to this section Summary

Functions

Parse a number format definition

Scan a number format definition and return the tokens of a date/time/datetime format string.

Link to this section Functions

Link to this function

compile(format_string, backend, context)

View Source

Specs

compile(String.t(), module(), module()) ::
  {:ok, Cldr.Calendar.calendar()} | {:error, String.t()}

Parse a number format definition

Arguments

  • 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

Returns 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.

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/3.

Arguments

  • definition is a date, datetime or time format string

Returns

A list of 3-tuples which represent the tokens of the format definition

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}