Skip to content

Commit a31549c

Browse files
committed
Ensure Calendar.ISO enforces date limits
1 parent be6e54d commit a31549c

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

lib/elixir/lib/calendar/iso.ex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,13 @@ defmodule Calendar.ISO do
122122
def date_to_iso_days_days(1970, 1, 1) do
123123
719528
124124
end
125-
def date_to_iso_days_days(year, month, day) do
125+
def date_to_iso_days_days(year, month, day) when year <= 9999 do
126126
:calendar.date_to_gregorian_days(year, month, day)
127127
end
128128

129129
# Converts count of days since 0000-01-01 to {year, month, day} tuple.
130130
@doc false
131-
def date_from_iso_days_days(days) do
131+
def date_from_iso_days_days(days) when days <= 3652424 do
132132
:calendar.gregorian_days_to_date(days)
133133
end
134134

lib/elixir/test/elixir/calendar_test.exs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,15 @@ defmodule DateTest do
6666
{:ok, Calendar.Holocene.date(12000, 01, 01)}
6767
end
6868

69+
test "add/2" do
70+
assert_raise FunctionClauseError, fn ->
71+
Date.add(~D[0000-01-01], 3652425)
72+
end
73+
assert_raise FunctionClauseError, fn ->
74+
Date.add(~D[0000-01-01], -1)
75+
end
76+
end
77+
6978
test "diff/2" do
7079
assert Date.diff(~D[2000-01-31], ~D[2000-01-01]) == 30
7180
assert Date.diff(~D[2000-01-01], ~D[2000-01-31]) == -30

0 commit comments

Comments
 (0)