DateTimeParser.Parser behaviour (DateTimeParser v1.1.0) View Source

Interface for the DateTimeParser to use when parsing a string.

The flow is:

  1. Preflight the string to see if the parser is appropriate. Sometimes the parsing can happen at this stage if it's a simple parser, for example it can be done in a single regex. Results of the preflight, if needed, can be stored in the struct.preflight.
  2. Parse the string. You have context/0 to check if you should return a time, date, or datetime. Also make sure you're honoring the user's options supplied in struct.opts

You may create your own parser and use it with the DateTimeParser by creating a module that follows this behaviour.

Link to this section Summary

Callbacks

Parse the string.

Determine if the string is appropriate to parse with this parser. If not, then other parsers will be attempted.

Link to this section Types

Specs

context() :: :datetime | :date | :time

Specs

t() :: %DateTimeParser.Parser{
  context: context(),
  mod: module(),
  opts: Keyword.t(),
  preflight: any(),
  string: String.t()
}

Link to this section Callbacks

Specs

parse(t()) ::
  {:ok, DateTime.t() | NaiveDateTime.t() | Time.t() | Date.t()}
  | {:error, any()}

Parse the string.

Specs

preflight(t()) :: {:ok, t()} | {:error, :not_compatible}

Determine if the string is appropriate to parse with this parser. If not, then other parsers will be attempted.