toJalali static method

dynamic toJalali(
  1. int y,
  2. int m,
  3. int d, {
  4. bool twoDigits = false,
})

Implementation

static toJalali(int y, int m, int d, {bool twoDigits = false}) {
  const sumMonthDay = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334];
  int jY = y > 1600 ? 979 : 0;
  y -= jY > 0 ? 1600 : 621;
  var gy = (m > 2) ? y + 1 : y;
  var day = (365 * y) +
      ((gy + 3) ~/ 4) -
      ((gy + 99) ~/ 100) +
      ((gy + 399) ~/ 400) -
      80 +
      d +
      sumMonthDay[m - 1];
  jY += 33 * (day.round() / 12053).floor();
  day %= 12053;
  jY += 4 * (day.round() / 1461).floor();
  day %= 1461;
  jY += ((day.round() - 1) / 365).floor();
  if (day > 365) day = ((day - 1).round() % 365);
  int jm;
  int jd;
  int days = day.toInt();
  if (days < 186) {
    jm = 1 + (days ~/ 31);
    jd = 1 + (days % 31);
  } else {
    jm = 7 + ((days - 186) ~/ 30);
    jd = 1 + (days - 186) % 30;
  }
  int month =
  twoDigits ? int.parse(jm.toString().padLeft(2, '0')) : jm;
  int jDay =
  twoDigits ? int.parse(jd.toString().padLeft(2, '0')) : jd;
  return DateTime(jY,month, jDay);
}