converts JDN to Gregorian

@param  {Number} jdn
@param  {Number} JD_OFFSET
@param  {Function} leapYear
@return {Number}
964796
function jdnToGregorian(jdn, JD_OFFSET = JD_EPOCH_OFFSET_GREGORIAN, leapYear = isGregorianLeap) {
  const nMonths = 12;
  const monthDays = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];

  const r2000 = mod((jdn - JD_OFFSET), 730485);
  const r400 = mod((jdn - JD_OFFSET), 146097);
  const r100 = mod(r400, 36524);
  const r4 = mod(r100, 1461);

  let n = mod(r4, 365) + 365 * Math.floor(r4 / 1460);
  const s = Math.floor(r4 / 1095);

  const aprime = 400 * Math.floor((jdn - JD_OFFSET) / 146097)
               + 100 * Math.floor(r400 / 36524)
               + 4 * Math.floor(r100 / 1461)
               + Math.floor(r4 / 365)
               - Math.floor(r4 / 1460)
               - Math.floor(r2000 / 730484);
  const year = aprime + 1;
  const t = Math.floor((364 + s - n) / 306);
  let month = t * (Math.floor(n / 31) + 1) + (1 - t) * (Math.floor((5 * (n - s) + 13) / 153) + 1);
  n += 1 - Math.floor(r2000 / 730484);
  let day = n;

  if ((r100 === 0) && (n === 0) && (r400 !== 0)) {
    month = 12;
    day = 31;
  } else {
    monthDays[2] = (leapYear(year)) ? 29 : 28;
    for (let i = 1; i <= nMonths; i += 1) {
      if (n <= monthDays[i]) {
        day = n;
        break;
      }

      n -= monthDays[i];
    }
  }

  return { year, month, day };
}
861877
private static int lYearDays(int year) {
        int i, sum = 348;
        for (i = 0x8000; i > 0x8; i >>= 1) {
            if ((lunarInfo[year - 1900] & i) != 0)
                sum += 1;
        }
        return (sum + leapDays(year));
    }
429759
def hdate_to_jdn(date):
    
    day = date.day
    month = date.month
    if date.month == 13:
        month = 6
    if date.month == 14:
        month = 6
        day += 30

    # Calculate days since 1,1,3744
    day = _days_from_3744(date.year) + (59 * (month - 1) + 1) // 2 + day

    # length of year
    length_of_year = get_size_of_hebrew_year(date.year)
    # Special cases for this year
    if length_of_year % 10 > 4 and month > 2:  # long Heshvan
        day += 1
    if length_of_year % 10 < 4 and month > 3:  # short Kislev
        day -= 1
    if length_of_year > 365 and month > 6:  # leap year
        day += 30

    # adjust to julian
    return day + 1715118
85782
def _from_jd_equinox(jd):
    ''''''
    jd = trunc(jd) + 0.5
    equinoxe = premier_da_la_annee(jd)

    an = gregorian.from_jd(equinoxe)[0] - YEAR_EPOCH
    mois = trunc((jd - equinoxe) / 30.) + 1
    jour = int((jd - equinoxe) % 30) + 1

    return (an, mois, jour)
657878
def from_jd(jdc):
    ""
    cdc = floor(jdc) + 0.5 - EPOCH
    year = floor((cdc - floor((cdc + 366) / 1461)) / 365) + 1

    yday = jdc - to_jd(year, 1, 1)

    month = floor(yday / 30) + 1
    day = yday - (month - 1) * 30 + 1
    return year, month, day
676006
def jdn_to_hdate(jdn):
    """"""
    # calculate Gregorian date
    date = jdn_to_gdate(jdn)

    # Guess Hebrew year is Gregorian year + 3760
    year = date.year + 3760

    jdn_tishrey1 = hdate_to_jdn(HebrewDate(year, 1, 1))
    jdn_tishrey1_next_year = hdate_to_jdn(HebrewDate(year + 1, 1, 1))

    # Check if computed year was underestimated
    if jdn_tishrey1_next_year <= jdn:
        year = year + 1
        jdn_tishrey1 = jdn_tishrey1_next_year
        jdn_tishrey1_next_year = hdate_to_jdn(HebrewDate(year + 1, 1, 1))

    size_of_year = get_size_of_hebrew_year(year)

    # days into this year, first month 0..29
    days = jdn - jdn_tishrey1

    # last 8 months always have 236 days
    if days >= (size_of_year - 236):  # in last 8 months
        days = days - (size_of_year - 236)
        month = days * 2 // 59
        day = days - (month * 59 + 1) // 2 + 1

        month = month + 4 + 1

        # if leap
        if size_of_year > 355 and month <= 6:
            month = month + 8
    else:  # in 4-5 first months
        # Special cases for this year
        if size_of_year % 10 > 4 and days == 59:   # long Heshvan (day 30)
            month = 1
            day = 30
        elif size_of_year % 10 > 4 and days > 59:  # long Heshvan
            month = (days - 1) * 2 // 59
            day = days - (month * 59 + 1) // 2
        elif size_of_year % 10 < 4 and days > 87:  # short kislev
            month = (days + 1) * 2 // 59
            day = days - (month * 59 + 1) // 2 + 2
        else:  # regular months
            month = days * 2 // 59
            day = days - (month * 59 + 1) // 2 + 1

        month = month + 1

    return HebrewDate(year, month, day)
1261802
private static int[] jdToGregorian(double jd) {
       double JULIAN_EPOCH = 1721425.5;
       double wjd, depoch, quadricent, dqc, cent, dcent, quad, dquad, yindex, yearday, leapadj;
       int year, month, day;
       
       wjd = Math.floor(jd - 0.5) + 0.5;
       depoch = wjd - JULIAN_EPOCH;
       quadricent = Math.floor(depoch / 146097);
       dqc = depoch % 146097;
       cent = Math.floor(dqc / 36524);
       dcent = dqc % 36524;
       quad = Math.floor(dcent / 1461);
       dquad = dcent % 1461;
       yindex = Math.floor(dquad / 365);
       year = (int)((quadricent * 400) + (cent * 100) + (quad * 4) + yindex);
       
       if (!((cent == 4) || (yindex == 4))) {
          year++;
       }
       
       yearday = wjd - gregorianToJD(year, 1, 1);
       leapadj = ((wjd < gregorianToJD(year, 3, 1)) ? 0
             :
             (isGregorianLeap(year) ? 1 : 2)
             );
       
       month = (int)Math.floor((((yearday + leapadj) * 12) + 373) / 367);
       day = (int)(wjd - gregorianToJD(year, month, 1)) + 1;

       int[] julianDate = new int[3];
       
       julianDate[0] = year;
       julianDate[1] = month;
       julianDate[2] = day;
       
       return julianDate;
    }
759553
def from_jd(jd):
    ''''''
    year = gregorian.from_jd(jd)[0]
    day = jwday(jd) + 1

    dayofyear = ordinal.from_jd(jd)[1]
    week = trunc((dayofyear - day + 10) / 7)

    # Reset year
    if week < 1:
        week = weeks_per_year(year - 1)
        year = year - 1

    # Check that year actually has 53 weeks
    elif week == 53 and weeks_per_year(year) != 53:
        week = 1
        year = year + 1

    return year, week, day
550380
def to_jd(year, month, day):
    ''''''

    if year >= 0:
        y = 474
    else:
        y = 473
    epbase = year - y
    epyear = 474 + (epbase % 2820)

    if month <= 7:
        m = (month - 1) * 31
    else:
        m = (month - 1) * 30 + 6

    return day + m + trunc(((epyear * 682) - 110) / 2816) + (epyear - 1) * 365 + trunc(epbase / 2820) * 1029983 + (EPOCH - 1)
1102748
function jWeekOfYear(mom, firstDayOfWeek, firstDayOfWeekOfYear) {
    var end = firstDayOfWeekOfYear - firstDayOfWeek
        , daysToDayOfWeek = firstDayOfWeekOfYear - mom.day()
        , adjustedMoment;

    if (daysToDayOfWeek > end) {
        daysToDayOfWeek -= 7;
    }
    if (daysToDayOfWeek < end - 7) {
        daysToDayOfWeek += 7;
    }
    adjustedMoment = jMoment(mom).add(daysToDayOfWeek, "d");
    return  { week: Math.ceil(adjustedMoment.jDayOfYear() / 7)
        , year: adjustedMoment.jYear()
    };
}
976211
private static function iauCal2jd($iy, $im, $id, &$djm0, &$djm)
    {
        $j;
        $ly;
        $my;
        $iypmy;
        /* Earliest year allowed (4800BC) */
        $IYMIN = -4799;
        /* Month lengths in days */
        $mtab = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
        /* Preset status. */
        $j = 0;
        /* Validate year and month. */
        if ($iy < $IYMIN) {
            return -1;
        }
        if ($im < 1 || $im > 12) {
            return -2;
        }
        /* If February in a leap year, 1, otherwise 0. */
        $ly = (($im == 2) && !($iy % 4) && ($iy % 100 || !($iy % 400)));
        /* Validate day, taking into account leap years. */
        if (($id < 1) || ($id > ($mtab[$im - 1] + $ly))) {
            $j = -3;
        }
        /* Return result. */
        $my    = intval(($im - 14) / 12);
        $iypmy = intval($iy + $my);
        $djm0  = 2400000.5;
        $djm   = (double)(intval((1461 * ($iypmy + 4800)) / 4) +
          intval((367 * intval($im - 2 - 12 * $my)) / 12) -
          intval((3 * (intval(($iypmy + 4900) / 100))) / 4) +
          intval($id - 2432076));

        /* Return status. */

        return $j;
    }
635648
private static Integer[] getAdjustedMonthDays(int year) {
        Integer[] newMonths;
        try {
            newMonths = ADJUSTED_MONTH_DAYS.get(Integer.valueOf(year));
        } catch (ArrayIndexOutOfBoundsException e) {
            newMonths = null;
        }
        if (newMonths == null) {
            if (isLeapYear(year)) {
                newMonths = DEFAULT_LEAP_MONTH_DAYS;
            } else {
                newMonths = DEFAULT_MONTH_DAYS;
            }
        }
        return newMonths;
    }
1130908
def julian(mon, day, year):
    
    ig = 15 + 31 * (10 + 12 * 1582)
    if year == 0:
        print("Julian no can do")
        return
    if year < 0:
        year = year + 1
    if mon > 2:
        julian_year = year
        julian_month = mon + 1
    else:
        julian_year = year - 1
        julian_month = mon + 13
    j1 = int(365.25 * julian_year)
    j2 = int(30.6001 * julian_month)
    j3 = day + 1720995
    julian_day = j1 + j2 + j3
    if day + 31 * (mon + 12 * year) >= ig:
        jadj = int(0.01 * julian_year)
        julian_day = julian_day + 2 - jadj + int(0.25 * jadj)
    return julian_day
1091283
def to_jd(year, month, day, method=None):
    ''''''
    method = method or 'equinox'

    if day < 1 or day > 30:
        raise ValueError("Invalid day for this calendar")

    if month > 13:
        raise ValueError("Invalid month for this calendar")

    if month == 13 and day > 5 + leap(year, method=method):
        raise ValueError("Invalid day for this month in this calendar")

    if method == 'equinox':
        return _to_jd_equinox(year, month, day)

    else:
        return _to_jd_schematic(year, month, day, method)
416440
def from_jd(jd):
    ''''''
    wjd = floor(jd - 0.5) + 0.5
    depoch = wjd - EPOCH

    quadricent = floor(depoch / INTERCALATION_CYCLE_DAYS)
    dqc = depoch % INTERCALATION_CYCLE_DAYS

    cent = floor(dqc / LEAP_SUPPRESSION_DAYS)
    dcent = dqc % LEAP_SUPPRESSION_DAYS

    quad = floor(dcent / LEAP_CYCLE_DAYS)
    dquad = dcent % LEAP_CYCLE_DAYS

    yindex = floor(dquad / YEAR_DAYS)
    year = (
        quadricent * INTERCALATION_CYCLE_YEARS +
        cent * LEAP_SUPPRESSION_YEARS +
        quad * LEAP_CYCLE_YEARS + yindex
    )

    if not (cent == 4 or yindex == 4):
        year += 1

    yearday = wjd - to_jd(year, 1, 1)

    leap = isleap(year)

    if yearday < 58 + leap:
        leap_adj = 0
    elif leap:
        leap_adj = 1
    else:
        leap_adj = 2

    month = floor((((yearday + leap_adj) * 12) + 373) / 367)
    day = int(wjd - to_jd(year, month, 1)) + 1

    return (year, month, day)
175479
function(jd) {
        jd = Math.floor(jd) + 0.5;
        var year = Math.floor((30 * (jd - this.jdEpoch) + 10646) / 10631);
        year = (year <= 0 ? year - 1 : year);
        var month = Math.min(12, Math.ceil((jd - 29 - this.toJD(year, 1, 1)) / 29.5) + 1);
        var day = jd - this.toJD(year, month, 1) + 1;
        return this.newDate(year, month, day);
    }
479109
function (jd) {
			var mcjdn = jd - 2400000 + 0.5; // Modified Chronological Julian Day Number (MCJDN)
			// the MCJDN's of the start of the lunations in the Umm al-Qura calendar 
			// are stored in the 'ummalqura_dat' array
			var index = 0;
			for (var i = 0; i < ummalqura_dat.length; i++) {
				if (ummalqura_dat[i] > mcjdn) break;
				index++;
			}
			var lunation = index + 15292; //UmmAlQura Lunation Number
			var ii = Math.floor((lunation - 1) / 12);
			var year = ii + 1;
			var month = lunation - 12 * ii;
			var day = mcjdn - ummalqura_dat[index - 1] + 1;
			return this.newDate(year, month, day);
		}