Skip to content

Commit c246400

Browse files
authored
Update Calendar.day_of_week callback and Date.day_of_week/2 docs (#14176)
1 parent 04a662a commit c246400

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

lib/elixir/lib/calendar.ex

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,15 @@ defmodule Calendar do
172172
`starting_on` represents the starting day of the week. All
173173
calendars must support at least the `:default` value. They may
174174
also support other values representing their days of the week.
175+
176+
The value of `day_of_week` is an ordinal number meaning that a
177+
value of `1` is defined to mean "first day of the week". It is
178+
specifically not defined to mean `1` is `Monday`.
179+
180+
It is a requirement that `first_day_of_week` is less than `last_day_of_week`
181+
and that `day_of_week` must be within that range. Therefore it can be said
182+
that `day_of_week in first_day_of_week..last_day_of_week//1` must be
183+
`true` for all values of `day_of_week`.
175184
"""
176185
@callback day_of_week(year, month, day, starting_on :: :default | atom) ::
177186
{day_of_week(), first_day_of_week :: non_neg_integer(),

lib/elixir/lib/calendar/date.ex

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ defmodule Date do
852852
end
853853

854854
@doc """
855-
Calculates the day of the week of a given `date`.
855+
Calculates the ordinal day of the week of a given `date`.
856856
857857
Returns the day of the week as an integer. For the ISO 8601
858858
calendar (the default), it is an integer from 1 to 7, where
@@ -861,10 +861,19 @@ defmodule Date do
861861
An optional `starting_on` value may be supplied, which
862862
configures the weekday the week starts on. The default value
863863
for it is `:default`, which translates to `:monday` for the
864-
built-in ISO calendar. Any other weekday may be given to.
864+
built-in ISO 8601 calendar. Any other weekday may be used for
865+
`starting_on`, in such cases, that weekday will be considered the first
866+
day of the week, and therefore it will be assigned the ordinal number 1.
867+
868+
The other calendars, the value returned is an ordinal day of week.
869+
For example, `1` may mean "first day of the week" and `7` is
870+
defined to mean "seventh day of the week". Custom calendars may
871+
also accept their own variations of the `starting_on` parameter
872+
with their own meaning.
865873
866874
## Examples
867875
876+
# 2016-10-31 is a Monday and by default Monday is the first day of the week
868877
iex> Date.day_of_week(~D[2016-10-31])
869878
1
870879
iex> Date.day_of_week(~D[2016-11-01])
@@ -874,6 +883,7 @@ defmodule Date do
874883
iex> Date.day_of_week(~D[-0015-10-30])
875884
3
876885
886+
# 2016-10-31 is a Monday but, as we start the week on Sunday, now it returns 2
877887
iex> Date.day_of_week(~D[2016-10-31], :sunday)
878888
2
879889
iex> Date.day_of_week(~D[2016-11-01], :sunday)

0 commit comments

Comments
 (0)