Skip to content

Commit 00aa2ee

Browse files
Fix Time.add/3 for integer unit (#13125)
1 parent 15e17c1 commit 00aa2ee

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

lib/elixir/lib/calendar/time.ex

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,8 +515,12 @@ defmodule Time do
515515

516516
def add(%{calendar: calendar, microsecond: {_, precision}} = time, amount_to_add, unit)
517517
when is_integer(amount_to_add) do
518-
if (is_integer(unit) and unit < 1) or
519-
unit not in ~w(second millisecond microsecond nanosecond)a do
518+
valid? =
519+
if is_integer(unit),
520+
do: unit > 0,
521+
else: unit in ~w(second millisecond microsecond nanosecond)a
522+
523+
unless valid? do
520524
raise ArgumentError,
521525
"unsupported time unit. Expected :hour, :minute, :second, :millisecond, " <>
522526
":microsecond, :nanosecond, or a positive integer, got #{inspect(unit)}"

lib/elixir/test/elixir/calendar/time_test.exs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ defmodule TimeTest do
9696

9797
assert Time.add(time, 1, :hour) == ~T[01:00:00.0]
9898

99+
assert Time.add(time, 1, 10) == ~T[00:00:00.100000]
100+
99101
assert_raise ArgumentError, ~r/Expected :hour, :minute, :second/, fn ->
100102
Time.add(time, 1, 0)
101103
end

0 commit comments

Comments
 (0)