@@ -285,20 +285,23 @@ defmodule Duration do
285
285
end
286
286
287
287
@ doc """
288
- Parses an ISO 8601 formatted duration string to a `Duration` struct.
288
+ Parses an [ ISO 8601](https://en.wikipedia.org/wiki/ISO_8601#Durations) formatted duration string to a `Duration` struct.
289
289
290
- A decimal fraction may be specified for seconds only, using either a comma or a full stop.
290
+ - A duration string must be designated in order of magnitude: `P[n]Y[n]M[n]W[n]DT[n]H[n]M[n]S`.
291
+ - A duration string may be prefixed with a minus sign to negate it: `-P10DT4H`.
292
+ - Individual units may be prefixed with a minus sign: `P-10DT4H`.
293
+ - Only seconds may be specified with a decimal fraction, using either a comma or a full stop: `P1DT4,5S`.
291
294
292
295
## Examples
293
296
294
297
iex> Duration.from_iso8601("P1Y2M3DT4H5M6S")
295
298
{:ok, %Duration{year: 1, month: 2, day: 3, hour: 4, minute: 5, second: 6}}
296
- iex> Duration.from_iso8601("PT10H30M")
297
- {:ok, %Duration{hour: 10, minute: 30, second: 0}}
298
299
iex> Duration.from_iso8601("P3Y-2MT3H")
299
300
{:ok, %Duration{year: 3, month: -2, hour: 3}}
300
- iex> Duration.from_iso8601("P1YT4.650S")
301
- {:ok, %Duration{year: 1, second: 4, microsecond: {650000, 3}}}
301
+ iex> Duration.from_iso8601("-PT10H-30M")
302
+ {:ok, %Duration{hour: -10, minute: 30}}
303
+ iex> Duration.from_iso8601("PT4.650S")
304
+ {:ok, %Duration{second: 4, microsecond: {650000, 3}}}
302
305
303
306
"""
304
307
@ spec from_iso8601 ( String . t ( ) ) :: { :ok , t } | { :error , atom }
@@ -313,12 +316,14 @@ defmodule Duration do
313
316
end
314
317
315
318
@ doc """
316
- Same as `from_iso8601/1` but raises an ArgumentError.
319
+ Same as `from_iso8601/1` but raises an ` ArgumentError` .
317
320
318
321
## Examples
319
322
320
323
iex> Duration.from_iso8601!("P1Y2M3DT4H5M6S")
321
324
%Duration{year: 1, month: 2, day: 3, hour: 4, minute: 5, second: 6}
325
+ iex> Duration.from_iso8601!("P10D")
326
+ %Duration{day: 10}
322
327
323
328
"""
324
329
@ spec from_iso8601! ( String . t ( ) ) :: t
0 commit comments