DateTimeParser.Parser behaviour (DateTimeParser v1.2.1)

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.

Summary

Callbacks

Parse the string.

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

Types

context()

@type context() :: :datetime | :date | :time | :best

t()

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

Callbacks

parse(t)

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

Parse the string.

preflight(t)

@callback 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.