def jd_to_bahai(jd):
    #//  JD_TO_BAHAI  --  Calculate Bahai date from Julian day
    
    jd = floor(jd) + 0.5
    gy = jd_to_gregorian(jd)[0]
    bstarty = jd_to_gregorian(BAHAI_EPOCH)[0]

    if jd <= gregorian_to_jd(gy, 3, 20):
        x = 1
    else:
        x = 0
    # verify this next line...
    bys = gy - (bstarty + (((gregorian_to_jd(gy, 1, 1) <= jd) and x)))
    major = floor(bys / 361) + 1
    cycle = floor(mod(bys, 361) / 19) + 1
    year = mod(bys, 19) + 1
    days = jd - bahai_to_jd(major, cycle, year, 1, 1)
    bld = bahai_to_jd(major, cycle, year, 20, 1)
    if jd >= bld:
        month = 20
    else:
        month = floor(days / 19) + 1
    day = int((jd + 1) - bahai_to_jd(major, cycle, year, month, 1))
    
    return (major, cycle, year, month, day)
def jd_to_persian(jd):
    #//  JD_TO_PERSIAN  --  Calculate Persian date from Julian day
    jd = floor(jd) + 0.5
 
    depoch = jd - persian_to_jd(475, 1, 1)
    cycle = floor(depoch / 1029983)
    cyear = mod(depoch, 1029983)
    if cyear == 1029982:
        ycycle = 2820
    else:
        aux1 = floor(cyear / 366)
        aux2 = mod(cyear, 366)
        ycycle = floor(((2134 * aux1) + (2816 * aux2) + 2815) / 1028522) + aux1 + 1

    year = ycycle + (2820 * cycle) + 474
    if (year <= 0):
        year -= 1

    yday = (jd - persian_to_jd(year, 1, 1)) + 1
    if yday <= 186:
        month = ceil(yday / 31)
    else:
        month = ceil((yday - 6) / 30)

    day = int(jd - persian_to_jd(year, month, 1)) + 1
    return (year, month, day)
def jd_to_islamic(jd):
    #//  JD_TO_ISLAMIC  --  Calculate Islamic date from Julian day
        
    jd = floor(jd) + 0.5
    year = floor(((30 * (jd - ISLAMIC_EPOCH)) + 10646) / 10631)
    month = min(12,        
                ceil((jd - (29 + islamic_to_jd(year, 1, 1))) / 29.5) + 1)
    day = int(jd - islamic_to_jd(year, month, 1)) + 1
    return (year, month, day)
def jd_to_mayan_count(jd):
    #//  JD_TO_MAYAN_COUNT  --  Calculate Mayan long count from Julian day
    d = jd - MAYAN_COUNT_EPOCH;
    baktun = floor(d / 144000)
    d = mod(d, 144000);
    katun = floor(d / 7200)
    d = mod(d, 7200)
    tun = floor(d / 360)
    d = mod(d, 360)
    uinal = floor(d / 20)
    kin = int(mod(d, 20))
    
    return (baktun, katun, tun, uinal, kin)
def  julian_to_jd(year, month, day):
    #/* Adjust negative common era years to the zero-based notation we use.  */

    if year < 1:
        year += 1

    #/* Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61 */

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

    return ((floor((365.25 * (year + 4716))) +
            floor((30.6001 * (month + 1))) +
            day) - 1524.5)
def islamic_to_jd(year, month, day):
    #//  ISLAMIC_TO_JD  --  Determine Julian day from Islamic date
    return ((day +
            ceil(29.5 * (month - 1)) +
            (year - 1) * 354 +
            floor((3 + (11 * year)) / 30) +
            ISLAMIC_EPOCH) - 1)
def jd_to_french_revolutionary(jd):
    #/*  JD_TO_FRENCH_REVOLUTIONARY  --  Calculate date in the French Revolutionary
    #                                    calendar from Julian day.  The five or six
    #                                    "sansculottides" are considered a thirteenth
    #                                    month in the results of this function.  */
    jd = floor(jd) + 0.5
    adr = annee_da_la_revolution(jd)
    an = int(adr[0])
    equinoxe = adr[1]
    mois = floor((jd - equinoxe) / 30) + 1
    jour = (jd - equinoxe) % 30
    decade = floor(jour / 10) + 1
    jour = int(jour % 10) + 1

    
    return (an, mois, decade, jour)
def gregorian_to_jd(year, month, day):
    if month <= 2:
        leap_adj = 0
    elif leap_gregorian(year):
        leap_adj = -1
    else:
        leap_adj = -2

    return ((GREGORIAN_EPOCH - 1) +
           (365 * (year - 1)) +
           floor((year - 1) / 4) +
           (-floor((year - 1) / 100)) +
           floor((year - 1) / 400) +
           floor((((367 * month) - 362) / 12) +
           leap_adj +
           day))
def jd_to_julian(td):
    #//  JD_TO_JULIAN  --  Calculate Julian calendar date from Julian day

    td += 0.5;
    z = floor(td)

    a = z
    b = a + 1524
    c = floor((b - 122.1) / 365.25)
    d = floor(365.25 * c)
    e = floor((b - d) / 30.6001)

    if floor(e < 14):
        month = e - 1
    else:
        month = e - 13

    if floor(month > 2):
        year = c - 4716
    else:
        year = c - 4715

    day = b - d - floor(30.6001 * e)

    #/*  If year is less than 1, subtract one to convert from
    #    a zero based date system to the common era system in
    #    which the year -1 (1 B.C.E) is followed by year 1 (1 C.E.).  */

    if year < 1:
        year -= 1

    return (year, month, day)
def paris_equinoxe_jd(year):
    #/*  PARIS_EQUINOXE_JD  --  Calculate Julian day during which the  
    #                           September equinox, reckoned from the Paris
    #                           meridian, occurred for a given Gregorian
    #                           year.  */'
    ep = equinoxe_a_paris(year)
    epg = floor(ep - 0.5) + 0.5

    return epg
def jd_to_iso(jd):
    #//  JD_TO_ISO  --  Return tuple of ISO (year, week, day) for Julian day
    year = jd_to_gregorian(jd - 3)[0]
    if jd >= iso_to_julian(year + 1, 1, 1):
        year += 1
    week = floor((jd - iso_to_julian(year, 1, 1)) / 7) + 1
    day = jwday(jd)
    if day == 0:
        day = 7

    return (year, week, day)
def persian_to_jd(year, month, day):
    #//  PERSIAN_TO_JD  --  Determine Julian day from Persian date

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

    if month <= 7:
        m = (month - 1) * 31
    else:
        m = (month - 1) * 30 + 6
 
    return (day +
            m +
            floor(((epyear * 682) - 110) / 2816) +
            (epyear - 1) * 365 +
            floor(epbase / 2820) * 1029983 +
            (PERSIAN_EPOCH - 1))
def jd_to_hebrew(jd):
    jd = floor(jd) + 0.5;
    count = floor(((jd - HEBREW_EPOCH) * 98496.0) / 35975351.0)
    year = count - 1;
    i = count
    while jd >= hebrew_to_jd(i, 7, 1):
        i += 1
        year += 1

    if jd < hebrew_to_jd(year, 1, 1):
        first = 7
    else:
        first = 1

    month = i = first;
    while jd > hebrew_to_jd(year, i, hebrew_month_days(year, i)):
        i += 1
        month += 1

    day = int(jd - hebrew_to_jd(year, month, 1)) + 1
    return (year, month, day)
def jd_to_gregorian(jd):
    wjd = floor(jd - 0.5) + 0.5
    depoch = wjd - GREGORIAN_EPOCH
    quadricent = floor(depoch / 146097)
    dqc = mod(depoch, 146097)
    cent = floor(dqc / 36524)
    dcent = mod(dqc, 36524)
    quad = floor(dcent / 1461)
    dquad = mod(dcent, 1461)
    yindex = floor(dquad / 365)
    year = (quadricent * 400) + (cent * 100) + (quad * 4) + yindex
    if not (cent == 4 or yindex == 4):
        year +=1

    yearday = wjd - gregorian_to_jd(year, 1, 1)
    if wjd < gregorian_to_jd(year, 3, 1):
        leap_adj = 0
    elif leap_gregorian(year):
        leap_adj = 1
    else:
        leap_adj = 2


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

    if month == 0 and day == 30:
        year -= 1;
        month = 12;
        day = 31;

    return (year, month, day)
def jd_to_indian_civil(jd):
    #//  JD_TO_INDIAN_CIVIL  --  Calculate Indian Civil date from Julian day
    Saka = 79 - 1;                    #// Offset in years from Saka era to Gregorian epoch
    start = 80;                       #// Day offset between Saka and Gregorian
    
    jd = floor(jd) + 0.5
    greg = jd_to_gregorian(jd)        #// Gregorian date for Julian day
    leap = leap_gregorian(greg[0])    #// Is this a leap year?
    year = greg[0] - Saka             #// Tentative year in Saka era
    greg0 = gregorian_to_jd(greg[0], 1, 1)    #// JD at start of Gregorian year
    yday = jd - greg0                #// Day number (0 based) in Gregorian year
    
    if leap:
        Caitra = 31           #// Days in Caitra this year
    else:
        Caitra = 30
    
    if yday < start:
        #//  Day is at the end of the preceding Saka year
        year -= 1
        yday += Caitra + (31 * 5) + (30 * 3) + 10 + start


    yday -= start
    if yday < Caitra:
        month = 1
        day = yday + 1
    else:
        mday = yday - Caitra
        if (mday < (31 * 5)):
            month = floor(mday / 31) + 2
            day = (mday % 31) + 1
        else:
            mday -= 31 * 5; 
            month = floor(mday / 30) + 7
            day = (mday % 30) + 1

    return (year, month, int(day))
def jd_to_iso_day(jd):
    #//  JD_TO_ISO_DAY  --  Return tuple of ISO (year, day_of_year) for Julian day
    year = jd_to_gregorian(jd)[0]
    day = floor(jd - gregorian_to_jd(year, 1, 1)) + 1
    return (year, day)
def jd_to_mayan_haab(jd):
    #//  JD_TO_MAYAN_HAAB  --  Determine Mayan Haab "month" and day from Julian day
    lcount = jd - MAYAN_COUNT_EPOCH
    day = mod(lcount + 8 + ((18 - 1) * 20), 365)
     
    return (floor(day / 20) + 1, int(mod(day, 20)))