# Path: src/convertdate/gregorian.py
# def from_jd(jd):
#     '''Return Gregorian date in a (Y, M, D) tuple'''
#     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)
# 
# Path: src/convertdate/gregorian.py
# def to_jd(year, month, day):
#     '''Convert gregorian date to julian day count.'''
#     legal_date(year, month, day)
# 
#     if month <= 2:
#         leap_adj = 0
#     elif isleap(year):
#         leap_adj = -1
#     else:
#         leap_adj = -2
# 
#     return (
#         EPOCH
#         - 1
#         + (YEAR_DAYS * (year - 1))
#         + floor((year - 1) / LEAP_CYCLE_YEARS)
#         + (-floor((year - 1) / LEAP_SUPPRESSION_YEARS))
#         + floor((year - 1) / INTERCALATION_CYCLE_YEARS)
#         + floor((((367 * month) - 362) / 12) + leap_adj + day)
#     )
# 
# Path: src/convertdate/utils.py
# def floor(x):
#     return int(math.floor(x))
# 
# def jwday(j):
#     '''Calculate day of week from Julian day. Consider using ``calendar.weekday``!'''
#     return math.trunc((j + 0.5)) % 7
# 
# def monthcalendarhelper(start_weekday, month_length):
#     end_weekday = start_weekday + (month_length - 1) % 7
# 
#     lpad = (start_weekday + 1) % 7
#     rpad = (5 - end_weekday % 7) % 6
# 
#     days = [None] * lpad + list(range(1, 1 + month_length)) + rpad * [None]
# 
#     return [days[i : i + 7] for i in range(0, len(days), 7)]


from .gregorian import from_jd as gregorian_from_jd
from .gregorian import to_jd as gregorian_to_jd
from .utils import floor, jwday, monthcalendarhelper

def from_jd(jd):
    '''Calculate Julian calendar date from Julian day'''
    jd += 0.5
    a = floor(jd)
    b = a + 1524
    c = floor((b - 122.1) / 365.25)
    d = floor(365.25 * c)
    e = floor((b - d) / 30.6001)
    if e < 14:
        month = floor(e - 1)
    else:
        month = floor(e - 13)
    if month > 2:
        year = floor(c - 4716)
    else:
        year = floor(c - 4715)
    day = b - d - floor(30.6001 * e)
    return (year, month, day)


def to_jd(year, month, day):
    '''Convert to Julian day using astronomical years (0 = 1 BC, -1 = 2 BC)'''
    legal_date(year, month, day)

    # Algorithm as given in Meeus, Astronomical Algorithms, Chapter 7, page 61
    if month <= 2:
        year -= 1
        month += 12


# Path: src/convertdate/gregorian.py
# def from_jd(jd):
#     '''Return Gregorian date in a (Y, M, D) tuple'''
#     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)
# 
# Path: src/convertdate/gregorian.py
# def to_jd(year, month, day):
#     '''Convert gregorian date to julian day count.'''
#     legal_date(year, month, day)
# 
#     if month <= 2:
#         leap_adj = 0
#     elif isleap(year):
#         leap_adj = -1
#     else:
#         leap_adj = -2
# 
#     return (
#         EPOCH
#         - 1
#         + (YEAR_DAYS * (year - 1))
#         + floor((year - 1) / LEAP_CYCLE_YEARS)
#         + (-floor((year - 1) / LEAP_SUPPRESSION_YEARS))
#         + floor((year - 1) / INTERCALATION_CYCLE_YEARS)
#         + floor((((367 * month) - 362) / 12) + leap_adj + day)
#     )
# 
# Path: src/convertdate/utils.py
# def floor(x):
#     return int(math.floor(x))
# 
# def jwday(j):
#     '''Calculate day of week from Julian day. Consider using ``calendar.weekday``!'''
#     return math.trunc((j + 0.5)) % 7
# 
# def monthcalendarhelper(start_weekday, month_length):
#     end_weekday = start_weekday + (month_length - 1) % 7
# 
#     lpad = (start_weekday + 1) % 7
#     rpad = (5 - end_weekday % 7) % 6
# 
#     days = [None] * lpad + list(range(1, 1 + month_length)) + rpad * [None]
# 
#     return [days[i : i + 7] for i in range(0, len(days), 7)]


# Path: src/convertdate/julian.py
from .gregorian import from_jd as gregorian_from_jd
from .gregorian import to_jd as gregorian_to_jd
from .utils import floor, jwday, monthcalendarhelper


def from_jd(jd):
    '''Calculate Julian calendar date from Julian day'''
    jd += 0.5
    a = floor(jd)
    b = a + 1524
    c = floor((b - 122.1) / 365.25)
    d = floor(365.25 * c)
    e = floor((b - d) / 30.6001)
    if e < 14:
        month = floor(e - 1)
    else:
        month = floor(e - 13)
    if month > 2:
        year = floor(c - 4716)
    else:
        year = floor(c - 4715)
    day = b - d - floor(30.6001 * e)
    return (year, month, day)


def to_jd(year, month, day):
    '''Convert to Julian day using astronomical years (0 = 1 BC, -1 = 2 BC)'''
    legal_date(year, month, day)

    # 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