Skip to content

Commit a324c88

Browse files
authored
Fix Duration.from_iso8601/1 accepting duplicate S (secs) when fractional
1 parent f401038 commit a324c88

2 files changed

Lines changed: 13 additions & 12 deletions

File tree

lib/elixir/lib/calendar/iso.ex

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -731,19 +731,19 @@ defmodule Calendar.ISO do
731731
defp parse_duration_time(string, acc, allowed) do
732732
case Integer.parse(string) do
733733
{second, <<delimiter, _::binary>> = rest} when delimiter in [?., ?,] ->
734-
case parse_microsecond(rest) do
735-
{{ms, precision}, "S"} ->
736-
ms =
737-
case string do
738-
"-" <> _ ->
739-
-ms
740-
741-
_ ->
742-
ms
743-
end
744-
745-
{:ok, [second: second, microsecond: {ms, precision}] ++ acc}
734+
with {:second, _allowed} <- find_unit(allowed, ?S),
735+
{{ms, precision}, "S"} <- parse_microsecond(rest) do
736+
ms =
737+
case string do
738+
"-" <> _ ->
739+
-ms
740+
741+
_ ->
742+
ms
743+
end
746744

745+
{:ok, [second: second, microsecond: {ms, precision}] ++ acc}
746+
else
747747
_ ->
748748
{:error, :invalid_time_component}
749749
end

lib/elixir/test/elixir/calendar/duration_test.exs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -242,6 +242,7 @@ defmodule DurationTest do
242242
assert Duration.from_iso8601("P5H3HT4M") == {:error, :invalid_date_component}
243243
assert Duration.from_iso8601("P0.5Y") == {:error, :invalid_date_component}
244244
assert Duration.from_iso8601("PT1D") == {:error, :invalid_time_component}
245+
assert Duration.from_iso8601("PT1S2.5S") == {:error, :invalid_time_component}
245246
assert Duration.from_iso8601("PT.6S") == {:error, :invalid_time_component}
246247
assert Duration.from_iso8601("PT0.5H") == {:error, :invalid_time_component}
247248
assert Duration.from_iso8601("invalid") == {:error, :invalid_duration}

0 commit comments

Comments
 (0)