convertToGregorianDate function Null safety

DateTime convertToGregorianDate(
  1. HijriDateTime? date,
  2. {int year = 0,
  3. int month = 0,
  4. int day = 0}
)

Converts and returns the gregorian date from the given hijri date values.

Implementation

DateTime convertToGregorianDate(HijriDateTime? date,
    {int year = 0, int month = 0, int day = 0}) {
  if (date != null) {
    return date._date;
  }

  assert(year != 0);
  assert(month != 0);
  assert(day != 0);

  /// When the year exceeds the maximum year limit return the maximum value.
  if (year > 1500) {
    return DateTime(2077, 11, 16);
  } else if (year < 1356) {
    /// Return minimum hijri date equivalent gregorian date value when
    /// hijri year value before the minimum year value.
    return DateTime(1937, 03, 14);
  }

  final int iy = year;
  final int im = month;
  final int id = day;
  final int ii = iy - 1;
  final int iln = (ii * 12) + 1 + (im - 1);
  final int i = iln - 16260;
  final int mcjdn = id + _kDateCollection[i - 1] - 1;
  final int julianDate = mcjdn + 2400000;
  final int z = (julianDate + 0.5).floor();
  int a = ((z - 1867216.25) / 36524.25).floor();
  a = z + 1 + a - (a / 4).floor();
  final int b = a + 1524;
  final int c = ((b - 122.1) / 365.25).floor();
  final int d = (365.25 * c).floor();
  final int e = ((b - d) / 30.6001).floor();
  final int gDay = b - d - (e * 30.6001).floor();
  int gMonth = e - (e > 13.5 ? 13 : 1);
  final int gYear = c - (gMonth > 2.5 ? 4716 : 4715);
  /* istanbul ignore next */
  if (gYear <= 0) {
    gMonth--;
  } // No year zero
  return DateTime(gYear, gMonth, gDay);
}