Determine Julian day from Persian date
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)
1294096
def SunLongitude(jdn):
    '''
    '''
    T = (jdn - 2451545.0) / 36525.
    # Time in Julian centuries
    # from 2000-01-01 12:00:00 GMT
    T2 = T * T
    dr = math.pi / 180.  # degree to radian
    M = 357.52910 + 35999.05030 * T \
        - 0.0001559 * T2 - 0.00000048 * T * T2
    # mean anomaly, degree
    L0 = 280.46645 + 36000.76983 * T + 0.0003032 * T2
    # mean longitude, degree
    DL = (1.914600 - 0.004817 * T - 0.000014 * T2) \
        * math.sin(dr * M)
    DL += (0.019993 - 0.000101 * T) * math.sin(dr * 2 * M) \
        + 0.000290 * math.sin(dr * 3 * M)
    L = L0 + DL  # true longitude, degree
    L = L * dr
    L = L - math.pi * 2 * (float(L / (math.pi * 2)))
    # Normalize to (0, 2*math.pi)
    return L
281842
def julian_day(t: date) -> int:
    """"""
    # Compute the number of days from January 1, 2000 to date t
    dt = t - julian_base_date
    # Add the julian base number to the number of days from the julian base date to date t
    return julian_base_number + dt.days
1165225
def to_jd(year, month, day):
    ''''''

    gyear = year + 78
    leap = isleap(gyear)
    # // Is this a leap year ?

    # 22 - leap = 21 if leap, 22 non-leap
    start = gregorian.to_jd(gyear, 3, 22 - leap)
    if leap:
        Caitra = 31
    else:
        Caitra = 30

    if month == 1:
        jd = start + (day - 1)
    else:
        jd = start + Caitra
        m = month - 2
        m = min(m, 5)
        jd += m * 31
        if month >= 8:
            m = month - 7
            jd += m * 30

        jd += day - 1

    return jd
593077
protected function jdToY($julian_day)
    {
        // Estimate the year, and underestimate it, it will be refined after
        $year = max((int) ((($julian_day - 347998) * 98496) / 35975351) - 1, 1);

        // Adjust by adding years;
        while ($julian_day >= $this->yToJd($year + 1)) {
            $year++;
        }

        return $year;
    }
268585
function moonPos(date) {
  // julian date
  var jd = getJD(date) + 2451543.5;
  var T = (jd - 2451545.0) / 36525;
  var T2 = T * T;
  var T3 = T2 * T;
  var T4 = T3 * T;
  // Moons mean longitude L'
  var LP = 218.3164477 + 481267.88123421 * T - 0.0015786 * T2 + T3 / 538841.0 - T4 / 65194000.0;
  // Moons mean elongation
  var D = 297.8501921 + 445267.1114034 * T - 0.0018819 * T2 + T3 / 545868.0 - T4 / 113065000.0;
  // Suns mean anomaly
  var M = 357.5291092 + 35999.0502909 * T - 0.0001536 * T2 + T3 / 24490000.0;
  // Moons mean anomaly M'
  var MP = 134.9633964 + 477198.8675055 * T + 0.0087414 * T2 + T3 / 69699.0 - T4 / 14712000.0;
  // Moons argument of latitude
  var F = 93.2720950 + 483202.0175233 * T - 0.0036539 * T2 - T3 / 3526000.0 + T4 / 863310000.0;

  // Additional arguments
  var A1 = 119.75 + 131.849 * T;
  var A2 = 53.09 + 479264.290 * T;
  var A3 = 313.45 + 481266.484 * T;
  var E = 1 - 0.002516 * T - 0.0000074 * T2;
  var E2 = E * E;
  // Sums of periodic terms from table 45.A and 45.B
  var Sl = 0.0;
  var Sr = 0.0;
  for (var i = 0; i < 60; i++) {
    var Eterm = 1;
    if (Math.abs(T45AM[i]) === 1) Eterm = E;
    if (Math.abs(T45AM[i]) === 2) Eterm = E2;
    Sl += T45AL[i] * Eterm * sind(rev(T45AD[i] * D + T45AM[i] * M + T45AMP[i] * MP + T45AF[i] * F));
    Sr += T45AR[i] * Eterm * cosd(rev(T45AD[i] * D + T45AM[i] * M + T45AMP[i] * MP + T45AF[i] * F));
  }
  var Sb = 0.0;
  for (var i = 0; i < 60; i++) {
    var Eterm = 1;
    if (Math.abs(T45BM[i]) === 1) Eterm = E;
    if (Math.abs(T45BM[i]) === 2) Eterm = E2;
    Sb += T45BL[i] * Eterm * sind(rev(T45BD[i] * D + T45BM[i] * M + T45BMP[i] * MP + T45BF[i] * F));
  }
  // Additional additive terms
  Sl = Sl + 3958 * sind(rev(A1)) + 1962 * sind(rev(LP - F)) + 318 * sind(rev(A2));
  Sb = Sb - 2235 * sind(rev(LP)) + 382 * sind(rev(A3)) + 175 * sind(rev(A1 - F)) +
    175 * sind(rev(A1 + F)) + 127 * sind(rev(LP - MP)) - 115 * sind(rev(LP + MP));
  // geocentric longitude, latitude and distance
  var mglong = rev(LP + Sl / 1000000.0);
  var mglat = rev(Sb / 1000000.0);
  if (mglat > 180.0) mglat = mglat - 360;
  var mr = Math.round(385000.56 + Sr / 1000.0);
  // Obliquity of Ecliptic
  var obl = 23.4393 - 3.563E-9 * (jd - 2451543.5);
  // RA and dec
  var ra = rev(atan2d(sind(mglong) * cosd(obl) - tand(mglat) * sind(obl),
    cosd(mglong))) / 15.0;
  var dec = rev(asind(sind(mglat) * cosd(obl) + cosd(mglat) * sind(obl) * sind(mglong)));
  if (dec > 180.0) dec = dec - 360;
  return {
    ra: ra,
    dec: dec,
    distance: mr
  };
}
685895
def caleom(date):
        
        date = parsefun(date)
        date += datetime.timedelta(days=32-date.day)
        date -= datetime.timedelta(days=date.day)
        return date
509785
@Override
    public long toEpochDay() {
        long year = (long) prolepticYear;
        long julianEpochDay = ((year - 1) * 365) + Math.floorDiv((year - 1), 4) + (getDayOfYear() - 1);
        return julianEpochDay - JULIAN_0001_TO_ISO_1970;
    }
98528
final int getMajorSolarTerm(long utcDays) {
        double jd = JulianDay.ofEphemerisTime(this.midnight(utcDays)).getValue();
        int index = (2 + (int) Math.floor(SolarTerm.solarLongitude(jd) / 30)) % 12;
        return ((index == 0) ? 12 : index);
    }
678584
private static function getDayNames($day, $shorten = false, $len = 1, $numeric = false)
    {
        $days = ['sat' => [1, 'شنبه'], 'sun' => [2, 'یکشنبه'], 'mon' => [3, 'دوشنبه'], 'tue' => [4, 'سه شنبه'], 'wed' => [5, 'چهارشنبه'], 'thu' => [6, 'پنجشنبه'], 'fri' => [7, 'جمعه']];
        $day = substr(strtolower($day), 0, 3);
        $day = $days[ $day ];

        return ($numeric) ? $day[0] : (($shorten) ? self::substr($day[1], 0, $len) : $day[1]);
    }
784097
def to_jd2(year, month, day):
    ''''''
    # http://quasar.as.utexas.edu/BillInfo/JulianDatesG.html

    legal_date(year, month, day)

    if month <= 2:
        year = year - 1
        month = month + 12

    a = floor(year / 100)
    b = floor(a / 4)
    c = 2 - a + b
    e = floor(365.25 * (year + 4716))
    f = floor(30.6001 * (month + 1))
    return c + day + e + f - 1524.5
14599
def _DateToEpoch(date):
  """"""
  tz_zero = datetime.datetime.utcfromtimestamp(0)
  diff_sec = int((date - tz_zero).total_seconds())
  return diff_sec * 1000000
1125203
def from_jd(jd):
    ''''''

    jd = trunc(jd) + 0.5
    year = trunc(((30 * (jd - EPOCH)) + 10646) / 10631)
    month = min(12, ceil((jd - (29 + to_jd(year, 1, 1))) / 29.5) + 1)
    day = int(jd - to_jd(year, month, 1)) + 1
    return (year, month, day)
1052712
private static double IndianToJD(int year, int month, int date) {
       int leapMonth, gyear, m;
       double start, jd;

       gyear = year + INDIAN_ERA_START;


       if(isGregorianLeap(gyear)) {
          leapMonth = 31;
          start = gregorianToJD(gyear, 3, 21);
       } else {
          leapMonth = 30;
          start = gregorianToJD(gyear, 3, 22);
       }

       if (month == 1) {
          jd = start + (date - 1);
       } else {
          jd = start + leapMonth;
          m = month - 2;
          m = Math.min(m, 5);
          jd += m * 31;
          if (month >= 8) {
             m = month - 7;
             jd += m * 30;
          }
          jd += date - 1;
       }

       return jd;
    }
1336558
function (date) {
                if (date instanceof Date === false) {
                    throw new ArgumentError(
                        Logger.logMessage(Logger.LEVEL_SEVERE, "SunPosition", "getAsCelestialLocation",
                            "missingDate"));
                }

                var julianDate = this.computeJulianDate(date);

                //number of days (positive or negative) since Greenwich noon, Terrestrial Time, on 1 January 2000 (J2000.0)
                var numDays = julianDate - 2451545;

                var meanLongitude = WWMath.normalizeAngle360(280.460 + 0.9856474 * numDays);

                var meanAnomaly = WWMath.normalizeAngle360(357.528 + 0.9856003 * numDays) * Angle.DEGREES_TO_RADIANS;

                var eclipticLongitude = meanLongitude + 1.915 * Math.sin(meanAnomaly) + 0.02 * Math.sin(2 * meanAnomaly);
                var eclipticLongitudeRad = eclipticLongitude * Angle.DEGREES_TO_RADIANS;

                var obliquityOfTheEcliptic = (23.439 - 0.0000004 * numDays) * Angle.DEGREES_TO_RADIANS;

                var declination = Math.asin(Math.sin(obliquityOfTheEcliptic) * Math.sin(eclipticLongitudeRad)) *
                    Angle.RADIANS_TO_DEGREES;

                var rightAscension = Math.atan(Math.cos(obliquityOfTheEcliptic) * Math.tan(eclipticLongitudeRad)) *
                    Angle.RADIANS_TO_DEGREES;

                //compensate for atan result
                if (eclipticLongitude >= 90 && eclipticLongitude < 270) {
                    rightAscension += 180;
                }
                rightAscension = WWMath.normalizeAngle360(rightAscension);

                return {
                    declination: declination,
                    rightAscension: rightAscension
                };
            }
87098
def _precesion(date):
    

    t = date.change_scale('TT').julian_century

    zeta = (2306.2181 * t + 0.30188 * t ** 2 + 0.017998 * t ** 3) / 3600.
    theta = (2004.3109 * t - 0.42665 * t ** 2 - 0.041833 * t ** 3) / 3600.
    z = (2306.2181 * t + 1.09468 * t ** 2 + 0.018203 * t ** 3) / 3600.

    # print("zeta = {}\ntheta = {}\nz = {}\n".format(zeta, theta, z))
    return zeta, theta, z
38141
@Deprecated
    protected void handleComputeFields(int julianDay) {
        int era, year;
        int[] fields = new int[3];
        jdToCE(julianDay, getJDEpochOffset(), fields);

        // fields[0] eyear
        // fields[1] month
        // fields[2] day

        if (fields[0] <= 0) {
            era = BCE;
            year = 1 - fields[0];
        } else {
            era = CE;
            year = fields[0];
        }

        internalSet(EXTENDED_YEAR, fields[0]);
        internalSet(ERA, era);
        internalSet(YEAR, year);
        internalSet(MONTH, fields[1]);
        internalSet(DAY_OF_MONTH, fields[2]);
        internalSet(DAY_OF_YEAR, (30 * fields[1]) + fields[2]);
    }