Jalali constructor

Jalali(
  1. int year,
  2. [int month = 1,
  3. int day = 1]
)

Create a Jalali date by using year, month and day year and month default to 1 year, month and day can not be null

throws on null arguments

non-null

Implementation

Jalali(int year, [int month = 1, int day = 1])
    : year = year,
      month = month,
      day = day {
  ArgumentError.checkNotNull(year, 'year');
  ArgumentError.checkNotNull(month, 'month');
  ArgumentError.checkNotNull(day, 'day');

  // should be between: Jalali(-61, 1, 1) and Jalali(3177, 10, 11)
  if (year < -61 || year > 3177) {
    throw DateException('Jalali date is out of computable range.');
  }

  if (month < 1 || month > 12) {
    throw DateException('Jalali month is out of valid range.');
  }

  // monthLength is very cheap
  // but isLeapYear is not cheap
  // if month is 12, monthLength will use isLeapYear
  // month 12 will always have 29 days or 30 days
  // so if we are at 30 of month 12 we should use isLeapYear to check validity
  // and it is more than 30 we should throw immediately
  // but if is less than 30 it is always ok if it is more than 0
  if (month != 12 || day == 30) {
    // month != 12 || (month == 12 && day == 30)
    final ml = monthLength;

    if (day < 1 || day > ml) {
      throw DateException('Jalali day is out of valid range.');
    }
  } else {
    // month == 12 && day != 30
    // from 1 to 29 is valid
    // 30 has been handled
    // more than 30 or less than 1 is invalid
    if (day < 1 || day > 30) {
      throw DateException('Jalali day is out of valid range.');
    }
  }

  // no need for further analysis for MIN, but for MAX being in year 3177:
  if (year == 3177) {
    if (month > 10 || (month == 10 && day > 11)) {
      throw DateException('Jalali date is out of computable range.');
    }
  }
}