Skip to content

Commit 78d6b88

Browse files
simonprevjosevalim
authored andcommitted
Add complete list of time units on DateTime.add/3 argument error (#12152)
1 parent 7f494e6 commit 78d6b88

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

lib/elixir/lib/calendar/datetime.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1529,6 +1529,12 @@ defmodule DateTime do
15291529
microsecond: {_, precision}
15301530
} = datetime
15311531

1532+
if not is_integer(unit) and
1533+
unit not in ~w(second millisecond microsecond nanosecond)a do
1534+
raise ArgumentError,
1535+
"unsupported time unit. Expected :day, :hour, :minute, :second, :millisecond, :microsecond, :nanosecond, or a positive integer, got #{inspect(unit)}"
1536+
end
1537+
15321538
ppd = System.convert_time_unit(86400, :second, unit)
15331539
total_offset = System.convert_time_unit(utc_offset + std_offset, :second, unit)
15341540

lib/elixir/test/elixir/calendar/datetime_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -958,6 +958,15 @@ defmodule DateTimeTest do
958958
end
959959

960960
describe "add" do
961+
test "add with invalid time unit" do
962+
dt = DateTime.utc_now()
963+
964+
message =
965+
~r/unsupported time unit\. Expected :day, :hour, :minute, :second, :millisecond, :microsecond, :nanosecond, or a positive integer, got "day"/
966+
967+
assert_raise ArgumentError, message, fn -> DateTime.add(dt, 1, "day") end
968+
end
969+
961970
test "add with non-struct map that conforms to Calendar.datetime" do
962971
dt_map = DateTime.from_naive!(~N[2018-08-28 00:00:00], "Etc/UTC") |> Map.from_struct()
963972

0 commit comments

Comments
 (0)