# `PgInterop.Interval.ISO8601Parser`
[🔗](https://github.com/electric-sql/electric/tree/%40core/sync-service%401.6.2/packages/sync-service/lib/pg_interop/interval/iso8601_parser.ex#L1)

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.

# `parse`

```elixir
@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`"}

---

*Consult [api-reference.md](api-reference.md) for complete listing*
