Datix.Date (datix v0.1.1) View Source

A Date parser using Calendar.strftime format string.

Link to this section Summary

Functions

Parses a date string according to the given format.

Parses a date string according to the given format, erroring out for invalid arguments.

Link to this section Functions

Link to this function

parse(date_str, format_str, opts \\ [])

View Source

Specs

parse(String.t(), String.t(), list()) ::
  {:ok, Date.t()}
  | {:error, :invalid_date}
  | {:error, :invalid_input}
  | {:error, {:parse_error, [expected: String.t(), got: String.t()]}}
  | {:error, {:conflict, [expected: term(), got: term(), modifier: String.t()]}}
  | {:error, {:invalid_string, [{:modifier, String.t()}]}}
  | {:error, {:invalid_integer, [{:modifier, String.t()}]}}
  | {:error, {:invalid_modifier, [{:modifier, String.t()}]}}

Parses a date string according to the given format.

See the Calendar.strftime documentation for how to specify a format-string.

Options

  • :calendar - the calendar to build the Date, defaults to Calendar.ISO

  • :preferred_date - a string for the preferred format to show dates, it can't contain the %x format and defaults to "%Y-%m-%d" if the option is not received

  • :month_names - a list of the month names, if the option is not received it defaults to a list of month names in English

  • :abbreviated_month_names - a list of abbreviated month names, if the option is not received it defaults to a list of abbreviated month names in English

  • :day_of_week_names - a list of day names, if the option is not received it defaults to a list of day names in English

  • :abbreviated_day_of_week_names - a list of abbreviated day names, if the option is not received it defaults to a list of abbreviated day names in English

Missing values will be set to minimum.

Examples

    iex> Datix.Date.parse("2022-05-11", "%x")
    {:ok, ~D[2022-05-11]}

    iex> Datix.Date.parse("2021/01/10", "%Y/%m/%d")
    {:ok, ~D[2021-01-10]}

    iex> Datix.Date.parse("2021/01/10", "%x", preferred_date: "%Y/%m/%d")
    {:ok, ~D[2021-01-10]}

    iex> Datix.Date.parse("18", "%y")
    {:ok, ~D[0018-01-01]}

    iex> Datix.Date.parse("", "")
    {:ok, ~D[0000-01-01]}

    iex> Datix.Date.parse("1736/13/03", "%Y/%m/%d", calendar: Coptic)
    {:ok, ~D[1736-13-03 Cldr.Calendar.Coptic]}

    iex> Datix.Date.parse("Mi, 1.4.2020", "%a, %-d.%-m.%Y",
    ...>   abbreviated_day_of_week_names: ~w(Mo Di Mi Do Fr Sa So))
    {:ok, ~D[2020-04-01]}

    iex> Datix.Date.parse("Fr, 1.4.2020", "%a, %-d.%-m.%Y",
    ...>   abbreviated_day_of_week_names: ~w(Mo Di Mi Do Fr Sa So))
    {:error, :invalid_date}
Link to this function

parse!(date_str, format_str, opts \\ [])

View Source

Specs

parse!(String.t(), String.t(), list()) :: Date.t()

Parses a date string according to the given format, erroring out for invalid arguments.