Skip to content

Commit 5678a33

Browse files
committed
Fix parsing of fractional durations with 0 in the integer component
The parsing of fractional durations checked for non-negativity by testing second > 0, which reports false for not only negative integers but also for 0. Note that changing `if second > 0` to `if second >= 0` would fix behaviour for "PT0,6S", but would break "PT-0,6S".
1 parent 49b5f71 commit 5678a33

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/elixir/lib/calendar/iso.ex

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -715,7 +715,15 @@ defmodule Calendar.ISO do
715715
{second, <<delimiter, _::binary>> = rest} when delimiter in [?., ?,] ->
716716
case parse_microsecond(rest) do
717717
{{ms, precision}, "S"} ->
718-
ms = if second > 0, do: ms, else: -ms
718+
ms =
719+
case string do
720+
"-" <> _ ->
721+
-ms
722+
723+
_ ->
724+
ms
725+
end
726+
719727
{:ok, [second: second, microsecond: {ms, precision}] ++ acc}
720728

721729
_ ->

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ defmodule DurationTest do
264264
assert Duration.from_iso8601!("PT6S") == %Duration{second: 6}
265265
assert Duration.from_iso8601!("PT1,6S") == %Duration{second: 1, microsecond: {600_000, 1}}
266266
assert Duration.from_iso8601!("PT-1.6S") == %Duration{second: -1, microsecond: {-600_000, 1}}
267-
assert _faulty = Duration.from_iso8601!("PT0,6S") == %Duration{second: 0, microsecond: {-600_000, 1}}
267+
assert Duration.from_iso8601!("PT0,6S") == %Duration{second: 0, microsecond: {600_000, 1}}
268268
assert Duration.from_iso8601!("PT-0,6S") == %Duration{second: 0, microsecond: {-600_000, 1}}
269269
assert Duration.from_iso8601!("-PT-0,6S") == %Duration{second: 0, microsecond: {600_000, 1}}
270270
assert Duration.from_iso8601!("-P10DT4H") == %Duration{day: -10, hour: -4}

0 commit comments

Comments
 (0)