convertToHijriDate function

HijriDateTime convertToHijriDate(
  1. DateTime date
)

Converts and retusn the hijri date for the given gregorian date.

Implementation

HijriDateTime convertToHijriDate(DateTime date) {
  int day = date.day;
  int month = date.month;
  int year = date.year;
  int tMonth = month;
  int tYear = year;
  if (tMonth < 3) {
    tYear -= 1;
    tMonth += 12;
  }
  int yPrefix = (tYear / 100).floor();
  int julianOffset = yPrefix - (yPrefix / 4.0).floor() - 2;
  final int julianNumber = (365.25 * (tYear + 4716)).floor() +
      (30.6001 * (tMonth + 1)).floor() +
      day -
      julianOffset -
      1524;
  yPrefix = ((julianNumber - 1867216.25) / 36524.25).floor();
  julianOffset = yPrefix - (yPrefix / 4.0).floor() + 1;
  final int b = julianNumber + julianOffset + 1524;
  int c = ((b - 122.1) / 365.25).floor();
  final int d = (365.25 * c).floor();
  final int tempMonth = ((b - d) / 30.6001).floor();
  day = (b - d) - (30.6001 * tempMonth).floor();
  month = ((b - d) / 20.6001).floor();
  if (month > 13) {
    c += 1;
    month -= 12;
  }
  month -= 1;
  year = c - 4716;
  final int modifiedJulianDate = julianNumber - 2400000;
  // date calculation for year after 2077
  const double iYear = 10631.0 / 30.0;
  int z = julianNumber - 1948084;
  final int cyc = (z / 10631.0).floor();
  z = z - 10631 * cyc;
  final int j = ((z - 0.1335) / iYear).floor();
  final int iy = 30 * cyc + j;
  z = z - (j * iYear + 0.1335).floor();
  int im = ((z + 28.5001) / 29.5).floor();
  /* istanbul ignore next */
  if (im == 13) {
    im = 12;
  }

  final int tempDay = z - (29.5001 * im - 29).floor();
  int i = 0;
  for (; i < _kDateCollection.length; i++) {
    if (_kDateCollection[i] > modifiedJulianDate) {
      break;
    }
  }
  final int iln = i + 16260;
  final int ii = ((iln - 1) / 12).floor();
  int hYear = ii + 1;
  int hMonth = iln - 12 * ii;
  int hDate = modifiedJulianDate - _kDateCollection[i - 1] + 1;
  if ('$hDate'.length > 2) {
    hDate = tempDay;
    hMonth = im;
    hYear = iy;
  }

  return HijriDateTime(hYear, hMonth, hDate);
}