Parse, validate, and humanize EDTF date strings

Summary

Functions

Humanize an EDTF date string

Parse an EDTF date string

Convert an EDTF date string (or a previously parsed EDTF.Date, EDTF.Interval, or EDTF.Aggregate struct) into a {start_date, end_date} tuple of Date.t() values.

Validate an EDTF date string

Functions

humanize(edtf)

Humanize an EDTF date string

Example:

  iex> humanize("1999-06-10")
  "June 10, 1999"

  iex> humanize("bad date!")
  {:error, :invalid_format}

parse(edtf)

Parse an EDTF date string

Example:

  iex> parse("1999-06-10")
  {:ok, %EDTF.Date{level: 0, type: :date, values: [1999, 5, 10]}}

  iex> parse("bad date!")
  {:error, :invalid_format}

to_date_range(edtf)

Convert an EDTF date string (or a previously parsed EDTF.Date, EDTF.Interval, or EDTF.Aggregate struct) into a {start_date, end_date} tuple of Date.t() values.

Explicitly open inputs (/.., ../, aggregate ..-continuations) produce :unbounded on that side. Inputs with an unknown bound (1985/, /1985) produce :unknown. Qualifiers (~, ?, %) are ignored; the range uses the nominal date. Unspecified-digit suffixes (e.g. 19XX) expand to their full span. See EDTF.DateRange for the full semantics.

Returns {:error, :invalid_format} when parsing fails, {:error, :out_of_range} when Date.new/3 itself rejects a value, and {:error, :unsupported} for shapes the converter declines (e.g. fully-unknown year XXXX or non-suffix unspecified digits like X9X2).

Example:

  iex> to_date_range("1999-06-10")
  {:ok, {~D[1999-06-10], ~D[1999-06-10]}}

  iex> to_date_range("1985/..")
  {:ok, {~D[1985-01-01], :unbounded}}

  iex> to_date_range("19XX")
  {:ok, {~D[1900-01-01], ~D[1999-12-31]}}

  iex> to_date_range("[1667, 1668, 1670..1672]")
  {:ok, {~D[1667-01-01], ~D[1672-12-31]}}

  iex> to_date_range("bad date!")
  {:error, :invalid_format}

validate(edtf)

Validate an EDTF date string

Example:

  iex> validate("1999-06-10")
  {:ok, "1999-06-10"}

  iex> validate("bad date!")
  {:error, :invalid_format}