extractWeeks static method

List<Month> extractWeeks(
  1. DateTime minDate,
  2. DateTime maxDate
)

Implementation

static List<Month> extractWeeks(DateTime minDate, DateTime maxDate) {
  DateTime weekMinDate = _findDayOfWeekInMonth(minDate, DateTime.monday);
  DateTime weekMaxDate = _findDayOfWeekInMonth(maxDate, DateTime.sunday);

  DateTime firstDayOfWeek = weekMinDate;
  DateTime lastDayOfWeek = _lastDayOfWeek(weekMinDate);

  if (!lastDayOfWeek.isBefore(weekMaxDate)) {
    return <Month>[
      Month(<Week>[Week(firstDayOfWeek, lastDayOfWeek)])
    ];
  } else {
    List<Month> months = [];
    List<Week> weeks = [];

    while (lastDayOfWeek.isBefore(weekMaxDate)) {
      Week week = Week(firstDayOfWeek, lastDayOfWeek);
      weeks.add(week);

      if (week.isLastWeekOfMonth) {
        if (lastDayOfWeek.isSameDayOrAfter(minDate)) {
          months.add(Month(weeks));
        }

        weeks = [];

        firstDayOfWeek = firstDayOfWeek.toFirstDayOfNextMonth();
        lastDayOfWeek = _lastDayOfWeek(firstDayOfWeek);

        weeks.add(Week(firstDayOfWeek, lastDayOfWeek));
      }

      firstDayOfWeek = lastDayOfWeek.nextDay;
      lastDayOfWeek = _lastDayOfWeek(firstDayOfWeek);
    }

    if (!lastDayOfWeek.isBefore(weekMaxDate)) {
      weeks.add(Week(firstDayOfWeek, lastDayOfWeek));
    }

    months.add(Month(weeks));

    return months;
  }
}