Skip to content

Commit 9c734ac

Browse files
author
José Valim
committed
last_day_of_month -> days_in_month
1 parent 9388198 commit 9c734ac

File tree

2 files changed

+32
-14
lines changed

2 files changed

+32
-14
lines changed

lib/elixir/lib/calendar.ex

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,9 @@ defmodule Calendar do
6666
time_zone: time_zone, zone_abbr: zone_abbr, utc_offset: utc_offset, std_offset: std_offset}
6767

6868
@doc """
69-
Returns the last day of the month for the given year-month pair.
69+
Returns how many days there are in the given year-month.
7070
"""
71-
@callback last_day_of_month(year, month) :: day
71+
@callback days_in_month(year, month) :: day
7272

7373
@doc """
7474
Returns true if the given year is a leap year.
@@ -175,6 +175,24 @@ defmodule Date do
175175
calendar.leap_year?(year)
176176
end
177177

178+
@doc """
179+
Returns the number of days in the given date month.
180+
181+
## Examples
182+
183+
iex> Date.days_in_month(~D[1900-01-13])
184+
31
185+
iex> Date.days_in_month(~D[1900-02-09])
186+
28
187+
iex> Date.days_in_month(~N[2000-02-20 01:23:45])
188+
29
189+
190+
"""
191+
@spec days_in_month(Calendar.date) :: Calendar.day
192+
def days_in_month(%{calendar: calendar, year: year, month: month}) do
193+
calendar.days_in_month(year, month)
194+
end
195+
178196
@doc """
179197
Builds a new ISO date.
180198

lib/elixir/lib/calendar/iso.ex

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,32 +17,32 @@ defmodule Calendar.ISO do
1717
@type day :: 1..31
1818

1919
@doc """
20-
Returns the last day of the month for the given year.
20+
Returns how many days there are in the given year-month.
2121
2222
## Examples
2323
24-
iex> Calendar.ISO.last_day_of_month(1900, 1)
24+
iex> Calendar.ISO.days_in_month(1900, 1)
2525
31
26-
iex> Calendar.ISO.last_day_of_month(1900, 2)
26+
iex> Calendar.ISO.days_in_month(1900, 2)
2727
28
28-
iex> Calendar.ISO.last_day_of_month(2000, 2)
28+
iex> Calendar.ISO.days_in_month(2000, 2)
2929
29
30-
iex> Calendar.ISO.last_day_of_month(2001, 2)
30+
iex> Calendar.ISO.days_in_month(2001, 2)
3131
28
32-
iex> Calendar.ISO.last_day_of_month(2004, 2)
32+
iex> Calendar.ISO.days_in_month(2004, 2)
3333
29
34-
iex> Calendar.ISO.last_day_of_month(2004, 4)
34+
iex> Calendar.ISO.days_in_month(2004, 4)
3535
30
3636
3737
"""
38-
@spec last_day_of_month(year, month) :: 28..31
39-
def last_day_of_month(year, month)
38+
@spec days_in_month(year, month) :: 28..31
39+
def days_in_month(year, month)
4040

41-
def last_day_of_month(year, 2) do
41+
def days_in_month(year, 2) do
4242
if leap_year?(year), do: 29, else: 28
4343
end
44-
def last_day_of_month(_, month) when month in [4, 6, 9, 11], do: 30
45-
def last_day_of_month(_, month) when month in 1..12, do: 31
44+
def days_in_month(_, month) when month in [4, 6, 9, 11], do: 30
45+
def days_in_month(_, month) when month in 1..12, do: 31
4646

4747
@doc """
4848
Returns if the given year is a leap year.

0 commit comments

Comments
 (0)