DateTimeParser v1.0.0 DateTimeParser.Parser behaviour 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

Link to this type

context()

View Source
context() :: :datetime | :date | :time
Link to this type

t()

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

Link to this section Callbacks

Link to this callback

parse(t)

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

Parse the string.

Link to this callback

preflight(t)

View Source
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.