weekNumber method

int weekNumber()

Returns the ISO week number, handling year boundaries correctly.

If the week number is less than 1, returns the last week of the previous year. If the week number exceeds the number of weeks in the year, returns 1.

Implementation

int weekNumber() {
  if (weekOfYear < 1) return numOfWeeks(year - 1);
  if (weekOfYear > numOfWeeks(year)) return 1;
  return weekOfYear;
}