fromCalendarWeekRule static method

WeekYearRule fromCalendarWeekRule(
  1. CalendarWeekRule calendarWeekRule,
  2. DayOfWeek firstDayOfWeek
)

Creates a rule which behaves the same way as the BCL Calendar.getWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) method.

The BCL week year rules are subtly different to the ISO rules. In particular, the last few days of the calendar year are always part of the same week-year in the BCL rules, whereas in the ISO rules they can fall into the next week-year. (The first few days of the calendar year can be part of the previous week-year in both kinds of rule.) This means that in the BCL rules, some weeks are incomplete, whereas ISO weeks are always exactly 7 days long.

  • calendarWeekRule: The BCL rule to emulate.
  • firstDayOfWeek: The first day of the week to use in the rule. A rule which behaves the same way as the BCL Calendar.GetWeekOfYear(DateTime, CalendarWeekRule, DayOfWeek) method.

Implementation

static WeekYearRule fromCalendarWeekRule(CalendarWeekRule calendarWeekRule, DayOfWeek firstDayOfWeek) {
  int minDaysInFirstWeek;
  switch (calendarWeekRule) {
    case CalendarWeekRule.firstDay:
      minDaysInFirstWeek = 1;
      break;
    case CalendarWeekRule.firstFourDayWeek:
      minDaysInFirstWeek = 4;
      break;
    case CalendarWeekRule.firstFullWeek:
      minDaysInFirstWeek = 7;
      break;
    default:
      throw ArgumentError('Unsupported CalendarWeekRule: $calendarWeekRule');
  }
  return SimpleWeekYearRule(minDaysInFirstWeek, firstDayOfWeek, true);
}