Skip to content

Commit 24ff3df

Browse files
author
José Valim
committed
Remove week_of_year
We are uncertain this is the best API to expose this information. Maybe it is best to expose this as a separate calendar. That uses the week-based year and week-based month in the Date/Time structs.
1 parent fb97713 commit 24ff3df

File tree

5 files changed

+2
-163
lines changed

5 files changed

+2
-163
lines changed

CHANGELOG.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@
1111
#### Elixir
1212

1313
* [Calendar] Add `Calendar.TimeZoneDatabase` and a `Calendar.UTCOnlyTimeZoneDatabase` implementation
14-
* [Calendar] Add callbacks `day_of_year/3`, `quarter_of_year/3`, `year_of_era/1`, `day_of_era/3` and `week_of_year/3`
14+
* [Calendar] Add callbacks `day_of_year/3`, `quarter_of_year/3`, `year_of_era/1`, and `day_of_era/3`
1515
* [Code.Formatter] Preserve user's choice of new line after most operators
16-
* [Date] Add `Date.day_of_year/1`, `Date.quarter_of_year/1`, `Date.year_of_era/1`, `Date.day_of_era/1` and `Date.week_of_year/1`
16+
* [Date] Add `Date.day_of_year/1`, `Date.quarter_of_year/1`, `Date.year_of_era/1`, and `Date.day_of_era/1`
1717
* [DateTime] Add `DateTime.from_naive/3`, `DateTime.now/1` and `DateTime.shift_zone/3`
1818
* [File] Allow `:raw` option in `File.exists?/2`, `File.regular?/2` and `File.dir?/2`
1919
* [File] Allow POSIX time as an integer in `File.touch/2` and `File.touch!/2`

lib/elixir/lib/calendar.ex

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -157,11 +157,6 @@ defmodule Calendar do
157157
"""
158158
@callback day_of_week(year, month, day) :: day_of_week()
159159

160-
@doc """
161-
Calculates the week of year from the given `year`, `month`, and `day`.
162-
"""
163-
@callback week_of_year(year, month, day) :: {year(), week(), day_of_week()}
164-
165160
@doc """
166161
Calculates the day of the year from the given `year`, `month`, and `day`.
167162
"""

lib/elixir/lib/calendar/date.ex

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -659,42 +659,6 @@ defmodule Date do
659659
calendar.day_of_week(year, month, day)
660660
end
661661

662-
@doc """
663-
Calculates the week of year of a given `date`.
664-
665-
It returns a tuple with the year, the week, and the day of the week.
666-
For the ISO 8601 calendar (the default), the first week of the
667-
year is the first one that has a Thursday.
668-
669-
## Examples
670-
671-
iex> Date.week_of_year(~D[2016-01-01])
672-
{2015, 53, 5}
673-
iex> Date.week_of_year(~D[2016-01-04])
674-
{2016, 1, 1}
675-
676-
iex> Date.week_of_year(~D[2018-12-31])
677-
{2019, 1, 1}
678-
iex> Date.week_of_year(~D[2019-01-01])
679-
{2019, 1, 2}
680-
681-
iex> Date.week_of_year(~D[2016-11-01])
682-
{2016, 44, 2}
683-
iex> Date.week_of_year(~D[-0015-10-30])
684-
{-15, 44, 3}
685-
iex> Date.week_of_year(~D[2004-12-31])
686-
{2004, 53, 5}
687-
688-
"""
689-
@doc since: "1.8.0"
690-
@spec week_of_year(Calendar.date()) ::
691-
{Calendar.year(), Calendar.week(), Calendar.day_of_week()}
692-
def week_of_year(date)
693-
694-
def week_of_year(%{calendar: calendar, year: year, month: month, day: day}) do
695-
calendar.week_of_year(year, month, day)
696-
end
697-
698662
@doc """
699663
Calculates the day of the year of a given `date`.
700664

lib/elixir/lib/calendar/iso.ex

Lines changed: 0 additions & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -346,123 +346,6 @@ defmodule Calendar.ISO do
346346
Integer.mod(iso_days + 5, 7) + 1
347347
end
348348

349-
@doc """
350-
Returns the week of a given date according to ISO.
351-
352-
## Examples
353-
354-
# First day of the year is a Monday
355-
iex> Calendar.ISO.week_of_year(2017, 12, 31)
356-
{2017, 52, 7}
357-
iex> Calendar.ISO.week_of_year(2018, 1, 1)
358-
{2018, 1, 1}
359-
iex> Calendar.ISO.week_of_year(2018, 1, 2)
360-
{2018, 1, 2}
361-
362-
# First day of the year is a Tuesday
363-
iex> Calendar.ISO.week_of_year(2018, 12, 31)
364-
{2019, 1, 1}
365-
iex> Calendar.ISO.week_of_year(2019, 1, 1)
366-
{2019, 1, 2}
367-
iex> Calendar.ISO.week_of_year(2019, 1, 2)
368-
{2019, 1, 3}
369-
370-
# First day of the year is a Wednesday
371-
iex> Calendar.ISO.week_of_year(2013, 12, 29)
372-
{2013, 52, 7}
373-
iex> Calendar.ISO.week_of_year(2013, 12, 30)
374-
{2014, 1, 1}
375-
iex> Calendar.ISO.week_of_year(2013, 12, 31)
376-
{2014, 1, 2}
377-
iex> Calendar.ISO.week_of_year(2014, 1, 1)
378-
{2014, 1, 3}
379-
iex> Calendar.ISO.week_of_year(2014, 1, 2)
380-
{2014, 1, 4}
381-
382-
# First day of the year is a Thursday
383-
iex> Calendar.ISO.week_of_year(2014, 12, 28)
384-
{2014, 52, 7}
385-
iex> Calendar.ISO.week_of_year(2014, 12, 29)
386-
{2015, 1, 1}
387-
iex> Calendar.ISO.week_of_year(2014, 12, 30)
388-
{2015, 1, 2}
389-
iex> Calendar.ISO.week_of_year(2014, 12, 31)
390-
{2015, 1, 3}
391-
iex> Calendar.ISO.week_of_year(2015, 1, 1)
392-
{2015, 1, 4}
393-
iex> Calendar.ISO.week_of_year(2015, 1, 2)
394-
{2015, 1, 5}
395-
396-
# First day of the year is a Friday
397-
iex> Calendar.ISO.week_of_year(2016, 1, 1)
398-
{2015, 53, 5}
399-
iex> Calendar.ISO.week_of_year(2016, 1, 2)
400-
{2015, 53, 6}
401-
iex> Calendar.ISO.week_of_year(2016, 1, 3)
402-
{2015, 53, 7}
403-
iex> Calendar.ISO.week_of_year(2016, 1, 4)
404-
{2016, 1, 1}
405-
406-
# First day of the year is a Saturday
407-
iex> Calendar.ISO.week_of_year(2011, 1, 1)
408-
{2010, 52, 6}
409-
iex> Calendar.ISO.week_of_year(2011, 1, 2)
410-
{2010, 52, 7}
411-
iex> Calendar.ISO.week_of_year(2011, 1, 3)
412-
{2011, 1, 1}
413-
414-
# First day of the year is a Sunday
415-
iex> Calendar.ISO.week_of_year(2017, 1, 1)
416-
{2016, 52, 7}
417-
iex> Calendar.ISO.week_of_year(2017, 1, 2)
418-
{2017, 1, 1}
419-
420-
Any other date:
421-
422-
iex> Calendar.ISO.week_of_year(2018, 12, 4)
423-
{2018, 49, 2}
424-
425-
"""
426-
@doc since: "1.8.0"
427-
@spec week_of_year(year, month, day) :: {year, 1..53, 1..7}
428-
@impl true
429-
def week_of_year(year, month, day) do
430-
true = day <= days_in_month(year, month)
431-
{first_day_of_year, first_week_day_of_year} = first_day_and_week_day_of_year_in_iso_days(year)
432-
433-
iso_days =
434-
first_day_of_year + days_before_month(month) + leap_day_offset(year, month) + day - 1
435-
436-
day_of_week = iso_days_to_day_of_week(iso_days)
437-
438-
cond do
439-
# If we are in a monday, tuesday or wednesday where the thursday
440-
# is in the next year, so we are already in the next year week.
441-
month == 12 and day >= 29 and day_of_week < 4 ->
442-
{year + 1, 1, day_of_week}
443-
444-
# If we are in a friday, saturday, sunday as one of the first
445-
# three days in the year, so we are still in the previous year.
446-
iso_days < first_week_day_of_year ->
447-
{_, first_week_day_of_year} = first_day_and_week_day_of_year_in_iso_days(year - 1)
448-
{year - 1, div(iso_days - first_week_day_of_year, 7) + 1, day_of_week}
449-
450-
true ->
451-
{year, div(iso_days - first_week_day_of_year, 7) + 1, day_of_week}
452-
end
453-
end
454-
455-
defp first_day_and_week_day_of_year_in_iso_days(year) do
456-
first_day_of_year = days_in_previous_years(year)
457-
day_of_week = iso_days_to_day_of_week(first_day_of_year)
458-
459-
if day_of_week > 4 do
460-
{first_day_of_year, first_day_of_year + 7 - day_of_week + 1}
461-
else
462-
{first_day_of_year, first_day_of_year - day_of_week + 1}
463-
end
464-
end
465-
466349
@doc """
467350
Calculates the day of the year from the given `year`, `month`, and `day`.
468351

lib/elixir/test/elixir/calendar/holocene.exs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,6 @@ defmodule Calendar.Holocene do
9696
@impl true
9797
defdelegate day_of_week(year, month, day), to: Calendar.ISO
9898

99-
@impl true
100-
defdelegate week_of_year(year, month, day), to: Calendar.ISO
101-
10299
@impl true
103100
defdelegate day_of_year(year, month, day), to: Calendar.ISO
104101

0 commit comments

Comments
 (0)