Jalali.fromJulianDayNumber constructor

Jalali.fromJulianDayNumber(
  1. int julianDayNumber
)

Converts the Julian Day number to a date in the Jalali calendar.

throws on null argument

non-null

Implementation

factory Jalali.fromJulianDayNumber(int julianDayNumber) {
  ArgumentError.checkNotNull(julianDayNumber, 'julianDayNumber');

  if (julianDayNumber < 1925675 || julianDayNumber > 3108616) {
    throw DateException('Julian day number is out of computable range.');
  }

  // Calculate Gregorian year (gy).
  int gy = Gregorian.fromJulianDayNumber(julianDayNumber).year;
  int jy = gy - 621;
  final r = _JalaliCalculation.calculate(jy);
  int jdn1f = Gregorian(gy, 3, r.march!).julianDayNumber;
  int jd, jm, k;

  // Find number of days that passed since 1 Farvardin.
  k = julianDayNumber - jdn1f;
  if (k >= 0) {
    if (k <= 185) {
      // The first 6 months.
      jm = 1 + (k ~/ 31);
      jd = (k % 31) + 1;

      return Jalali(jy, jm, jd);
    } else {
      // The remaining months.
      k -= 186;
    }
  } else {
    // Previous Jalali year.
    jy -= 1;
    k += 179;
    if (r.leap == 1) k += 1;
  }
  jm = 7 + (k ~/ 30);
  jd = (k % 30) + 1;

  return Jalali(jy, jm, jd);
}