Skip to content

Commit 3c31aeb

Browse files
author
José Valim
committed
Do not expose @doc false functions in Date
1 parent 8c086e1 commit 3c31aeb

File tree

2 files changed

+60
-48
lines changed

2 files changed

+60
-48
lines changed

lib/elixir/lib/calendar/date.ex

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,28 +57,47 @@ defmodule Date do
5757
day: Calendar.day, calendar: Calendar.calendar}
5858

5959
@doc """
60-
Returns a Date.Range
60+
Returns a range of dates.
61+
62+
A range of dates represents a discrete number of dates where
63+
the first and last values are dates with matching calendars.
64+
65+
Ranges of dates can be either increasing (`first <= last`) or
66+
decreasing (`first > last`). They are also always inclusive.
6167
6268
## Examples
6369
6470
iex> Date.range(~D[2000-01-01], ~D[2001-01-01])
6571
#DateRange<~D[2000-01-01], ~D[2001-01-01]>
6672
73+
A range of dates implements the `Enumerable` protocol, which means
74+
functions in the `Enum` module can be used to work with
75+
ranges:
76+
77+
iex> range = Date.range(~D[2001-01-01], ~D[2002-01-01])
78+
iex> Enum.count(range)
79+
366
80+
iex> Enum.member?(range, ~D[2001-02-01])
81+
true
82+
iex> Enum.reduce(range, 0, fn(_date, acc) -> acc - 1 end)
83+
-366
6784
"""
6885

6986
@spec range(Date.t, Date.t) :: Date.Range.t
7087
def range(%{calendar: calendar} = first, %{calendar: calendar} = last) do
88+
{first_days, _} = to_rata_die(first)
89+
{last_days, _} = to_rata_die(last)
90+
7191
%Date.Range{
7292
first: first,
7393
last: last,
74-
first_rata_die: to_rata_die_days(first),
75-
last_rata_die: to_rata_die_days(last),
94+
first_rata_die: first_days,
95+
last_rata_die: last_days,
7696
}
7797
end
7898

7999
def range(%Date{}, %Date{}) do
80-
raise ArgumentError,
81-
"both dates must have matching calendars"
100+
raise ArgumentError, "both dates must have matching calendars"
82101
end
83102

84103
@doc """
@@ -499,12 +518,6 @@ defmodule Date do
499518
calendar.naive_datetime_to_rata_die(year, month, day, 0, 0, 0, {0, 0})
500519
end
501520

502-
@doc false
503-
def to_rata_die_days(date) do
504-
{days, _} = to_rata_die(date)
505-
days
506-
end
507-
508521
defp from_rata_die({days, _}, Calendar.ISO) do
509522
{year, month, day} = Calendar.ISO.date_from_rata_die_days(days)
510523
%Date{year: year, month: month, day: day, calendar: Calendar.ISO}
@@ -514,11 +527,6 @@ defmodule Date do
514527
%Date{year: year, month: month, day: day, calendar: target_calendar}
515528
end
516529

517-
@doc false
518-
def from_rata_die_days(rata_die, target_calendar) do
519-
from_rata_die({rata_die, {0, 86400000000}}, target_calendar)
520-
end
521-
522530
@doc """
523531
Calculates the day of the week of a given `Date` struct.
524532

lib/elixir/lib/calendar/date_range.ex

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,29 @@
11
defmodule Date.Range do
22
@moduledoc """
3-
Defines a range of dates.
3+
Returns an inclusive range between dates.
44
5-
A range of dates represents a discrete number of dates where
6-
the first and last values are dates with matching calendars.
5+
Ranges must be created with the `Date.range/2` function.
76
8-
Ranges of dates can be either increasing (`first <= last`) or
9-
decreasing (`first > last`). They are also always inclusive.
7+
The following fields are public:
108
11-
A range of dates implements the `Enumerable` protocol, which means
12-
functions in the `Enum` module can be used to work with
13-
ranges:
14-
15-
iex> range = Date.range(~D[2001-01-01], ~D[2002-01-01])
16-
iex> Enum.count(range)
17-
366
18-
iex> Enum.member?(range, ~D[2001-02-01])
19-
true
20-
iex> Enum.reduce(range, 0, fn(_date, acc) -> acc - 1 end)
21-
-366
9+
* `:first` - the initial date on the range
10+
* `:last` - the last date on the range
2211
12+
The remaining fields are private and should not be accessed.
2313
"""
2414

25-
@opaque t :: %__MODULE__{first: Date.t, last: Date.t}
26-
@doc false
15+
@type t :: %__MODULE__{first: Date.t, last: Date.t,
16+
first_rata_die: rata_die_days, last_rata_die: rata_die_days}
17+
18+
@opaque rata_die_days :: Calendar.days
19+
2720
defstruct [:first, :last, :first_rata_die, :last_rata_die]
2821

2922
defimpl Enumerable do
30-
def member?(%Date.Range{first: %{calendar: calendar, year: first_year, month: first_month, day: first_day},
31-
last: %{calendar: calendar, year: last_year, month: last_month, day: last_day},
32-
first_rata_die: first_rata_die, last_rata_die: last_rata_die},
33-
%Date{calendar: calendar, year: year, month: month, day: day}) do
23+
def member?(%{first: %{calendar: calendar, year: first_year, month: first_month, day: first_day},
24+
last: %{calendar: calendar, year: last_year, month: last_month, day: last_day},
25+
first_rata_die: first_rata_die, last_rata_die: last_rata_die},
26+
%Date{calendar: calendar, year: year, month: month, day: day}) do
3427
first = {first_year, first_month, first_day}
3528
last = {last_year, last_month, last_day}
3629
date = {year, month, day}
@@ -50,29 +43,40 @@ defmodule Date.Range do
5043
{:ok, abs(first_rata_die - last_rata_die) + 1}
5144
end
5245

53-
def reduce(%Date.Range{first_rata_die: first_rata_die, last_rata_die: last_rata_die, first: %{calendar: c}}, acc, fun) do
54-
reduce(first_rata_die, last_rata_die, acc, &(fun.(Date.from_rata_die_days(&1, c), &2)), first_rata_die <= last_rata_die)
46+
def reduce(%Date.Range{first_rata_die: first_rata_die, last_rata_die: last_rata_die,
47+
first: %{calendar: calendar}}, acc, fun) do
48+
reduce(first_rata_die, last_rata_die, acc, fun, calendar, first_rata_die <= last_rata_die)
5549
end
5650

57-
defp reduce(_x, _y, {:halt, acc}, _fun, _up?) do
51+
defp reduce(_x, _y, {:halt, acc}, _fun, _calendar, _up?) do
5852
{:halted, acc}
5953
end
6054

61-
defp reduce(x, y, {:suspend, acc}, fun, up?) do
62-
{:suspended, acc, &reduce(x, y, &1, fun, up?)}
55+
defp reduce(x, y, {:suspend, acc}, fun, calendar, up?) do
56+
{:suspended, acc, &reduce(x, y, &1, fun, calendar, up?)}
6357
end
6458

65-
defp reduce(x, y, {:cont, acc}, fun, _up? = true) when x <= y do
66-
reduce(x + 1, y, fun.(x, acc), fun, _up? = true)
59+
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = true) when x <= y do
60+
reduce(x + 1, y, fun.(date_from_rata_days(x, calendar), acc), fun, calendar, up?)
6761
end
6862

69-
defp reduce(x, y, {:cont, acc}, fun, _up? = false) when x >= y do
70-
reduce(x - 1, y, fun.(x, acc), fun, _up? = false)
63+
defp reduce(x, y, {:cont, acc}, fun, calendar, up? = false) when x >= y do
64+
reduce(x - 1, y, fun.(date_from_rata_days(x, calendar), acc), fun, calendar, up?)
7165
end
7266

73-
defp reduce(_, _, {:cont, acc}, _fun, _up) do
67+
defp reduce(_, _, {:cont, acc}, _fun, _calendar, _up) do
7468
{:done, acc}
7569
end
70+
71+
defp date_from_rata_days(days, Calendar.ISO) do
72+
{year, month, day} = Calendar.ISO.date_from_rata_die_days(days)
73+
%Date{year: year, month: month, day: day, calendar: Calendar.ISO}
74+
end
75+
76+
defp date_from_rata_days(days, calendar) do
77+
{year, month, day, _, _, _, _} = calendar.naive_datetime_from_rata_die({days, {0, 86400000000}})
78+
%Date{year: year, month: month, day: day, calendar: calendar}
79+
end
7680
end
7781

7882
defimpl Inspect do

0 commit comments

Comments
 (0)