Skip to content

Commit 1254dae

Browse files
committed
Avoid checking date validity twice
1 parent 651bdb4 commit 1254dae

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

lib/elixir/lib/calendar/iso.ex

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ defmodule Calendar.ISO do
671671
{{year, month, day}, time_from_day_fraction(day_fraction)}
672672

673673
{extra_days, day_fraction} ->
674-
base_days = date_to_iso_days(year, month, day)
674+
base_days = valid_date_to_iso_days(year, month, day)
675675
{date_from_iso_days(base_days + extra_days), time_from_day_fraction(day_fraction)}
676676
end
677677

@@ -892,12 +892,15 @@ defmodule Calendar.ISO do
892892

893893
# Converts year, month, day to count of days since 0000-01-01.
894894
@doc false
895-
def date_to_iso_days(0, 1, 1), do: 0
896-
def date_to_iso_days(1970, 1, 1), do: @unix_epoch_days
897-
898895
def date_to_iso_days(year, month, day) do
899896
ensure_day_in_month!(year, month, day)
897+
valid_date_to_iso_days(year, month, day)
898+
end
899+
900+
defp valid_date_to_iso_days(0, 1, 1), do: 0
901+
defp valid_date_to_iso_days(1970, 1, 1), do: @unix_epoch_days
900902

903+
defp valid_date_to_iso_days(year, month, day) do
901904
y = if month <= 2, do: year - 1, else: year
902905
era = if y >= 0, do: div(y, @years_per_era), else: div(y - 399, @years_per_era)
903906
year_of_era = y - era * @years_per_era
@@ -1875,7 +1878,7 @@ defmodule Calendar.ISO do
18751878
@doc false
18761879
def shift_days({year, month, day}, days) do
18771880
{year, month, day} =
1878-
date_to_iso_days(year, month, day)
1881+
valid_date_to_iso_days(year, month, day)
18791882
|> Kernel.+(days)
18801883
|> date_from_iso_days()
18811884

0 commit comments

Comments
 (0)