@@ -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
0 commit comments