extractWeeks static method

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

Implementation

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

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

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

    while (lastDayOfWeek.isBefore(weekMaxDate)) {
      var 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;
  }
}