Datix (datix v0.1.1) View Source

A date-time parser using Calendar.strftime format strings.

Link to this section Summary

Functions

Parses a date-time string according to the given format.

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

Link to this section Types

Specs

t() :: %{
  optional(:am_pm) => :am | :pm,
  optional(:day) => pos_integer(),
  optional(:day_of_week) => pos_integer(),
  optional(:day_of_year) => pos_integer(),
  optional(:hour) => pos_integer(),
  optional(:hour_12) => pos_integer(),
  optional(:microsecond) => pos_integer(),
  optional(:minute) => pos_integer(),
  optional(:month) => pos_integer(),
  optional(:quarter) => pos_integer(),
  optional(:second) => pos_integer(),
  optional(:year) => pos_integer(),
  optional(:year_2_digit) => pos_integer(),
  optional(:zone_abbr) => String.t(),
  optional(:zone_offset) => integer()
}

Link to this section Functions

Link to this function

strptime(date_time_str, format_str, opts \\ [])

View Source

Specs

strptime(String.t(), String.t(), keyword()) ::
  {:ok, t()}
  | {: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-time string according to the given format.

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

Options

  • :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

  • :preferred_time - a string for the preferred format to show times, it can't contain the %X format and defaults to "%H:%M:%S" if the option is not received

  • :am_pm_names - a keyword list with the names of the period of the day, defaults to [am: "am", pm: "pm"].

Examples

    iex> Datix.strptime("2021/01/10", "%Y/%m/%d")
    {:ok, %{day: 10, month: 1, year: 2021}}

    iex> Datix.strptime("21/01/10", "%y/%m/%d")
    {:ok, %{day: 10, month: 1, year_2_digit: 21}}

    iex> Datix.strptime("13/14/15", "%H/%M/%S")
    {:ok, %{hour: 13, minute: 14, second: 15}}

    iex> Datix.strptime("1 PM", "%-I %p")
    {:ok, %{am_pm: :pm, hour_12: 1}}

    iex> Datix.strptime("Tuesday", "%A")
    {:ok, %{day_of_week: 2}}

    iex> Datix.strptime("Tue", "%a")
    {:ok, %{day_of_week: 2}}

    iex> Datix.strptime("Di", "%a",
    ...>   abbreviated_day_of_week_names: ~w(Mo Di Mi Do Fr Sa So))
    {:ok, %{day_of_week: 2}}
Link to this function

strptime!(date_time_str, format_str, opts \\ [])

View Source

Specs

strptime!(String.t(), String.t(), keyword()) :: t()

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