jalaliToGregorian static method

dynamic jalaliToGregorian(
  1. int y,
  2. int m,
  3. int d
)

Implementation

static jalaliToGregorian(int y, int m, int d) {
  int gY;
  if (y > 979) {
    gY = 1600;
    y -= 979;
  } else {
    gY = 621;
  }

  var days = (365 * y) +
      ((y / 33).floor() * 8) +
      (((y % 33) + 3) / 4) +
      78 +
      d +
      (((m < 7) ? (m - 1) * 31 : (((m - 7) * 30) + 186)));
  gY += 400 * (days ~/ 146097);
  days %= 146097;
  if (days.floor() > 36524) {
    gY += 100 * (--days ~/ 36524);
    days %= 36524;
    if (days >= 365) days++;
  }
  gY += 4 * (days ~/ 1461);
  days %= 1461;
  gY += (days - 1) ~/ 365;

  if (days > 365) days = (days - 1) % 365;
  var gD = (days + 1).floor();
  var montDays = [
    0,
    31,
    (((gY % 4 == 0) && (gY % 100 != 0)) || (gY % 400 == 0)) ? 29 : 28,
    31,
    30,
    31,
    30,
    31,
    31,
    30,
    31,
    30,
    31
  ];
  int i = 0;
  for (; i <= 12; i++) {
    if (gD <= montDays[i]) break;
    gD -= montDays[i];
  }
  return DateTime(gY,i,gD);
}