View Source Timex.Parse.Duration.Parsers.ISO8601Parser (timex v3.7.11)

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

Link to this section Summary

Functions

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

Link to this section Functions

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

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

examples

Examples

iex> {:ok, d} = Elixir.Timex.Parse.Duration.Parsers.ISO8601Parser.parse("P15Y3M2DT1H14M37.25S")
...> Timex.Format.Duration.Formatter.format(d)
"P15Y3M2DT1H14M37.25S"

iex> {:ok, d} = Elixir.Timex.Parse.Duration.Parsers.ISO8601Parser.parse("P15Y3M2D")
...> Timex.Format.Duration.Formatter.format(d)
"P15Y3M2D"

iex> {:ok, d} = Elixir.Timex.Parse.Duration.Parsers.ISO8601Parser.parse("PT3H12M25.001S")
...> Timex.Format.Duration.Formatter.format(d)
"PT3H12M25.001S"

iex> {:ok, d} = Elixir.Timex.Parse.Duration.Parsers.ISO8601Parser.parse("P2W")
...> Timex.Format.Duration.Formatter.format(d)
"P14D"

iex> Elixir.Timex.Parse.Duration.Parsers.ISO8601Parser.parse("P15YT3D")
{:error, "invalid use of date component after time separator"}