View Source PgInterop.Interval.ISO8601Parser (electric v0.9.5)

This module parses ISO-8601 duration strings into Interval structs.

Implementation taken from https://github.com/bitwalker/timex/blob/3.7.9/lib/parse/duration/parsers/iso8601.ex and adapted to fill a different structure + support negatives.

Summary

Functions

Parses an ISO-8601 formatted duration string into a Interval struct. The parse result is wrapped in a :ok/:error tuple.

Functions

@spec parse(String.t()) :: {:ok, PgInterop.Interval.t()} | {:error, term()}

Parses an ISO-8601 formatted duration string into a Interval struct. The parse result is wrapped in a :ok/:error tuple.

Examples

iex> parse("P15Y3M2DT1H14M37.25S")
{:ok, Interval.parse!("P15Y3M2DT1H14M37.25S")}

iex> parse("P15Y3M2D")
{:ok, Interval.parse!("P15Y3M2D")}

iex> parse("PT3H12M25.001S")
{:ok, Interval.parse!("PT3H12M25.001S")}

iex> parse("P2W1D")
{:ok, Interval.parse!("P15D")}

iex> parse("")
{:error, "input string cannot be empty"}
iex> parse("P15YT3D")
{:error, "invalid use of date component after time separator"}
iex> parse("P15Y3H")
{:error, "missing T separator between date and time components"}
iex> parse("P15YTT3H")
{:error, "encountered duplicate time separator T"}

iex> parse("P1O")
{:error, "unexpected token O"}
iex> parse("P1-1D")
{:error, "invalid number `1-1`"}
iex> parse("P1")
{:error, "unexpected end of input at 1"}
iex> parse("P11")
{:error, "unexpected end of input at 1"}
iex> parse("PT")
{:error, "unexpected end of input at T"}
iex> parse("PO")
{:error, "expected numeric, but got `O`"}
iex> parse("O")
{:error, "expected P, got `O`"}