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

This module parses Postgres classic and SQL strings

# `parse`

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

Parses an Postgres classic and SQL formatted duration string into
a Interval struct. The parse result is wrapped in a :ok/:error tuple.

## Examples

    iex> parse("1-2")
    {:ok, Interval.parse!("P1Y2M")}

    iex> parse("@ 1-2")
    {:ok, Interval.parse!("P1Y2M")}

    iex> parse("-1-2 +3 -4:05:06")
    {:ok, Interval.parse!("P-1Y-2M3DT-4H-5M-6S")}

    iex> parse("-1-2 -5:10.1")
    {:ok, Interval.parse!("P-1Y-2MT-5M-10.1S")}

    iex> parse("3 4:05:06")
    {:ok, Interval.parse!("P3DT4H5M6S")}

    iex> parse("1 year 2 months 3 days 4 hours 5 minutes 6 seconds")
    {:ok, Interval.parse!("P1Y2M3DT4H5M6S")}

    iex> parse("1 year 2-1 3 days +2")
    {:ok, Interval.parse!("P3Y1M3DT2S")}

    iex> parse("1 year 2-1 3 days -2")
    {:ok, Interval.parse!("P3Y1M3DT-2S")}

    iex> parse("1 year 2-1 3 days 2:2")
    {:ok, Interval.parse!("P3Y1M3DT2H2M")}

    iex> parse("1 year 2-1 3 days 2.2")
    {:ok, Interval.parse!("P3Y1M3DT2.2S")}

    iex> parse("-1-2 -5:10.1")
    {:ok, Interval.parse!("P-1Y-2MT-5M-10.1S")}

    iex> parse("1.3 cent 100-11 10.3")
    {:ok, Interval.parse!("P230Y11MT10.3S")}

    iex> parse("-   1 year -2 mons +3 days -  04:05:06")
    {:ok, Interval.parse!("P-1Y-2M3DT-4H-5M-6S")}

    iex> parse("0.1 mils 1 cent 1 decade 1 year 1 month 1 week 1 day 1 hour 1 minute 1 second 1000 ms 1000000 us")
    {:ok, Interval.parse!("P211Y1M8DT1H1M3S")}

    iex> parse("1.3 cent 100-11 10.3    1-1")
    {:error, "invalid input syntax at `10.3`"}

    iex> parse("10 10:01  1 10:01:10")
    {:error, "invalid input syntax"}
    iex> parse("1 month 1-10")
    {:error, "invalid input syntax"}
    iex> parse("1-13")
    {:error, "invalid input syntax"}
    iex> parse("1 minute 1 10:01:10")
    {:error, "invalid input syntax"}
    iex> parse("1 second 1 10:01:10")
    {:error, "invalid input syntax"}
    iex> parse("10 10:01 1")
    {:error, "invalid input syntax"}

    iex> parse("")
    {:error, "input string cannot be empty"}

---

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